def create\plot\image(ticker, expiry, strike, call\put, now): build =…
- User
def create_plot_image(ticker, expiry, strike, call_put, now): build = build_polygon_option_symbol(underlying_symbol=f"{ticker}", expiry=expiry,strike_price=strike,call_or_put=call_put) all = o.get_trades(option_symbol=f"{build}", limit=40000,merge_all_pages=True) d = all.pop('results')
conditions = [i['conditions'] for i in d] price = [i['price'] for i in d] size = [i['size'] for i in d] timestamp = [i['sip_timestamp'] for i in d] #timestap in ns format symb = f"O:{build}" sym = human_readable(symb)
Create the scatter plot
fig, ax = plt.subplots(figsize=(7, 4))
Set the color of the points based on buy/sell
colors = np.where(np.array(conditions) == 'B', 'green', 'red') ax.scatter(price, size, c=colors, alpha=0.7)
Set the background color and grid
ax.set_facecolor('black') ax.grid(color='green', linestyle='--', linewidth=0.5)
Add a text box with data source
props = dict(boxstyle='round', facecolor='white', alpha=0.8)
Add labels and title
ax.set_xlabel('Price Paid', fontsize=14, color='gold') ax.set_ylabel('Contract Volume', fontsize=14, color='gold')
if price:
Set the x-axis scale to 5% below the minimum price and 5% above the maximum price
ax.set_xlim(0.50 * min(price), 1.05 * max(price))
Customize tick labels and legend
fig.tight_layout() ax.tick_params(axis='both', labelsize=14, colors='gold') ax.xaxis.set_major_formatter(ScalarFormatter()) ax.yaxis.set_major_formatter(ScalarFormatter())
Remove the white border surrounding the image
fig.patch.set_facecolor('black') filename = f"plot.png"
Save the plot to a buffer and encode it as base64
buffer = BytesIO() plt.savefig(filename, format='png') plt.savefig(buffer, format='png') buffer.seek(0) image_base64 = base64.b64encode(buffer.getvalue()).decode('utf-8')
return image_base64 Exception has occurred: ValueError 'c' argument must be a color, a sequence of colors, or a sequence of numbers, not red TypeError: len() of unsized object
The above exception was the direct cause of the following exception:
File "C:\Users\chuck\OneDrive\Desktop\channelbot\options_socket\functions.py", line 41, in create_plot_image ax.scatter(price, size, c=colors, alpha=0.7) File "C:\Users\chuck\OneDrive\Desktop\channelbot\socketer_mock_after.py", line 106, in main image_base64= create_plot_image(symb, expiry,option_strike,call_put,now) File "C:\Users\chuck\OneDrive\Desktop\channelbot\socketer_mock_after.py", line 325, in asyncio.run(main()) ValueError: 'c' argument must be a color, a sequence of colors, or a sequence of numbers, not red
- Assistant
It looks like there is an error in the
create_plot_imagefunction. Thecolorsvariable is being created as a numpy array of strings ("green" and "red") based on theconditionslist. However, thecparameter in theax.scattermethod expects either a single color or a sequence of colors as a string or a list of numbers.To fix this error, you can replace the following line:
colors = np.where(np.array(conditions) == 'B', 'green', 'red')with:
colors = np.where(np.array(conditions) == 'B', 'green', 'red').tolist()This will convert the numpy array to a Python list of strings, which can be passed to the
cparameter in theax.scattermethod. - User
there's still no output on the plot
- Assistant
If there is still no output on the plot after making the suggested modification, there could be a few reasons for this. Here are a few things you can try:
- Check that the
price,size, andcolorslists have valid data. You can print out the length of these lists to make sure they are not empty, and also print out some sample values to make sure they are in the expected format. - Check that the x-axis and y-axis limits are set correctly. Make sure the minimum and maximum values in the
priceandsizelists are within the ranges you set withax.set_xlimandax.set_ylim. - Check that the
fig.tight_layout()method is not causing the plot to be cut off. You can try commenting out this line to see if it makes a difference. - Check that the
ax.set_facecolorandax.gridmethods are not hiding the scatter plot points. You can try commenting out these lines to see if the points become visible. - Check that the
human_readablefunction is returning a valid symbol string. You can print out thesymbandsymvariables to see if they are in the expected format.
If none of these suggestions work, you can try adding more debugging statements or posting the code in a forum or chat to get help from other developers.
- Check that the