绘制没有空日期的matplotlib图表

2024-06-16 11:26:01 发布

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

这是我的第一个问题,所以stackoverflow将图片显示为链接。 我用股票价格画图表。我的datetime对象是:

一天的开始:

2019-07-17 10:00:00

2019-07-17 10:05:00

2019-07-17 10:10:00

2019-07-17 10:15:00

2019-07-17 18:20:00

2019-07-17 18:25:00

2019-07-17 18:30:00

2019-07-17 18:35:00

然后交易停止,没有更多的数据

我将这些datetime对象转换为matplotlib格式:

matplotlib.dates.date2num(dt_object)

然后我画图表:

fig, ax = plt.subplots()
ax.plot(x, y)
plt.show()

enter image description here

很难看。我的观点是因为它在x轴上有空格,没有数据。也许我错了

如果我使用

x = np.linspace(0, 100, len(y))

一切都很完美:

enter image description here

我怎样才能画出完美的图表

matplotlib.dates.date2num(dt_object)

Tags: 数据对象datetimeobjectmatplotlib链接图表dt