首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >数据动画- matplotlib - python -未绘制线条

数据动画- matplotlib - python -未绘制线条
EN

Stack Overflow用户
提问于 2020-01-30 04:44:56
回答 1查看 56关注 0票数 0

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

代码语言:javascript
复制
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

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

代码语言:javascript
复制
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 (参见下面的代码),结果完全相同。

代码语言:javascript
复制
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()

你能帮帮我吗?

感谢您的时间和支持!

汤姆

EN

回答 1

Stack Overflow用户

发布于 2020-01-30 05:46:58

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

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

代码语言:javascript
复制
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)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59975291

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档