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.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…