我有一个这种格式的数据框:
DATE NAME ARRIVAL TIME
275 2018-07-05 Adam 19:33:51.579885
276 2018-07-05 Bill 19:38:57.578135
277 2018-07-05 Cindy 19:40:24.704381
278 2018-07-05 Don 19:34:29.689414
279 2018-07-05 Eric 19:33:54.173609
我想绘制固定桶中到达时间的直方图,例如每10分钟。
利用其他答案中的跟踪代码,我设法产生了以下直方图:
df['ARRIVAL TIME'] = pd.to_datetime(df['ARRIVAL TIME'])
plt.hist([t.hour + t.minute/60. for t in df['ARRIVAL TIME']], bins = 8)
那接近我想要的。但是,我希望将垃圾箱设置为“ 7:30”,“ 7:40”等。
答案 0 :(得分:2)
如果您只想手动更改默认刻度标签(例如,this answer),则应该可以执行以下操作(在运行已经完成的命令之后):
plt.draw() # do this so that the labels are generated
ax = plt.gca() # get the figure axes
xticks = ax.get_xticklabels() # get the current x-tick labels
newlabels = []
for label in xticks:
h, m = divmod(float(label.get_text())%12, 1) # get hours and minutes (in 12 hour clock)
newlabels.append('{0:02d}:{1:02d}'.format(int(h), int(m*60))) # create the new label
ax.set_xticklabels(newlabels) # set the new labels
但是,如果要专门设置直方图框的边缘间隔为10分钟,则可以执行以下操作:
import numpy as np
# get a list of the times
times = [t.hour + t.minute/60. for t in df['ARRIVAL TIME']]
# set the time interval required (in minutes)
tinterval = 10.
# find the lower and upper bin edges (on an integer number of 10 mins past the hour)
lowbin = np.min(times) - np.fmod(np.min(times)-np.floor(np.min(times)), tinterval/60.)
highbin = np.max(times) - np.fmod(np.max(times)-np.ceil(np.max(times)), tinterval/60.)
bins = np.arange(lowbin, highbin, tinterval/60.) # set the bin edges
# create the histogram
plt.hist(times, bins=bins)
ax = plt.gca() # get the current plot axes
ax.set_xticks(bins) # set the position of the ticks to the histogram bin edges
# create new labels in hh:mm format (in twelve hour clock)
newlabels = []
for edge in bins:
h, m = divmod(edge%12, 1) # get hours and minutes (in 12 hour clock)
newlabels.append('{0:01d}:{1:02d}'.format(int(h), int(m*60))) # create the new label
ax.set_xticklabels(newlabels) # set the new labels