2014-04-23 101 views
4

我正在寻找一种方法来更新我的轮廓线,动画不需要我每次重新绘制图形。更新matplotlib动画的轮廓

对这个问题的最多回复我发现主张回忆ax.contour,但因为我的轮廓叠加在另一张图片上,这是难以忍受的缓慢。

我发现,接近前回答这个问题有死链接答案是唯一的回应:Animating a contour plot in matplotlib using FuncAnimation

示例代码:

#!/usr/bin/env python 

import matplotlib.pylab as plt 
import matplotlib.animation as anim 
from matplotlib.colors import LinearSegmentedColormap as lsc 
import numpy 

#fig = 0; ax = 0; im = 0; co = 0 


image_data = numpy.random.random((100,50,50)) 
contour_data = numpy.random.random((100,50,50)) 

def init(): 
    global fig, ax, im, co 
    fig = plt.figure() 
    ax = plt.axes() 
    im = ax.imshow(image_data[0,:,:]) 
    co = ax.contour(contour_data[0,:,:]) 

def func(n): 
    im.set_data(image_data[n,:,:]) 
    co.set_array(contour_data[n,:,:]) 

init() 
ani = anim.FuncAnimation(fig, func, frames=100) 
plt.show() 

干杯。

回答

0

也许你现在已经明白了这一点;不幸的是,看起来,你必须重宣告整个轮廓/ contourf集艺术家的和在每一个时间步删除旧的实例。下面是一些信息从this link复制:

的set_array()方法(我认为),只影响了colormapping 信息contourf,甚至再没有出现更新。 您需要做的是创建一个新的等值线图并删除旧的等值线,特别是如果您需要更改基础等值线数据。这个 应该像C.remove()一样简单,但由于某种原因,这不存在 存在(我会在一分钟内添加它)。因此,相反,你需要做的 如下:

import matplotlib.pyplot as plt 
import numpy as np 

x = np.arange(0, 2 * np.pi, 0.1) 
X,Y = np.meshgrid(x,x) 
f1 = np.sin(X) + np.sin(Y) 
f2 = np.cos(X) + np.cos(Y) 

plt.figure() 
C = plt.contourf(f1) 
plt.show() 
for coll in C.collections: 
    plt.gca().collections.remove(coll) 
C = plt.contourf(f2) 
plt.draw() 

This answer is probably what you're looking for.