Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
550 views
in Technique[技术] by (71.8m points)

python - update a figure made with imshow(), contour() and quiver()

I'd like to know whether one can update a contour done with contour(), a vector field done with quiver(), and an image done with imshow(), without actually having to call those functions again or creating a new figure, axes etc. In other words, is it possible (and is it usually what people do) to update those elements of a figure without re-calling the routines.

I've tried solutions based on set_array() and pyplot.draw() but I can't make it work for the vector field and contour plot.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Well, you can do this for imshow by calling .set_data() on the image, then fig.canvas.draw() on the figure. I don't see any real performance advantage over just calling draw() - both give me about 25FPS with the benchmark below (using WXAgg as a backend).

import numpy as np
import matplotlib.pyplot as pp
import time

def animate_data(data):

    fig,ax = pp.subplots(1,1)

    # I'm not convinced that animated=True does anything either...
    image = ax.imshow(data[0,:,:],animated=True)

    # pp.draw()
    fig.canvas.draw()

    start = time.time()
    tic = start
    for ii in xrange(1,data.shape[0]):
        if not(ii % 10):
            toc = time.time()
            print "FPS =%.6G" %(10./(toc-tic))
            tic = time.time()
        image.set_data(data[ii,:,:])

        # pp.draw()
        fig.canvas.draw()

    print "Average FPS =%.6G" %(data.shape[0]/(time.time()-start))

fakedata = np.random.randn(200,512,512)
animate_data(fakedata)

In the case of quiver, you can use .set_UVC() to update the plot:

fig,ax = subplots(1,1)

u1 = np.random.rand(10,10)
v1 = np.random.rand(10,10)
c1 = np.random.rand(10,10)

q = ax.quiver(u1,v1,c1)
fig.canvas.draw()

u2 = np.random.rand(10,10)
v2 = np.random.rand(10,10)
c2 = np.random.rand(10,10)

q.set_UVC(u2,v2,c2)
fig.canvas.draw()

As far as I can tell, you can't update contour plots in the same way. I'm not sure there would be all that much to gain anyway, since any solution would still require re-computing where the contour lines should go for a given array input. If I were you I would just call ax.contour() and fig.canvas.draw().


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...