问题描述
我想根据以下数据对图表进行动画处理,以显示指定时间段内BTC股票价格的变化:
I would like to do some animation with my chart to show the evolution of the BTC stock price over a specified period of time, based on the following data:
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
我正在使用以下代码,未显示错误,但上没有行图表:
I am using the following code, no error shows up but there is no line on the chart:
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(请参见下面的代码),并且结果完全相同。
On the other hand, I also tried to animate the same chart, but with dates instead of id_BTC (see code below), and exactly the same result.
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()
请问您能帮帮我吗?
感谢您的时间和支持!
汤姆
推荐答案
您的问题是您一次只能绘制一个点,而您没有绘制提供标记,该点仍然不可见。
Your problem is that you are only plotting one point at a time, and since you did not provide a marker, the point remains invisible.
如果要使线随着时间的推移而扩展,则需要添加新点每次迭代都返回到旧点,而不是覆盖旧点:
If you want to have a line that expands over time, you need to add the new point to the old points at each iteration, and not overwrite the old points:
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)
这篇关于数据动画-Matplotlib-Python-未绘制线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!