why does no picture show

2024/11/16 4:54:29
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

Answer

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_pdffig1= 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_pdfdef plot(data):fig= plt.figure()ax = fig.add_subplot(111)ax.plot(data)return figfig1 = 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.

https://en.xdnf.cn/q/119233.html

Related Q&A

How Normalize Data Mining Min Max from Mysql in Python

This is example of my data in mysql, I use lib flashext.mysql and python 3RT NK NB SU SK P TNI IK IB TARGET 84876 902 1192 2098 3623 169 39 133 1063 94095 79194 …

complex json file to csv in python

I need to convert a complex json file to csv using python, I tried a lot of codes without success, I came here for help,I updated the question, the JSON file is about a million,I need to convert them t…

python pygame - how to create a drag and drop with multiple images?

So Ive been trying to create a jigsaw puzzle using pygame in python.The only problem is that Im having trouble creating the board with multiple images that i can drag along the screen (no need to conne…

Efficiently append an element to each of the lists in a large numpy array

I have a really large numpy of array of lists, and I want to append an element to each of the arrays. I want to avoid using a loop for the sake of performance. The following syntax is not working. a=np…

How to traverse a high-order range in Python? [duplicate]

This question already has answers here:Equivalent Nested Loop Structure with Itertools(2 answers)Closed 4 years ago.In python, we can use range(x) to traverse from 0 to x-1. But what if I want to trave…

How to send eth_requestAccounts to Metamask in PyScript?

I am trying to get address from installed MetaMask on the browser. We used to do this in JS as follow:const T1 = async () => {let Address = await window.ethereum.request({method: "eth_requestAc…

Extract strings that start with ${ and end with }

Im trying to extract the strings from a file that start with ${ and ends with } using Python. I am using the code below to do so, but I dont get the expected result.My input file looks like this:Click …

Weibull distribution and the data in the same figure (with numpy and scipy) [closed]

Closed. This question needs debugging details. It is not currently accepting answers.Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to repro…

python: use agg with more than one customized function

I have a data frame like this.mydf = pd.DataFrame({a:[1,1,3,3],b:[np.nan,2,3,6],c:[1,3,3,9]})a b c 0 1 NaN 1 1 1 2.0 3 2 3 3.0 3 3 3 6.0 9I would like to have a resulting dataframe like…

sending multiple images using socket python get sent as one to client

I am capturing screenshots from the server, then sending it to the client, but the images get all sent as one big file to the client that keeps expanding in size. This only happens when i send from one…