绘制数据帧时,X轴不显示[重复]

问题描述 投票:1回答:2

这个问题在这里已有答案:

嗨我有一个数据框,日期作为索引作为共享作为列我想要使用日期作为x轴绘制特定共享。我要绘制的系列是:df ['APPL'] =

Date
2018-10-29    24.903347
2018-10-30    25.165384
2018-10-31    25.087744
2018-11-01    24.777180
...
2018-12-06    25.709999

但是当我用df['APPL'].plot(use_index=True绘制它时,xasix没有显示出来。然后我尝试了plt.plot(df.index,df['APPL']),x轴的间隔太小,无法读取.enter image description here。例如,如何增加每10天显示一次的间隔?

python matlab dataframe
2个回答
1
投票

你可以旋转:

plt.xticks(rotation=90)

演示:

plt.plot(df.index,df['APPL'])
plt.xticks(rotation=90)

旋转后,文本将旋转90度。

如果您愿意:

plt.plot(df.index,df['APPL'])
plt.xticks(rotation=45)

测试你喜欢哪个。


1
投票

找到解决方案,此代码将间隔更改为3天。感谢@ U9-Forward和@wwii指出我的索引不是日期时间格式。

df.index=pandas.to_datetime(df.index)
ax = df['APPL'].plot()
# set monthly locator
ax.xaxis.set_major_locator(mdates.DayLocator(interval=3))
# set formatter
ax.xaxis.set_major_formatter(mdates.DateFormatter('%d-%m-%Y'))
# set font and rotation for date tick labels
plt.gcf().autofmt_xdate()
© www.soinside.com 2019 - 2024. All rights reserved.