Unable to adjust x-axis DateFormat in pandas bar chart

前端 未结 2 1473
旧巷少年郎
旧巷少年郎 2021-02-15 15:12

I wish to plot a pandas time-series object data with matplotlib. For a simple line chart data.plot(), I was able to successfully change the x-axis date

2条回答
  •  深忆病人
    2021-02-15 16:05

    Since Pandas simply uses matplotlib you can of course create an identical (stacked) barchart with matplotlib. There is not reason why you can only use Pandas for that.

    Its not going to help in this case though. Matplotlib's bar() changes the xvalues from dates to floats, therefore a DateFormatter doesnt work anymore. You can check the xticks with ax.get_xticks().

    I dont see how you can make the xticks dates, but you can override the xticklabels yourself:

    ax.set_xticklabels([dt.strftime('%Y-%m-%d %H:%M:%S') for dt in data.index.to_pydatetime()])
    

提交回复
热议问题