简体   繁体   中英

How to show matplotlib plot from a figure object

My code contains the following lines:

from matplotlib.figure import Figure

figure = Figure(figsize=(10, 5), dpi=dpi)

How can I get matplotlib to show this figure? I also show it embedded in tkinter, which workes fine. However I would also be able to show it in the standard matplotlib window. But I can't for the life of me get it to work.

According to AttributeError while trying to load the pickled matplotlib figure , a simple workaround is:

fig = plt.Figure(...)
......
managed_fig = plt.figure(...)
canvas_manager = managed_fig.canvas.manager
canvas_manager.canvas.figure = fig
fig.set_canvas(canvas_manager.canvas)

Note that I encountered "'Figure' object has no attribute '_original_dpi'" in my environment. Not sure if it's some compatibility issue between my PyPlot and the PyQt5. Just did a hack:

fig._original_dpi = 60

to get around this. Not sure if there are any better solutions.

I usually use matplotlib's pyplot for immediate generation (or produce images in jupyter notebooks). This would look like the following:

import matplotlib.pyplot as plt

figure = plt.figure(figsize=(10, 5), dpi=dpi)
plt.show()

This shows the (blank) figure as desired.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM