首页 > 解决方案 > 熊猫情节时间序列-出现奇怪的线条

问题描述

我正在处理汇率数据,结果很奇怪,图中出现了不规则的线条。

我已经阅读了不同的样本,遵循了这些样本,但仍然无法摆脱这些行。

有谁知道我的代码有什么问题?谢谢您的帮助。

在此处输入图像描述

df = df[['PRICE', 'TIME']]
start_time = '2018-08-01 19:50:00'
end_time = '2018-08-01 20:10:00'

df = df[(df['TIME'] > start_time) & (df['TIME'] <= end_time)]
df = df.set_index('TIME')

plt.figure(figsize = (18,9))
plt.plot(pd.to_datetime(df.index),df["PRICE"])
plt.xlabel('Time',fontsize=18)
plt.ylabel('Mid Price',fontsize=18)

如果需要任何数据,我将 csv 文件保存在谷歌驱动器https://drive.google.com/file/d/1ANybvOKeUYIhXxtm97VNT88SI8z2OWjV/view?usp=sharing

标签: pythonpandasmatplotlib

解决方案


使用了您提供的相同数据

你需要添加df = df.sort_values(['TIME'], ascending=[True])

代码:

df = df[(df['TIME'] > start_time) & (df['TIME'] <= end_time)]
# df = df.drop_duplicates('TIME')
df = df.sort_values(['TIME'], ascending=[True])
df = df.set_index('TIME')

plt.figure(figsize=(18, 9))
plt.plot(pd.to_datetime(df.index), df["PRICE"])
plt.xlabel('Time', fontsize=18)
plt.ylabel('Mid Price', fontsize=18)

plt.show()

输出:

在此处输入图像描述


推荐阅读