首页 文章

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

提问于
浏览
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天显示一次的间隔?

2 回答

  • 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()
    
  • 1

    你可以旋转:

    plt.xticks(rotation=90)
    

    Demo:

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

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

    如果您愿意:

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

    测试你喜欢哪个 .

相关问题