2015-09-27 22 views
2

嗨即時試圖使從數據顫抖情節的動畫在我的數據幀動畫箭袋情節熊貓數據幀

我有存儲這樣的熊貓數據幀的數據,有點像這個

QuivXLoc QuivYLoc QuivXVal QuivYVal QuivColorVal QuivPlotNum 
0 -70.22  -127.241  1.624  -0.879  1.846623  1 
1 -61.74  -127.241  -0.973  -0.027  0.973375  1 
2 -65.98  -121.835  0.046  2.416  2.416438  1 
3 -74.46  -121.835  -0.151  2.673  2.677262  1 
4 -78.70  -116.429  1.073  -0.954  1.435773  2 

我目前正在這樣繪製它,並且它完美地爲每個序列號生成單獨的圖。

for seq in quidf['QuivPlotNum'].unique(): 
    temp=quidf[quidf['QuivPlotNum']==seq] ## make subset to plot 
    plt.quiver(temp['QuivXLoc'], temp['QuivYLoc'], temp['QuivXVal'], temp['QuivYVal'],  # data 
      temp['QuivColorVal'],     # colour the arrows based on this array 
      cmap=cm.jet,  # colour map 
      headlength=3)  # length of the arrows 

Theres一些額外的代碼來格式化我忽略的情節。

我想要做的是根據迭代我的數據框中的序號對動畫序列進行動畫處理。我所看到的所有針對Quiver Animation的例子都涉及通過增加一些標量來縮放前一個函數。類似顫動的動畫

比如我想生成,我都試過,但無法弄清楚如何改變update_quiver爲我的應用程序工作: Plotting animated quivers in Python

+0

你的意思是將動畫箭頭箭頭增長到最終長度?你有什麼嘗試? – barny

+0

是的,在這個例子中,動畫顯示顫抖情節是按照幀改變的。但它是基於標量更新的。我想根據存儲在DataFrame中的數據進行繪圖.http://stackoverflow.com/questions/19329039/plotting-animated-quivers-in-python –

回答

4

使用matplotlib.animation模塊及其FuncAnimation類:

import matplotlib.pyplot as plt 
import numpy as np 
from matplotlib.animation import FuncAnimation 
import pandas as pd 

# read in the date and group it by the frame number 
data = pd.read_csv('data2.csv', index_col=0) 
grouped = data.groupby('QuivPlotNum') 

# set up the figure 
fig = plt.figure() 
ax = fig.add_subplot(1, 1, 1) 
ax.set_xlim(-200, 200) 
ax.set_ylim(-200, 200) 

# create empty plot for the update function to manipulate 
plot = ax.quiver([], [], [], [], [], cmap='jet', headlength=3) 

# create an iterator over the group, next() will return a tuple 
# of QuivPlotNum, DataFrame 
iterator = iter(grouped) 

def update(i): 
    # get next thing in the iterator 
    key, data = next(iterator) 
    # set new x, y coordinates for the plot 
    plot.set_offsets(np.column_stack([data.QuivXLoc, data.QuivYLoc])) 
    # update vector and color values 
    plot.set_UVC(data.QuivXVal, data.QuivYVal, data.QuivColorVal) 

# create the animation, update every 1000 ms 
ani = FuncAnimation(fig, update, interval=1000) 

# show it 
plt.show()