Python Matplotlib hangs when asked to plot a second chart (after closing first chart window)

StackOverflow https://stackoverflow.com/questions/1219394

  •  10-07-2019
  •  | 
  •  

Question

Weird behaviour, I'm sure it's me screwing up, but I'd like to get to the bottom of what's happening:

I am running the following code to create a very simple graph window using matplotlib:

>>> import matplotlib.pyplot as plt
>>> fig = plt.figure()
>>> ax = fig.add_subplot(111)
>>> ax.plot((1, 3, 1))
[<matplotlib.lines.Line2D object at 0x0290B750>]
>>> plt.show()

and as expected I get the chart one would expect, in a new window that has popped up, containing a very simple blue line going from 1 to 3 back to 1 again on y axis, with 0, 1, 2 as the x axis points (just as example). Now I close the graph window (using cross button in the top right under windows). This gives me control to the interpreter, and I start again, creating new objects:

>>>
>>> fig1 = plt.figure()
>>> bx = fig1.add_subplot(111)
>>> bx.plot((1, 3, 1))
[<matplotlib.lines.Line2D object at 0x029E8210>]
>>> plt.show()

This time though, I get a window frame, with nothing in it (just the frame, no white background nothing), and the whole bang shoot hangs. I have to "end task", the python interpreter is terminated by the system and I get a command prompt back. Similar behaviour on a mac (except it does actually plot the graph first, before also hanging).

So somehow Python and/or matplotlib doesn't want me to close the window manually. Anybody know what's going on and what I should be doing? What I'd like to do is play around with different plots from within the interpreter, and obviously this behaviour doesn't help. I know I could use "Ipython -pylab" but in the interests of learning, I want to understand the above error.

Thanks.

Was it helpful?

Solution

Apparently, this is caused by a bug in the tkinter backend. See, e.g., https://bugs.launchpad.net/ubuntu/+source/matplotlib/+bug/313834 . It's being worked on... If you can regress to a slightly older tkinter library, that should be a workaround for the time-being (I ran into this same thing a couple of weeks ago, and that was my only hope).

OTHER TIPS

Three months late to the party, but I found a suggestion in the matlibplot documentation to use draw() rather than show(); the former apparently just does a render of the current plot, while the latter starts up all the interactive tools, which is where the problems seem to start.

It's not terribly prominently placed in the documentation, but here's the link: http://matplotlib.sourceforge.net/faq/howto_faq.html#use-show

For what it's worth, I've tried pylab.show() and had exactly the same issue you did, while pylab.draw() seems to work fine if I just want to see the output.

Have you tried to use ipython instead of the standard python interpreter?

You can install ipython with the following command:

easy_install ipython

and then, ipython has a specific mode to be ran with pylab, called -pylab:

ipython -pylab

In[1]: ...

I think that most of the people use this solution to plot graphs with python, it is a command line similar to the one of R/Matlab, completition, etc... and it runs a separated thread for every plot so it shouldn't have the problem you have described.

did you try:

plt.close()

to make sure you closed the plot object?

As posted somewhere above:

Use plt.draw() for all your plots except the last one.

For your last plot, use plt.show()

It's weird, but if you don't use plt.show() in the last one and try plt.draw() instead, you don't see any plots.

Good luck with this!

I had this problem when using TkAgg as the backend. After using plt.close('all') my computer froze.

The solution was to switch to a different backend. I now use Qt4Agg instead.

If you have Qt4Agg installed it is possible to switch backends by typing:

plt.switch_backend('Qt4Agg')

before plotting data

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top