63

In python (for one figure created in a GUI) I was able to save the figure under .jpg and also .pdf by either using:

plt.savefig(filename1 +  '.pdf')

or

plt.savefig(filename1 +  '.jpg')

Using one file I would like to save multiple figures in either .pdf or .jpg (just like its done in math lab). Can anybody please help with this?

tacaswell
  • 84,579
  • 22
  • 210
  • 199
Ahmed Niri
  • 653
  • 1
  • 5
  • 11

3 Answers3

103

Use PdfPages to solve your problem. Pass your figure object to the savefig method.

For example, if you have a whole pile of figure objects open and you want to save them into a multi-page PDF, you might do:

import matplotlib.backends.backend_pdf
pdf = matplotlib.backends.backend_pdf.PdfPages("output.pdf")
for fig in xrange(1, figure().number): ## will open an empty extra figure :(
    pdf.savefig( fig )
pdf.close()
J0e3gan
  • 8,740
  • 10
  • 53
  • 80
Mind Mixer
  • 1,503
  • 3
  • 13
  • 15
  • 1
    Worked a treat for me too, however I had to use `xrange(1, figure.number)` as `Figure object is not callable` – FriskyGrub Nov 25 '14 at 00:53
  • 2
    I had to use pyplot.gcf().number, which I recommend you use as it's more widely applicable. – jimh Nov 13 '17 at 01:48
  • 3
    Also, one thing I forgot to recommend is you should go to pyplot.gcf().number+1 – jimh Nov 13 '17 at 04:37
  • 5
    @jimh Good advice, thank you! Using `plt.gcf().number + 1` (with `import matplotlib.pyplot as plt`, of course) instead of `figure().number` did the job. (I'm on Python 3.5.) – erekalper Jan 16 '18 at 17:14
  • Great answer, thanks! It helped a lot with some minor modifications. One thing to point out is that you cannot edit/append anything to the pdf once it's closed. Any updates will overwrite the original pdf. If you have multiple plots, the suggestion would be to save them in a list/dict and then loop over it when creating the pdf. – impiyush Jun 14 '18 at 22:40
  • It is best to use the context manger here: `with PdfPages("graphs.pdf") as pdf: pdf.savefig(fig)` – Chris Aug 13 '19 at 17:24
  • This worked for me `for fig in range(1, plt.gcf().number + 1) pdf.savefig(fig) pdf.close()` – Nicolas Jul 10 '20 at 08:49
  • getting error AttributeError: 'LatexManager' object has no attribute 'latex' – marti_ Apr 13 '23 at 16:50
16

Do you mean save multiple figures into one file, or save multiple figures using one script?

Here's how you can save two different figures using one script.

>>> from matplotlib import pyplot as plt
>>> fig1 = plt.figure()
>>> plt.plot(range(10))
[<matplotlib.lines.Line2D object at 0x10261bd90>]
>>> fig2 = plt.figure()
>>> plt.plot(range(10,20))
[<matplotlib.lines.Line2D object at 0x10263b890>]
>>> fig1.savefig('fig1.png')
>>> fig2.savefig('fig2.png')

...which produces these two plots into their own ".png" files: enter image description here

enter image description here

To save them to the same file, use subplots:

>>> from matplotlib import pyplot as plt
>>> fig = plt.figure()
>>> axis1 = fig.add_subplot(211)
>>> axis1.plot(range(10))
>>> axis2 = fig.add_subplot(212)
>>> axis2.plot(range(10,20))
>>> fig.savefig('multipleplots.png')

The above script produces this single ".png" file: enter image description here

Brett Morris
  • 791
  • 1
  • 4
  • 13
  • 2
    I think that this should be the accepted answer for it really does what OP was asking for without merging two pdf files using external library. – Dilawar Jan 16 '14 at 08:27
  • 38
    This is not a good answer, because the OP is not asking how to plot multiple subplots in the same figure and them save it, nor is he asking how to save to separate files -- presumably that is obvious. The question is about saving separate figures to the same file. – sh37211 Nov 03 '18 at 10:40
0

I struggled with the same issue. I was trying to put 2,000 scatter plots into a single .pdf. I was able to start the procedure, but it aborted after a few hundred. Even when I created six scatter charts into one .pdf, the .pdf file was enormous (like 7mb) for just six .jpg's that were 30kb each. When I opened the .pdf, it appeared that the .pdf was painting every point on the chart (each chart had thousands of points) instead of displaying an image. Some day, I will figure out the correct options, but here is a quick and dirty work-around. I printed the scatter plots to individual .jpg files in a local directory.

  • this is a good general approach to saving figures, however it doesn't address the question asked! the author already knew how to save individual figures but was wondering how to save multiple figures in one document. – lunguini Jul 02 '21 at 02:29