python - 如何使用 matplotlib 从数据帧创建简单的动画图

标签 python numpy matplotlib

有人可以帮助我更正下面的代码,以使用动画 matplotlib 可视化这些数据吗?

X 轴和 Y 轴的数据集如下所述。

X- Range
    mydata.iloc[:,[4]].head(10)

       Min_pred
    0  1.699189
    1  0.439975
    2  2.989244
    3  2.892075
    4  2.221990
    5  3.456261
    6  2.909323
    7 -0.474667
    8 -1.629343
    9  2.283976

    Y - range
    dataset_meteo.iloc[:,[2]].head(10)
    Out[122]: 
       Min
    0  0.0
    1 -1.0
    2  2.0
    3 -2.0
    4 -4.0
    5 -4.0
    6 -5.0
    7 -7.0
    8 -3.0
    9 -1.0

我已经尝试过下面的代码,

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

d = pd.read_excel("mydata.xls")
x = np.array(d.index)
y = np.array(d.iloc[:,[2]])
mydata = pd.DataFrame(y,x)

fig = plt.figure(figsize=(10,6))
plt.xlim(1999, 2016)
plt.ylim(np.min(x), np.max(x))
plt.xlabel('Year',fontsize=20)
plt.ylabel(title,fontsize=20)
plt.title('Meteo Paris',fontsize=20)

def animate(i):
    data = mydata.iloc[:int(i+1)] #select data range
    p = sns.lineplot(x=data.index, y=data[title], data=data, color="r")
    p.tick_params(labelsize=17)
    plt.setp(p.lines,linewidth=7)
ani = matplotlib.animation.FuncAnimation(fig, animate, frames=17, repeat=True)

这个想法是创建一个图表,其中预测的 (Y) 将被动画化 类似于下面链接中的这个。 https://www.courspython.com/animation-matplotlib.html

谢谢您的帮助

最佳答案

这就是你想要得到的吗?

x = np.arange(1999,2017)
y = np.random.random(size=x.shape)

fig = plt.figure(figsize=(4,3))
plt.xlim(1999, 2016)
plt.ylim(np.min(y), np.max(y))
plt.xlabel('Year',fontsize=20)
plt.ylabel('Y',fontsize=20)
plt.title('Meteo Paris',fontsize=20)
plt.tick_params(labelsize=17)

line, = plt.plot([],[],'r-',lw=7)

def animate(i):
    x_, y_ = x[:i+1],y[:i+1]
    line.set_data(x_,y_)
    return line,

ani = matplotlib.animation.FuncAnimation(fig, animate, frames=len(x), repeat=True)

enter image description here

关于python - 如何使用 matplotlib 从数据帧创建简单的动画图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55637437/

相关文章:

python - 如何检查单元格中的值是否在范围内。基于此将不同的 'scores' 分配给新列

python - Flask instance init中的第一个参数怎么理解?

python - 如何过滤数组中与某个字典属于同一家族或层次结构的所有字典?

python - 在 NumPy 数组的每一行(按行)应用函数

python - 使用numpy为特定值的像素制作掩码数组

python - matplotlib:在堆叠散点图中对齐 y 轴标签

python - 如何在 Python Tkinter GUI 中嵌入 Cartopy?

python - 使用 python 中的 matplotlib 清楚地显示特定点的 x 轴值

python - 如何调整子图之间的不同边距

python - 使用多字节 key 对文件进行 XORing