带有datetime64的熊猫图条形图

时间:2018-11-08 18:32:45

标签: python pandas datetime bar-chart datetime-format

我有以下数据框:

gr_data = data.groupby([pd.Grouper(key='date', freq='W-SUN')])['name'].count()


print(gr_data)

    date
    2018-08-19     582
    2018-09-02    1997
    2018-09-16    3224
    2018-10-07    4282
    2018-10-28    5618
    2018-11-04    5870
    Freq: W-SUN, Name: name, dtype: int64

我正在使用plot.bar()

绘制这些数据

'date'datetime64[ns]类型。

当我绘制数据时,可以看到小时/分钟/秒。如何减少小时/分钟/秒? enter image description here

1 个答案:

答案 0 :(得分:1)

您应使用strfttime进行转换。您可能要确保在绘制之前对dates进行排序,以便始终按时间顺序进行绘制。

import matplotlib.pyplot as plt

gr_data.sort_index(inplace=True) #Ensure plotting in time order

fig, ax = plt.subplots()
gr_data.assign(dates = gr_data.index.strftime('%Y-%m-%d')).plot(kind='bar', x='dates', ax=ax)

fig.autofmt_xdate()  #Rotate the dates so they aren't squished
plt.show()

enter image description here

相关问题