Unable to adjust x-axis DateFormat in pandas bar chart

前端 未结 2 1474
旧巷少年郎
旧巷少年郎 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:15

    If you have grouped your data using groupby then you can do the same thing as the answer above using:

    new_ticks = []
    for dt in data.index:
        new_ticks.append(datetime.datetime(dt[0],dt[1],1))
    ax.set_xticklabels([dt.strftime('%Y-%m') for dt in new_ticks])
    

    Mine was summed and grouped by month so the index tuple was only (2009,4) for example. Obviously, if you have more aspects to your tuple (i.e. day, hour) then you would add those to the datetime function

提交回复
热议问题