[英]Funcanimation is not animating line Graph/time series from fixed excel file?
我正在嘗試使用 Python 和 excel 文件為時間序列設置動畫。 數據如下:
但是,它不會為固定圖形設置動畫,僅以 gif 格式存儲圖形:
import random
from itertools import count
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
plt.style.use('fivethirtyeight')
x_vals = []
y_vals = []
index = count()
def animate(i):
data = pd.read_excel(r'D:\new result\Results\SCA\all_inone_final.xlsx')
x = data['Date']
y1 = data['LAC']
y2 = data['GAC']
plt.cla()
plt.plot(x, y1, label='LAC')
plt.plot(x, y2, label='GAC')
plt.legend(loc='upper left')
plt.tight_layout()
ani = FuncAnimation(plt.gcf(), animate, interval=100000)
ani.save(r'D:\new result\Results\SCA\live_plots\_'+'_SCA_4.gif', writer='imagemagick')
plt.tight_layout()
plt.show()
任何推薦幫助? TIA
檢查此代碼:
# import section
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
from datetime import datetime, timedelta
plt.style.use('fivethirtyeight')
# dataframe creation section
N = 400
Date = [datetime.strptime('Jan 1 2002', '%b %d %Y') + timedelta(days = x) for x in range(N)]
x = np.linspace(0, N/10, N)
LAC = np.sin(3*x) * np.cos(6*x)
GAC = np.cos(x) * np.cos(5*x)
data = pd.DataFrame({'Date': Date,
'LAC': LAC,
'GAC': GAC})
# figure preparation section
fig, ax = plt.subplots(1, 1, figsize = (16, 8))
# animate function definition section
# note the use of i
velocity = 1
span = 7
def animate(i):
plt.cla()
x = data['Date'][velocity*i : span + velocity*i]
y1 = data['LAC'][velocity*i : span + velocity*i]
y2 = data['GAC'][velocity*i : span + velocity*i]
ax.plot(x, y1, label='LAC')
plt.plot(x, y2, label='GAC')
ax.legend(loc='upper left')
ax.set_ylim([-2, 2])
plt.tight_layout()
# animation function call
ani = FuncAnimation(fig, animate, interval = 1, frames = N-span)
plt.show()
這給出了這個結果:
由於我無權訪問您的數據源,因此在“數據框創建部分”中,我創建了一個 dataframe 以執行 animation,您必須將其替換為您的數據。
請注意在animate
function 中使用i
:我用它來切片x
、 y1
和y2
arrays 以便 plot 僅部分span
元素長。 隨着 animation 的進行, i
增加,因此 arrays 的切片沿它們流動,並且 plot 顯示y1
和y2
隨時間推移的趨勢。
我不知道這是否是您想要的,因為您沒有指定在 animation 進行時您希望看到的變化,但是我希望這段代碼可以幫助您了解如何創建一個。
編輯
這是您的數據的代碼:
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
plt.style.use('fivethirtyeight')
data = pd.read_excel('data_comparison.xlsx')
velocity = 1
span = 7
fig, ax = plt.subplots(1, 1, figsize = (16, 8))
def animate(i):
plt.cla()
x = data['Date'][velocity*i : span + velocity*i]
y1 = data['LAC'][velocity*i : span + velocity*i]
y2 = data['GAC'][velocity*i : span + velocity*i]
ax.plot(x, y1, label='LAC')
plt.plot(x, y2, label='GAC')
ax.legend(loc='upper left')
ax.set_ylim([0, 65])
plt.tight_layout()
ani = FuncAnimation(fig, animate, interval = 1, frames = len(data)-span)
plt.show()
這給出了這個 animation:
(我剪掉了 animation 因為它的大小太大了,超過 2 MB。但是你可以運行上面的代碼,你會得到相同的動畫)
編輯 2
固定x軸:
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
plt.style.use('fivethirtyeight')
data = pd.read_excel('data_comparison.xlsx')
velocity = 1
span = 1
fig, ax = plt.subplots(1, 1, figsize = (16, 8))
def animate(i):
plt.cla()
x = data['Date'][0 : span + velocity*i]
y1 = data['LAC'][0 : span + velocity*i]
y2 = data['GAC'][0 : span + velocity*i]
ax.plot(x, y1, label='LAC')
plt.plot(x, y2, label='GAC')
ax.legend(loc='upper left')
ax.set_xlim([data['Date'].iloc[0], data['Date'].iloc[-1]])
ax.set_ylim([0, 65])
plt.tight_layout()
ani = FuncAnimation(fig, animate, interval = 1, frames = len(data)-span)
ani.save('animation.gif', writer = 'imagemagick')
plt.show()
您可以使用[0: span + velocity*i]
對 arrays 進行切片:起點是固定的,而終點隨着 animation 的繼續而變化。 為了修復我使用這條線的軸:
ax.set_xlim([data['Date'].iloc[0], data['Date'].iloc[-1]])
ax.set_ylim([0, 65])
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.