【问题标题】:Plot a column of timestamps in a timeline axis along with counts在时间轴轴上绘制一列时间戳以及计数
【发布时间】:2022-09-27 19:05:04
【问题描述】:

我有一个带有单列的数据框:\'date\'。在某些情况下,此列有一堆时间戳(大约 500,000),其中一些是重复的。我想在 x 轴上绘制一个带有时间戳的图表,并在 y 轴上计算这些时间戳。

我尝试了一个简单的绘图命令,结果几乎完美:

df.value_counts(sort=False).plot(kind=\'bar\',figsize=(20,10))

如您所见,x 轴全部挤压在一起,这可能是因为时间戳是字符串格式,但我已经将它们转换为时间戳。是否可以在 x 轴上有一个时间刻度并绘制这些时间戳出现在 y 轴上的次数,在没有时间戳的时候有空格。

这是数据框的样子:

2017-07-06 12:00:43
2017-07-06 12:00:43
2017-07-06 12:00:53
2017-07-06 12:00:53
2017-07-06 12:00:53
                ...        
2017-07-06 20:03:43
2017-07-06 20:03:43
2017-07-06 20:03:43
2017-07-06 20:04:05
2017-07-06 20:04:05
Name: date, Length: 17105, dtype: datetime64[ns]

这就是我在上面运行value_counts() 时的样子:

2017-07-06 17:47:23    99
2017-07-06 16:54:54    93
2017-07-06 16:12:10    53
2017-07-06 19:42:33    48
2017-07-06 15:56:54    35
                       ..
2017-07-06 14:18:03     1
2017-07-06 17:31:33     1
2017-07-06 14:17:58     1
2017-07-06 14:17:20     1
2017-07-06 16:26:06     1
Name: date, Length: 6399, dtype: int64
  • 你真的想要价值倒计时到每秒的水平吗?

标签: python pandas dataframe matplotlib timestamp


【解决方案1】:

您可以使用matplotlib 进行绘图,并使用set_major_locator 设置刻度间隔,例如

fig, ax = plt.subplots()
time = pd.date_range(start='1/1/2018 12:00:00', freq='20S', periods=50)
count = np.random.randint(0, 5, 50)
ax.stem(time, count)
ax.xaxis.set_major_locator(mdates.SecondLocator(interval=200))
plt.show()

【讨论】:

    猜你喜欢
    • 2018-06-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多