1

嗨,我有一个数据框,其中日期作为索引,份额作为列,我想使用日期作为 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轴的间隔太小了,读不出来。在此处输入图像描述. 例如,如何增加间隔显示每 10 天?

4

2 回答 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)

测试你喜欢的。

于 2018-12-09T01:33:20.150 回答
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()
于 2018-12-09T03:02:51.243 回答