2017-07-06 201 views
0

我試圖在matplotlib中創建幹區陰謀,並且找不到必要的文檔來幫助我。我有一系列的數據文件,每個看起來像這樣:在matplotlib中激活幹區陰謀

1 0.345346 
2 0.124325 
3 0.534585 

我想繪製每個文件作爲一個單獨的框架。

根據thisthis other教程中,我要創造出更新包含在每個情節對象中的數據的函數(藝術家嗎?我不知道有關的術語)

從第二個環節,這是更新函數

def update(frame): 
global P, C, S 

# Every ring is made more transparent 
C[:,3] = np.maximum(0, C[:,3] - 1.0/n) 

# Each ring is made larger 
S += (size_max - size_min)/n 

# Reset ring specific ring (relative to frame number) 
i = frame % 50 
P[i] = np.random.uniform(0,1,2) 
S[i] = size_min 
C[i,3] = 1 

# Update scatter object 
scat.set_edgecolors(C) 
scat.set_sizes(S) 
scat.set_offsets(P) 

# Return the modified object 
return scat, 

我該如何調整這種類型的更新函數以用於乾圖?該documentationstem是可怕的短暫(其實這是我學習matplotlib一個反覆出現的問題),但example code顯示,stem輸出是一個元組markerline, stemlines, baseline而不是一個藝術家對象像plt.plotplt.imshow

因此,當我爲動畫編寫我的update函數時,如何更新干圖中的數據?

回答

1

在這裏,你去!

import matplotlib.pyplot as plt 
from matplotlib.animation import FuncAnimation 

import numpy as np 

fig, ax = plt.subplots() 
x = np.linspace(0.1, 2*np.pi, 10) 
markerline, stemlines, baseline = ax.stem(x, np.cos(x), '-.') 

def update(i): 
    ax.cla() 
    markerline, stemlines, baseline = ax.stem(x, np.cos(x+i/10), '-.') 
    ax.set_ylim((-1, 1)) 

anim = FuncAnimation(fig, update, frames=range(10, 110, 10), interval=500) 
anim.save('so.gif', dpi=80, writer='imagemagick') 

我認爲可以有更好的方法來實現這一點 - 不需要每次都清除繪圖。但是,這工作!

enter image description here

+0

好了,所以你建議清除情節,並在每一幀重繪。這將工作,謝謝!我發現很難學習matplotlib,因爲文檔很少。我通常可以將這些例子拼湊在一起,但我並不完全瞭解所有的機制。例如,軸和藝術家之間的差異等。 您是否知道解釋matplotlib中的邏輯和結構的資源? –