I'm having an issue while trying to plot multiple charts with a for
loop in python.
fig, ax = plt.subplots(nrows=len(Opts_Data_All),figsize=(20,10))
for index, contract in enumerate(Opts_Data_All):
ax[index].set_xlabel('Strike')
ax[index].set_ylabel('Imp Vol')
ax[index].set_title(key)
ax[index].plot(Opts_Data_All[contract]['IV'])
plt.show()
The code above will only squeeze the plots in one figure but I would like to have the charts as subplot.
Total there are 17 plots as there are 17 keys for the dictionary Opts_Data_All
.
Image attached is the result I get
If you want each plot to be in its own figure, then you need to create a new figure in each iteration of the loop.
for contract in Opts_Data_All:
fig, ax = plt.subplots(figsize=(20,10))
ax.plot(Opts_Data_All[contract]['IV'])
ax.set_xlabel('Strike')
ax.set_ylabel('Imp Vol')
ax.set_title(key)
fig.show()