更新用imshow(),contour()和quiver()创建的数字

MCF*_*MCF 10 python matplotlib

我想知道是否可以更新完成的轮廓,完成contour()的矢量场quiver()和完成的图像imshow(),而不必实际再次调用这些函数或创建新的图形,轴等.换句话说,是它可能(通常是人们做的事情)在不重新调用例程的情况下更新图形的元素.

我试过基于解决方案set_array()pyplot.draw(),但我不能让它的矢量场和等高线图工作.

ali*_*i_m 6

好吧,你可以imshow通过调用.set_data()图像然后fig.canvas.draw()在图上来做到这一点.我没有看到任何真正的性能优势而不仅仅是调用draw()- 两者都给我大约25FPS与下面的基准(WXAgg用作后端).

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 =\t%.6G" %(10./(toc-tic))
            tic = time.time()
        image.set_data(data[ii,:,:])

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

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

fakedata = np.random.randn(200,512,512)
animate_data(fakedata)
Run Code Online (Sandbox Code Playgroud)

在这种情况下quiver,您可以使用.set_UVC()更新图:

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()
Run Code Online (Sandbox Code Playgroud)

据我所知,你不能contour以同样的方式更新情节.我不确定无论如何都会获得太大的收益,因为任何解决方案仍然需要重新计算轮廓线应该用于给定的阵列输入.如果我是你,我只想叫ax.contour()fig.canvas.draw().