【问题标题】:Pandas plot bar graph with datetime64Pandas 用 datetime64 绘制条形图
【发布时间】:2019-04-12 08:02:45
【问题描述】:

我有以下数据框:

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] 类型。

当我绘制数据小时/分钟/秒时可见。如何减少小时/分钟/秒?

【问题讨论】:

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


    【解决方案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()
    

    【讨论】:

      猜你喜欢
      • 2015-06-12
      • 2016-05-24
      • 2017-08-27
      • 2016-03-18
      • 2018-11-26
      • 2020-07-25
      相关资源
      最近更新 更多