我想知道是否可以更新使用contour()
完成的轮廓,使用quiver()
完成的矢量字段和使用imshow()
完成的图像,而无需实际调用那些功能再次或创建一个新的图形,轴等。换句话说,是否有可能(通常是人们做的)更新图形的元素而不重新调用例程。
我尝试过基于set_array()
和pyplot.draw()
的解决方案,但我不能让它适用于矢量场和等高线图。
答案 0 :(得分: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)
如果是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()
据我所知,您无法以相同的方式更新contour
图。我不确定无论如何都会获得那么多,因为任何解决方案仍然需要重新计算轮廓线应该用于给定的数组输入。如果我是你,我只需致电ax.contour()
和fig.canvas.draw()
。