Matplotlib pyplot show() doesn't work once closed

前端 未结 4 812
迷失自我
迷失自我 2020-12-31 20:06

I have a loop like this

#!/usr/bin/env python
import matplotlib.pyplot as p

for i in xrange(N):
    # Create my_image here

    # Display this image
    p.fi         


        
相关标签:
4条回答
  • 2020-12-31 20:34

    It might be from a bug in previous versions of matplotlib. I was having a similar problem when I issued sequential show() commands -- only the first would show (and stay); but, when I updated matplotlib to 1.0.1 the problem went away.

    0 讨论(0)
  • 2020-12-31 20:38

    There might be a better way to animate imshow's, but this should work in a pinch. It's a lightly modified version of an animation example from the docs.

    # For detailed comments on animation and the techniqes used here, see
    # the wiki entry http://www.scipy.org/Cookbook/Matplotlib/Animations
    
    import matplotlib
    matplotlib.use('TkAgg')
    
    import matplotlib.pyplot as plt
    import matplotlib.mlab as mlab
    import matplotlib.cm as cm
    
    import sys
    import numpy as np
    import time
    
    ax = plt.subplot(111)
    canvas = ax.figure.canvas
    
    delta=0.025
    x=y= np.arange(-3.0, 3.0, delta)
    x,y=np.meshgrid(x, y)
    z1=mlab.bivariate_normal(x, y, 1.0, 1.0, 0.0, 0.0)
    z2=mlab.bivariate_normal(x, y, 1.5, 0.5, 1, 1)
    z=z2-z1  # difference of Gaussians
    
    def run(z):
        fig=plt.gcf()
        for i in range(10):
            plt.imshow(z, interpolation='bilinear', cmap=cm.gray,
                      origin='lower', extent=[-3,3,-3,3])
            canvas.draw()
            plt.clf()
            z**=2
    
    manager = plt.get_current_fig_manager()
    manager.window.after(100, run, z)
    plt.show()
    
    0 讨论(0)
  • 2020-12-31 20:47

    I have long been looking into this problem and I may have a solution although I haven't thoroughly tested it yet.

    The key is in writing code more like MatLab, name your figures and then call them to show().

    eg.

     from matplotlib import pyplot as plt
    
     fig1 = plt.figure()
     fig2 = plt.figure()
    
     fig1.show()
     fig2.show()
    

    This might work for animations and plotting at different stages

    0 讨论(0)
  • 2020-12-31 20:54

    After tinkering around with unutbu's example, I found a behavior that I could normally and debug with PyDev where I could progressively see the plots.

    import time, threading
    import numpy
    from matplotlib.pyplot import *
    
    x = numpy.linspace(0, 10)
    y = x**2
    
    def main():
        plot(x, x)
        draw()
        time.sleep(2)
        plot(x, y)
        draw()
    
    thread = threading.Thread()
    thread.run = main
    
    manager = get_current_fig_manager()
    manager.window.after(100, thread.start)
    figure(1)
    show()
    
    0 讨论(0)
提交回复
热议问题