Pandas 使用 datetime64 绘制条形图
Pandas plot bar graph with datetime64
我有以下数据框:
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]
类型。
当我绘制数据时 hours/minutes/seconds 是可见的。如何删除hours/minutes/seconds?
您应该使用 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()
我有以下数据框:
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]
类型。
当我绘制数据时 hours/minutes/seconds 是可见的。如何删除hours/minutes/seconds?
您应该使用 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()