我有一个数据框,它在非营业时间内丢弃数据,但在绘制数据框时,因为日期是索引,所以该图显示了前一天最后一次读取和第二天第一次读取之间的长连接线(见图)。我需要避免这种情况,只绘制工作时间。
我使用下面的简单代码
df.plot()
plt.show()
数据帧输出
date NIFTY 50 AARTIIND ... DIVISLAB GARFIBRES
...
2021-08-31 12:15:00+05:30 1.000000 1.000000 ... 1.000000 1.000000
2021-08-31 13:15:00+05:30 0.999627 0.996703 ... 1.002769 0.999557
2021-08-31 14:15:00+05:30 1.005706 0.996916 ... 1.005469 0.986966
2021-08-31 15:15:00+05:30 1.005078 0.997607 ... 1.004459 0.994337
2021-09-01 09:15:00+05:30 1.009123 1.003882 ... 1.006013 0.995697
2021-09-01 10:15:00+05:30 1.003989 0.990428 ... 1.005382 0.995413
2021-09-01 11:15:00+05:30 1.003241 0.993566 ... 1.021187 0.997517
2021-09-01 12:15:00+05:30 1.002904 0.986759 ... 1.018506 0.997184
如果您有这样一个数据框,索引上有日期,有些列有值:
您可以定义作业的开始和结束时间,并使用它们过滤数据帧;如果小时超出此界限,则将数据设置为
None
:完整代码
如果你想从this answer中汲取灵感,消除线之间的白色间隙,从而获得连续的线,你应该使用
range(df.index.size)
作为x轴进行绘图,然后你需要调整x刻度我找到了解决这个问题的办法。我已经更改了日期索引的格式,如图所示。这有助于我准确地得到图中数据框中的内容。谢谢你的帮助,如果有其他更好的方法,请建议
我修改过的代码
相关问题 更多 >
编程相关推荐