How to update a plot in matplotlib?

后端 未结 7 1898
不思量自难忘°
不思量自难忘° 2020-11-22 02:11

I\'m having issues with redrawing the figure here. I allow the user to specify the units in the time scale (x-axis) and then I recalculate and call this function plots

7条回答
  •  生来不讨喜
    2020-11-22 02:54

    In case anyone comes across this article looking for what I was looking for, I found examples at

    How to visualize scalar 2D data with Matplotlib?

    and

    http://mri.brechmos.org/2009/07/automatically-update-a-figure-in-a-loop (on web.archive.org)

    then modified them to use imshow with an input stack of frames, instead of generating and using contours on the fly.


    Starting with a 3D array of images of shape (nBins, nBins, nBins), called frames.

    def animate_frames(frames):
        nBins   = frames.shape[0]
        frame   = frames[0]
        tempCS1 = plt.imshow(frame, cmap=plt.cm.gray)
        for k in range(nBins):
            frame   = frames[k]
            tempCS1 = plt.imshow(frame, cmap=plt.cm.gray)
            del tempCS1
            fig.canvas.draw()
            #time.sleep(1e-2) #unnecessary, but useful
            fig.clf()
    
    fig = plt.figure()
    ax  = fig.add_subplot(111)
    
    win = fig.canvas.manager.window
    fig.canvas.manager.window.after(100, animate_frames, frames)
    

    I also found a much simpler way to go about this whole process, albeit less robust:

    fig = plt.figure()
    
    for k in range(nBins):
        plt.clf()
        plt.imshow(frames[k],cmap=plt.cm.gray)
        fig.canvas.draw()
        time.sleep(1e-6) #unnecessary, but useful
    

    Note that both of these only seem to work with ipython --pylab=tk, a.k.a.backend = TkAgg

    Thank you for the help with everything.

提交回复
热议问题