Saving multiple figures to one pdf file in matplotlib

后端 未结 1 1602
面向向阳花
面向向阳花 2021-01-05 01:56

I have a code that creates about 50 graphs based on groupby. The code looks like this:

import matplotlib.pyplot as plt
from matplotlib.backends         


        
相关标签:
1条回答
  • 2021-01-05 02:35

    There is an indentation error in your code. Since your plotting command was not in the loop, it will only create the last plot.

    import matplotlib.pyplot as plt
    from matplotlib.backends.backend_pdf import PdfPages
    
    with PdfPages('foo.pdf') as pdf:
       for i, group in df.groupby('station_id'):
           plt.figure()
           fig=group.plot(x='year', y='Value',title=str(i)).get_figure()
           pdf.savefig(fig)
    
    0 讨论(0)
提交回复
热议问题