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

数据动画-matplotlib-python-未绘制线,python,pandas,dataframe,date,matplotlib,Python,Pandas,Dataframe,Date,Matplotlib,我想用我的图表做一些动画,根据以下数据显示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 380

我想用我的图表做一些动画,根据以下数据显示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()
你能帮我一下吗

感谢您的时间和支持


Tom

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

如果希望直线随时间扩展,则需要在每次迭代时将新点添加到旧点,而不是覆盖旧点:

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)

尝试提出一个问题的解决方案,可以复制、粘贴和运行。可以(感谢您的链接),但是,我不知道如何以不同的方式解释我的问题,因为启动脚本时没有生成错误,但预期的结果没有显示。预期的输出是什么?你似乎一次只画一个点,这就是你想要的吗?或者,您希望每一帧的线条在哪里加长?预期的输出是在一段时间内有一条线条连接,连接点,以创建一个关于BTC价格演变的动画。嗨,Diziet,很抱歉反馈太晚,但它工作得很好,就像我想要的!非常感谢您的帮助和支持!不客气。如果你发现答案是有用的,请考虑点击左边的绿色复选标记来关闭主题。