Skip to content Skip to sidebar Skip to footer

Matplotlib Show() Doesn't Work Twice

I have a strange problem, with matplotlib. If I run this program, I'm able to open and close several time the same figure. import numpy from pylab import figure, show X = numpy.r

Solution 1:

At the start of your code, enable interactive mode via plt.ion()


Solution 2:

I have new information about this that a google search turned up

This is from the writer of matplotlib. This came from http://old.nabble.com/calling-show%28%29-twice-in-a-row-td24276907.html

Hi Ondrej,

I'm not sure where to find a good explanation of that, but let me give you some hints. It is intended to use show only once per program. Namely 'show' should be the last line in your script. If you want interactive plotting you may consider interactive mode (pyplot.ion-ioff) like in the example below.

Furthermore for dynamic plotting all animation demos might be useful.

Maybe you want to have also a look at http://matplotlib.sourceforge.net/users/shell.html .

best regards Matthias

So it seems it is an undocumented "feature" (bug?).

Edit: here is his code block:

from pylab import *

t = linspace(0.0, pi, 100)
x = cos(t)
y = sin(t)

ion()  # turn on interactive mode
figure(0)
subplot(111, autoscale_on=False, xlim=(-1.2, 1.2), ylim=(-.2, 1.2))

point = plot([x[0]], [y[0]], marker='o', mfc='r', ms=3)

for j in arange(len(t)):
    # reset x/y-data of point
    setp(point[0], data=(x[j], y[j]))
    draw() # redraw current figure

ioff() # turn off interactive mode
show()

So maybe by using draw() you can get what you want. I haven't tested this code, I'd like to know its behavior.


Solution 3:

I had the same issue with show() only working the first time. Are you still on version 0.99.3 or thereabouts? I was able to resolve my problem recently, if you're still interested in changing the behaviour of show(), try this:


I noticed this paragraph titled multiple calls to show supported on the what's new part of the matplotlib download site.

A long standing request is to support multiple calls to show(). This has been difficult because it is hard to get consistent behavior across operating systems, user interface toolkits and versions. Eric Firing has done a lot of work on rationalizing show across backends, with the desired behavior to make show raise all newly created figures and block execution until they are closed. Repeated calls to show should raise newly created figures since the last call. Eric has done a lot of testing on the user interface toolkits and versions and platforms he has access to, but it is not possible to test them all, so please report problems to the mailing list and bug tracker.

This was 'what's new' for version 1.0.1, at time of writing the version in synaptic was still on 0.99.3. I was able to download and build from source v1.0.1. The additional packages I also required to satisfy dependencies were libfreetype6-dev tk-dev tk8.5-dev tcl8.5-dev python-gtk2-dev; your mileage may vary.

Now that i have matplotlib.__version__ == 1.0.1 , the following code works how I would expect:

from matplotlib import pyplot as p
from scipy import eye
p.imshow(eye(3))
p.show()
print 'a' 
p.imshow(eye(6))
p.show()
print 'b' 
p.imshow(eye(9))
p.show()
print 'c' 

Solution 4:

 def onpick(self,event):
        print "click"
        print 'you pressed', event.canvas
        ...
        ax.plot(a)    
        fig.show() # <--- this blocks the entire loop

Try:

 def onpick(self,event):
        print "click"
        print 'you pressed', event.canvas
        ...
        ax.plot(a)    
        self.draw()
        self.update()

Solution 5:

My workaround to this problem is to never call close.

I'm pretty sure you can control the transparency of a widget in PyQt. You might try controlling the visibility using Qt instead of matplotlib. I'm sure someone else who knows more about matplotlib can give a better answer than that though :D


Post a Comment for "Matplotlib Show() Doesn't Work Twice"