I have a datframe with metric and grouping variables. I want to group the data by several grouping variables, do subplot and then export it to pdf. Right now I have a function which does subplots and returns it to another function. Then that function uses PdfPages to save the subplots to a pdf. However, the issue is that when number of subplots are way too many (like 200), it throws an error:-image size of 1296*96220 pixels is too large. I believe this is happening because my current codes are setup in such a way that it saved all the subplots to a single pdf page, making it a very large page. But then, that can go upto a limit, and since 200 charts are too many, I get this error. Because I have tried with 45 charts and it works. Note that data is in pandas dataframe and charting is done using matplotlib
Following is a rough idea of the code:
the first code takes in a dataframe, does a group by (can be multiple group by parameters) and then produces subplot. Imagine I have two groupby variables- A and B. so when i do subplot with the following code, I am expecting 20*15=300 subplots
grouped=df.groupby(grouping_vars)
fig,axs=plt.subplots( "some parameters")
fig.tight_layout
targets=zip(grouped.groups.keys(),axs.flatten())
for i,(key,ax) in enumerate(targets):
ax.plot("some parameters to plot based on group")
ax.set_title(key)
list_to_be_returned=[fig,plt]
return list_to_be_returned
Now, I use the following fucntion to save it to a pdf file
pp=Pdfpages(file_name)
pp.savefig(list_to_be_returned[0], bbox_inches='tight')
pp.close()
Now, what it does is puts all the subplots in a single large pdf page. I tried with 45 charts, and was successful. I am trying with 200 charts, and its throwing an error. Is there a way to instruct it to save maximum number of charts it can, on a single page, and then move on to next page once required.