熊猫:直方图绘图

时间:2018-01-26 12:24:01

标签: python pandas plot visualization

我在python中有一个带日期(datetime)的数据框。如何使用此数据框绘制出现时间为30分钟的柱状图?

             starttime
1 2016-09-11 00:24:24
2 2016-08-28 00:24:24
3 2016-07-31 05:48:31
4 2016-09-11 00:23:14
5 2016-08-21 00:55:23
6 2016-08-21 01:17:31
       .............
989872 2016-10-29 17:31:33
989877 2016-10-02 10:00:35
989878 2016-10-29 16:42:41
989888 2016-10-09 07:43:27
989889 2016-10-09 07:42:59
989890 2016-11-05 14:30:59

我尝试过查看Plotting series histogram in PandasA per-hour histogram of datetime using Pandas中的示例。但他们似乎正在使用一个不是我需要的条形图。我试图使用temp.groupby([temp["starttime"].dt.hour, temp["starttime"].dt.minute]).count().plot(kind="hist")创建直方图,给出结果,如下所示

enter image description here

如果可能,我希望X轴显示时间(例如07:30:00)

1 个答案:

答案 0 :(得分:0)

我认为您需要bar绘图,而对于时间最简单的轴,将datetime转换为string s strftime

temp = temp.resample('30T', on='starttime').count()
ax = temp.groupby(temp.index.strftime('%H:%M')).sum().plot(kind="bar")

#for nicer bar some ticklabels are hidden
spacing = 2
visible = ax.xaxis.get_ticklabels()[::spacing]
for label in ax.xaxis.get_ticklabels():
    if label not in visible:
        label.set_visible(False)