2013-04-21 92 views
7

我有一個項目來創建幾個圖動畫。我將有一個有向加權圖,在每一步中,我都會改變這一點。我想用動畫進行這些更改。所以,我的問題是這樣的:Python動畫圖

是否有可能使用python製作動畫,如果是這樣,你將如何製作一個簡單的動畫?

回答

11

Matplotlib是python的標準圖形庫,它附帶了一個相當不錯的動畫包。傑克範德普拉斯有一個很好的教程,使用這個here

從這個鏈接服用,如果你想動畫一個正弦波,你會用下面的方法:

import numpy as np 
from matplotlib import pyplot as plt 
from matplotlib import animation 

# First set up the figure, the axis, and the plot element we want to animate 
fig = plt.figure() 
ax = plt.axes(xlim=(0, 2), ylim=(-2, 2)) 
line, = ax.plot([], [], lw=2) 

# initialization function: plot the background of each frame 
def init(): 
    line.set_data([], []) 
    return line, 

# animation function. This is called sequentially 
def animate(i): 
    x = np.linspace(0, 2, 1000) 
    y = np.sin(2 * np.pi * (x - 0.01 * i)) 
    line.set_data(x, y) 
    return line, 

# call the animator. blit=True means only re-draw the parts that have changed. 
anim = animation.FuncAnimation(fig, animate, init_func=init, 
      frames=200, interval=20, blit=True) 

plt.show() 

動畫庫調用函數「動畫」每間隔一次(在指定爲20這個例子)。該函數應該適當地更新繪圖。在這種情況下,它使用set_data方法更新「line」,它是正弦波數據的數組。

+0

謝謝。這是我想要的! – Tasos 2013-04-21 16:18:01