views:

1286

answers:

6

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.

A: 

did you try:

plt.close()

to make sure you closed the plot object?

Casey
I have now Casey, but no change. When I call plt.show(), it shows the graph window by the Python interpreter goes out of use, ie I don't have a prompt. Only when I close the window do I get the prompt back. If I then call plt.close() and then carry on, it behaves in exactly the same way, ie hangs. Getting the feeling (digging around Matplotlib website) that they basically want you to stick to iPython in -pylab mode.
Thomas Browne
try download scipy, if you don't have it already and import pylab. it works just like matplotlib.
Casey
Nope - nothing either. If import pylab, and do all the above, behaves in exactly the same way.
Thomas Browne
+2  A: 

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.

dalloliogm
Thanks - yes I know the Ipython route (see above comments). Indeed it looks like this is the way to go, even though it too can be a little unpredictable. I think you are right though. Basicall Ipython is the way forward.
Thomas Browne
+1  A: 

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).

co
+2  A: 

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%5Ffaq.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.

Rich
Thanks Rich - useful. Three months is not too late as I am only now getting back into matplotlib after a bit of a hiatus, actually.
Thomas Browne
A: 

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!

anonymus
A: 

I'm having the same problem with matplotlib locking up on the second plot, but I'm not using show() at all... Again, it runs fine the first time, but on subsequent attempts (with changed data) it locks up. The last message to print is the '#a' from right before plot() so I suspect that's the culprit. Any ideas on how to fix it?

import matplotlib.pyplot as plt
self.x = [u[0] for u in usage_cache]
self.y = [u[1] for u in usage_cache]
print '#a'
plt.plot(self.x, self.y, '-k')
print '#b'
plt.xlabel("Time")
plt.ylabel("Stations Used")
print '#c'
plt.fill_between(self.x,self.y,facecolor='blue', alpha=0.5)
plt.savefig("temp.png", format='png')
Jack Kracny