【问题标题】:Data animation - matplotlib - python - No line drawn数据动画 - matplotlib - python - 没有画线
【发布时间】:2020-01-29 20:44:56
【问题描述】:

我想用我的图表做一些动画,以显示 BTC 股票价格在指定时间段内的演变,基于以下数据:

id_BTC       Date_and_Time BTC_Value
0     3800 2020-01-26 22:08:44   8636.96
1     3801 2020-01-26 22:08:55   8636.96
2     3802 2020-01-26 22:09:05   8626.76
3     3803 2020-01-26 22:09:15   8637.24
4     3804 2020-01-26 22:09:27   8626.77
5     3805 2020-01-26 22:09:37   8637.24
6     3806 2020-01-26 22:09:45   8637.24
7     3807 2020-01-26 22:09:57   8634.99
8     3808 2020-01-26 22:10:09   8634.99
9     3809 2020-01-26 22:10:19   8634.15
10    3810 2020-01-26 22:10:28   8634.15
11    3811 2020-01-26 22:10:38   8635.00
12    3812 2020-01-26 22:10:49   8635.00
13    3813 2020-01-26 22:11:00   8635.00
14    3814 2020-01-26 22:11:08   8634.99
15    3815 2020-01-26 22:11:18   8625.11
16    3816 2020-01-26 22:11:29   8625.10
17    3817 2020-01-26 22:11:41   8634.99
18    3818 2020-01-26 22:11:51   8634.99
19    3819 2020-01-26 22:12:02   8625.10
20    3820 2020-01-26 22:12:12   8620.58
21    3821 2020-01-26 22:12:21   8633.80
22    3822 2020-01-26 22:12:31   8633.80
23    3823 2020-01-26 22:12:42   8633.80
24    3824 2020-01-26 22:12:52   8619.37
25    3825 2020-01-26 22:13:03   8619.37
26    3826 2020-01-26 22:13:13   8619.37
27    3827 2020-01-26 22:13:23   8631.41
28    3828 2020-01-26 22:13:35   8617.98
29    3829 2020-01-26 22:13:45   8617.98
30    3830 2020-01-26 22:13:56   8617.78
31    3831 2020-01-26 22:14:06   8611.47
32    3832 2020-01-26 22:14:17   8611.47
33    3833 2020-01-26 22:14:27   8611.47
34    3834 2020-01-26 22:14:36   8611.47
35    3835 2020-01-26 22:14:49   8618.88
36    3836 2020-01-26 22:14:57   8618.88
37    3837 2020-01-26 22:15:10   8614.13
38    3838 2020-01-26 22:15:19   8627.51

我正在使用以下代码,没有错误显示但图表上没有线条:

sql = "SELECT * FROM t_btc_2 WHERE id_btc >= 3800;"

mycursor.execute(sql)

table_rows = mycursor.fetchall()

df = pd.DataFrame(table_rows)

mycursor.close()

df.rename(columns={0: 'id_BTC', 1:'Date_and_Time', 2:'BTC_Value'}, inplace=True)

# DataFrame creation
rng = pd.date_range('2020-01-26 00:00:00', '2020-01-27 00:00:00', freq='1S')
df = pd.DataFrame({'id_BTC': df.iloc[:,0].astype(int),
                   'BTC_Value': df.iloc[:,2].astype(float),
                   'Date_and_Time': df.iloc[:,1]})

value_min=np.min(df.iloc[:,1])
value_max=np.max(df.iloc[:,1])

fig= plt.figure()
ax = plt.axes(xlim=(3800, 3838), ylim=(value_min, value_max))

line, = ax.plot([],[],linewidth=2)

def init():
    line.set_data([],[])
    return line,

def animate(i):
    axe_x=df['id_BTC'][i]
    axe_y=df['BTC_Value'][i]
    line.set_data(axe_x, axe_y)
    return line,

anime = animation.FuncAnimation(fig, animate, frames=39, interval=500)

plt.show()

另一方面,我也尝试为同一张图表制作动画,但使用日期而不是 id_BTC(参见下面的代码),结果完全相同。

sql = "SELECT * FROM t_btc_2 WHERE id_btc >= 3800;"

mycursor.execute(sql)

table_rows = mycursor.fetchall()

df = pd.DataFrame(table_rows)

mycursor.close()

df.rename(columns={0: 'id_BTC', 1:'Date_and_Time', 2:'BTC_Value'}, inplace=True)

# DataFrame creation
rng = pd.date_range('2020-01-26 00:00:00', '2020-01-27 00:00:00', freq='1S')
df = pd.DataFrame({'id_BTC': df.iloc[:,0].astype(int),
                   'BTC_Value': df.iloc[:,2].astype(float),
                   'Date_and_Time': pd.to_datetime(df.iloc[:,1])})

value_min=np.min(df.iloc[:,1])
value_max=np.max(df.iloc[:,1])

fig= plt.figure()
ax = plt.axes(xlim=('2020-01-26 00:00:00', '2020-01-27 00:00:00'), ylim=(value_min, value_max))
line, = ax.plot([],[],lw=2)

def init():
    line.set_data([],[])
    return line,

def animate(i):
    line.set_data(str(df['Date_and_Time'][i]), df['BTC_Value'][i])
    return line,

anime = animation.FuncAnimation(fig, animate, frames=39, interval=500)

plt.show()

你能帮帮我吗?

感谢您的时间和支持!

汤姆

【问题讨论】:

  • 尝试提出问题的minimal reproducible example,可以复制、粘贴和运行。
  • 会做(感谢您提供链接),但是,我不知道如何以不同的方式解释我的问题,因为我启动脚本时没有产生错误,但没有显示预期的结果.
  • 预期输出是什么?您似乎一次只绘制一个点,这是您想要的吗?或者您希望每一帧的线条在哪里加长?
  • 预期的输出是有一条线连接,在一段时间内连接点,以创建有关 BTC 价格演变的动画。

标签: python pandas dataframe date matplotlib


【解决方案1】:

您的问题是您一次只绘制一个点,并且由于您没有提供标记,因此该点仍然不可见。

如果你想要一条随时间扩展的线,你需要在每次迭代时添加新点到旧点,而不是覆盖旧点:

value_min=df['BTC_Value'].min()
value_max=df['BTC_Value'].max()

fig= plt.figure()
ax = plt.axes(xlim=(3800, 3838), ylim=(value_min, value_max))

line, = ax.plot([],[],linewidth=2)

def init():
    line.set_data([],[])
    return line,

def animate(i):
    new_x=df['id_BTC'][i]
    new_y=df['BTC_Value'][i]
    old_x, old_y = line.get_data()
    new_x = np.concatenate([old_x,[new_x]])
    new_y = np.concatenate([old_y,[new_y]])
    line.set_data(new_x, new_y)
    return line,

anime = animation.FuncAnimation(fig, animate, frames=39, interval=500)

【讨论】:

  • 嗨 Diziet,对于迟到的反馈很抱歉,但它运行良好,就像我想要的一样!非常感谢您的帮助和支持!
  • 不客气。如果您认为答案有帮助,请考虑通过单击左侧的绿色复选标记关闭主题来接受它
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-21
  • 2018-07-12
  • 2021-02-25
  • 2015-03-20
相关资源
最近更新 更多