2014-04-23 99 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.