3

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.

J..
  • 41
  • 1
  • 1
  • 5
  • Yes I have tried `figure.show()`, but this didn't do the trick. And how do you mean use pyplot? I think I need Figure to later use `FigureCanvasTkAgg(figure, master=self)` in tkinter. The examples [here](http://matplotlib.org/examples/user_interfaces/index.html) all use matplotlib.figure. – J.. Dec 05 '16 at 01:20
  • What is your running environment? Operation system? Command line or any IDE? – pyan Dec 05 '16 at 01:40
  • OS: macOS Sierra. I run it in the command line (terminal) with python 3. – J.. Dec 05 '16 at 01:46

2 Answers2

1

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.

Kai Yu
  • 118
  • 1
  • 8
-5

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.

davidlowryduda
  • 2,404
  • 1
  • 25
  • 29
  • Using `plt.figure` instead of `Figure` does seem to work. However when I plot I also get another popup containing a matplotlib figure with a colorbar as shown [here](http://i.imgur.com/IVgZX2h.png). And when I plot inside tkinter, then decide to plot with `plt.show()` it shows the plot 2 times, as well as the colorbar plot 2 times. (When I plot 2 times inside tkinter, then without it shows 3 times, etc.) – J.. Dec 05 '16 at 02:13
  • @J.. I do not know the details of what you are doing. I can only answer what you asked in the question. If you have a different question or use-case in mind, then you should ask another question containing exactly what you're after. – davidlowryduda Dec 05 '16 at 02:19
  • Then I'll try to figure it out. And if I can't I'll create another question. Thanks for your help! – J.. Dec 05 '16 at 02:28