【问题标题】:How create a simple animated graph with matplotlib from a dataframe如何从数据框中使用 matplotlib 创建一个简单的动画图
【发布时间】:2019-04-11 16:40:25
【问题描述】:

有人可以帮助我更正下面的代码以使用动画 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

如果你能帮忙,谢谢

【问题讨论】:

    标签: python numpy matplotlib


    【解决方案1】:

    这是你想要得到的吗?

    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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-02-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-25
      • 1970-01-01
      • 2011-01-12
      相关资源
      最近更新 更多