如何为matplotlib绘图“重置索引”?

2024-06-06 00:10:45 发布

您现在位置:Python中文网/ 问答频道 /正文

我有以下代码:

fig, ax = plt.subplots(1, 1)
calls["2016-12-24"].resample("1h").sum().plot(ax=ax)
calls["2016-12-25"].resample("1h").sum().plot(ax=ax)
calls["2016-12-26"].resample("1h").sum().plot(ax=ax)

生成以下图像:

enter image description here

我怎样才能使这两条线共用x轴呢?换言之,我如何让他们不改变生活?在


Tags: 代码图像plotfigpltaxresamplesum
1条回答
网友
1楼 · 发布于 2024-06-06 00:10:45

如果您不关心使用正确的日期时间作为索引,您可以按照您对所有系列的建议重置索引。如果这是你想要达到的目标,这将覆盖所有的时间序列。在

# the below should
calls["2016-12-24"].resample("1h").sum().reset_index("2016-12-24").plot(ax=ax)
calls["2016-12-25"].resample("1h").sum().reset_index("2016-12-25").plot(ax=ax)
calls["2016-12-26"].resample("1h").sum().reset_index("2016-12-26").plot(ax=ax)

否则,您应该尝试同时对这三列重新采样。尝试一下下面的内容,但不知道你最初的数据帧是什么样子,我不确定这是否适合你的情况。你应该发布一些关于输入数据帧的更多信息。在

^{pr2}$

相关问题 更多 >