Funcanimation是否不从固定的Excel文件对线图/时间序列进行动画处理?

时间:2020-06-08 14:54:28

标签: python matplotlib animation graph time-series

我正在尝试使用带有Excel文件的Python动画时间序列。数据如下:

enter image description here

但是,它不对固定图进行动画处理,仅以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

1 个答案:

答案 0 :(得分:1)

检查此代码:

query = User.where("users.field_1 > ?", 18.months.ago)
query.where("users.field_2 > users.field_1 - :time", time: 18.months.ago)

给出以下结果:

enter image description here

由于我无权访问您的数据源,因此在“数据框创建部分”中,我创建了一个数据框以执行动画,因此您必须将其替换为数据。
注意# 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() 函数中i的使用:我用它来切片animatexy1数组,以便仅绘制其中的一部分。 y2个元素长。随着动画的进行,span不断增加,因此数组的切片沿它们流动,并且该图显示了iy1随时间的趋势。
我不知道这是否是您想要的,因为您没有指定要在动画进行时看到的变化,但是我希望这段代码可以帮助您了解如何创建动画。


编辑

此代码与您的数据:

y2

给出以下动画:

enter image description here

(我剪切动画是因为它的大小太大,超过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 = 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()

enter image description here

您可以使用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]