Pandas绘制时间序列-出现奇怪的线

3
我正在处理汇率数据,但是图表中出现了不必要的线条,结果非常奇怪。
我已经阅读了不同的示例,并按照示例进行操作,但仍无法消除这些线条。
是否有人知道我的代码有什么问题?谢谢您的帮助。 enter image description here
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文件保存在Google Drive中 https://drive.google.com/file/d/1ANybvOKeUYIhXxtm97VNT88SI8z2OWjV/view?usp=sharing

4
似乎有一些条目未经排序。 - Sheldore
谢谢,你是个天才。 - Leigh Tsai
2个回答

4

使用您提供的相同数据

您需要添加 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()

输出:

在此输入图像描述


谢谢你!问题解决了。之前没有注意到这个模式,需要在绘图之前对数据进行排序。非常感谢你的努力和时间! - Leigh Tsai
1
很高兴能帮助你。 - Nihal

1

无法复现,请确保CSV文件相同:

df=pd.read_csv('a.csv', index_col=0, sep='\t')
df.index = pd.to_datetime(df.index)
df.BID.plot()

enter image description here

获取其他列的类似图。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接