2017-08-08 108 views
0

我有一個每列有2列的熊貓DataFrames的列表。到目前爲止,我有一個函數,當給定索引i時,它將採用與索引i對應的幀,並繪製第一列的數據與第二列的數據的圖形。迭代遍歷熊貓數據框列表的Matplotlib動畫

list = [f0,f1,f2,f3,f4,f5,f6,f7,f8,f9] 
    def getGraph(i): 
     frame = list[i] 
     frame.plot(x = "firstColumn",y = "secondColumn") 
     return 0 

我現在的問題是,如何使這疊代幀列表和動畫連續顯示每一個0.3秒的曲線圖。

最好,我想在動畫庫中使用FuncAnimation類,它爲您做了繁重的工作和優化。

回答

0

集動畫功能和init到軸,圖形和線條:

from matplotlib import pyplot as plt 
from matplotlib import animation 
import pandas as pd 

f0 = pd.DataFrame({'firstColumn': [1,2,3,4,5], 'secondColumn': [1,2,3,4,5]}) 
f1 = pd.DataFrame({'firstColumn': [5,4,3,2,1], 'secondColumn': [1,2,3,4,5]}) 
f2 = pd.DataFrame({'firstColumn': [5,4,3.5,2,1], 'secondColumn': [5,4,3,2,1]}) 

# make a global variable to store dataframes 
global mylist 
mylist=[f0,f1,f2] 

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

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

# animation function of dataframes' list 
def animate(i): 
    line.set_data(mylist[i]['firstColumn'], mylist[i]['secondColumn']) 
    return line, 

# call the animator, animate every 300 ms 
# set number of frames to the length of your list of dataframes 
anim = animation.FuncAnimation(fig, animate, frames=len(mylist), init_func=init, interval=300, blit=True) 

plt.show() 

欲瞭解更多信息查找教程:https://jakevdp.github.io/blog/2012/08/18/matplotlib-animation-tutorial/