-1
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg
if __name__ == "__main__":
    fig1 = ...
    print("start plotting")
    canvas = FigureCanvasQTAgg(fig1)
    canvas.draw()
    canvas.show()

I have written to function which returns a matplotlib.figure object. I have run the above script. It has crashed Python. How do I do this?

The reasons I have worked with FigureCanvasQTAgg and matplotlib.figure instead of working with matplotlib.pyplot is that the Figure object also allow me to do something like

with PdfPages(...) as pdf_writer:
    canvas = FigureCanvasPDF(fig1)
    pdf_writer.savefig(fig1)
    pdf_writer.savefig(fig1)

which writes out two copies of the same figure in a single pdf file. Also it would allow me to write write multiple figures into the same PDF. I am unaware of any way we can do this using only matplotlib.pyplot

Lost1
  • 990
  • 1
  • 14
  • 34

1 Answers1

2

The easiest and best way to show a figure in matplotlib is to use the pyplot interface:

import matplotlib.pyplot as plt
fig1= plt.figure()
plt.show()

For creating the output in the form of a pdf file, you'd use:

import matplotlib.pyplot as plt
fig1= plt.figure()
plt.savefig("filename.pdf")

If you want to save multiple figures to the same pdf file, use matplotlib.backends.backend_pdf.PdfPages("output.pdf")

import matplotlib.pyplot as plt
import matplotlib.backends.backend_pdf

fig1= plt.figure()
ax=fig1.add_subplot(111)

outfile = "output.pdf"
with matplotlib.backends.backend_pdf.PdfPages(outfile) as pdf_writer:
    pdf_writer.savefig(fig1)
    pdf_writer.savefig(fig1)

A complete example of how to save multiple figures created from the same function would be

import matplotlib.pyplot as plt
import matplotlib.backends.backend_pdf

def plot(data):
    fig= plt.figure()
    ax = fig.add_subplot(111)
    ax.plot(data)
    return fig

fig1 = plot([1,2,3])
fig2 = plot([9,8,7])

outfile = "output.pdf"
with matplotlib.backends.backend_pdf.PdfPages(outfile) as pdf_writer:
    pdf_writer.savefig(fig1)
    pdf_writer.savefig(fig2)

FigureCanvasQTAgg is meant to be used in a PyQt GUI, which you will need to create first, if you want to use that. This question shows you how to do that but it seems a bit of an overkill for just showing or saving the figure.

Community
  • 1
  • 1
ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
  • there are reasons why this is not used, because when the code is not run as a script, we would like this to be written out as a PDF file. It seem to make much more sense to work with Figure object. – Lost1 Jan 20 '17 at 18:23
  • This is completely unrelated. I edited the answer on how to create pdf output. Opening a PyQt GUI just for creating a pdf file is even more absurd. Of course you can create the figure in any way you like, but using `FigureCanvasQTAgg` **only** makes sense if you want to use a PyQt GUI. – ImportanceOfBeingErnest Jan 20 '17 at 18:40
  • but using plt does not allow to save multiple multipled figures into the save PDF. The whole point was that we produce multiple plots and put them all in the same PDF using PdfPages – Lost1 Jan 20 '17 at 18:48
  • 1
    Can I ask you something: In how far does the question reflect what you **really** want to do? In the question you want to show a figure, now it turns out, you want to save it. In the question you have one figure, now it turns out you want to have multiple figures. What else do you want to do which you did not mention in the question? Please edit the question to reflect what you really want to do! But I can assure you, you will not need `FigureCanvasQTAgg` so leave that out completely. – ImportanceOfBeingErnest Jan 20 '17 at 18:54
  • Well, i want to get FigureCanvasQTAgg to work, that is what I would like. I actually got it working, but I deleted a line of import which was unused, then it broke again. – Lost1 Jan 20 '17 at 19:02
  • As I said, there is no reason at all to use `FigureCanvasQTAgg`. Even if you "get it to work" (whatever that would mean), it would not help you saving figures to pdf. I edited the answer yet again to show how to save multiple figures to the same pdf. – ImportanceOfBeingErnest Jan 20 '17 at 19:14
  • i am sorry,i am really not explaining this well. i have fig1,fig2... produced by the same function for different inputs. in another function,i write these into a pdf file. As far I am aware,i can only return fig1 fig2 as instances of Figure. Moreover,i think we might want to pickle fig1 and fig2. i do not think pyplot would do it – Lost1 Jan 20 '17 at 20:23
  • Yet again see edited answer where a function returns the figures (those **are** instances of `matplotlib.figure.Figure`). What makes you think that figures cannot be pickled? Where did you get that information? See, e.g. [this question about matplotlib pickling](http://stackoverflow.com/questions/4348733/saving-interactive-matplotlib-figures). – ImportanceOfBeingErnest Jan 20 '17 at 21:23
  • I really appreciate your patience and help. We are getting close to what I am trying to get at. In another script/function if I call fig1 = plot([1,2,3]), then plot.show() does not work. It turns up with the message: fig1.show() C:\Program Files\Anaconda3\lib\site-packages\matplotlib\figure.py:397: UserWarning: matplotlib is currently using a non-GUI backend, so cannot show the figure "matplotlib is currently using a non-GUI backend, – Lost1 Jan 21 '17 at 20:23
  • So now that we found out that you do not want to show the figure, you suddenly decide to show it? I don't know where you placed that `show` command, so you need to post the exact code you are running with the error you get. – ImportanceOfBeingErnest Jan 21 '17 at 23:32