首页 > 解决方案 > 如何绘制分散的时间戳

问题描述

我试图随着时间的推移绘制冠状病毒病例,但是当我运行我的代码时,所有时间戳都被挤压在一起。我希望将它们分开,以便您可以实际阅读索引并查看时间。
图形图像

这是我用来绘制图形的代码: washcases.plot.bar(rot=15, title="Cases per Day in Washtenaw County"),我的数据如下所示:

2020-03-01     0
2020-03-02     0
2020-03-03     0
2020-03-04     0
2020-03-05     3
2020-03-06     1
2020-03-07     5

谁能帮我用更易读的时间戳来绘制我的数据图?谢谢!

我看过的帖子:

带有时间戳的 Pandas 绘图图

pandas plot 聚合时间戳索引

标签: pythonpandasmatplotlib

解决方案


您可以减少 x 轴上显示的标签数量,如下所示:

import matplotlib.pyplot as plt

# your code for loading the data and plotting

# get the current axis
ax = plt.gca()
# set the number of labels (e.g. 10)
ax.xaxis.set_major_locator(plt.MaxNLocator(10)) 

plt.show()  

推荐阅读