Reputation: 45
Is there a way where I can arrange plots without specifying the number of rows but just the columns (3 plots in a row for n row)? I don't want to set a specific amount of rows because different dataframe will be used where the columns extracted to plot the charts is not constant. This section of the code is able to give me the plots but I can't find a way to arrange them.
for i, title in zip(df1.columns[2:7], df2.columns[67:72]):
ax = df1.plot.scatter(x='out_date', y=i, figsize = (8,5), title=title)
ax.set_xlabel('out date')
ax.set_ylabel('successes')
leg = ax.legend(bbox_to_anchor=(1.0,1.0), loc='upper left')
plt.xticks(rotation=90)
Besides that, when I tried saving the plots into a file, it will save each plots as a separate file, so, this is not a convenient solution. I am very new with this but I'm guessing a way to solve this is by using subplots as mentioned in one of the answers here, but again, I don't know how to loop it.
fig = ax.figure
fig.savefig("test plot{}.pdf".format(i), bbox_inches = 'tight')
Above is the code I used which is included in the for loop.
A lot to unpack in this question but any help is appreciated. Thank you.
Upvotes: 0
Views: 358
Reputation: 45
I hope this answer help those who are struggling just like me.
cols = 3
cols_used = len(df1.columns[2:])
rows = cols_used//cols + 1
plt.subplots_adjust(left=None, bottom=None, right=None, top=None, wspace=None, hspace=None)
fig, axs = plt.subplots(rows, cols, figsize=(30,200))
for i, title, ax in zip(df1.columns[2:], df2.columns[:], axs.ravel()):
df1.plot.scatter(x='out_date', y=i, title=title, ax=ax)
ax.axvline(x=cutoff_date, color='r', label='Recent 20pct')
ax.set_xlabel('out date')
ax.set_ylabel('successes')
plt.show()
fig.savefig('out plots.png', dpi = 300)
Referenced tutorial can be found here.
Upvotes: 1