python - Pandas: Plot a histogram of times in intervals of 10 minutes
问题描述
I have a dataframe in this format:
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
I would like to plot a histogram of arrival times in fixed buckets, e.g. every 10 minutes.
Utilising the follow code from other answers, I've managed to produce the following histogram:
df['ARRIVAL TIME'] = pd.to_datetime(df['ARRIVAL TIME'])
plt.hist([t.hour + t.minute/60. for t in df['ARRIVAL TIME']], bins = 8)
That's close to what I want. However, I'd prefer the bins to be "7:30", "7:40", etc.
解决方案
如果您只想手动更改默认刻度标签(例如,请参阅此答案),则以下内容应该可以工作(在运行您已经完成的命令之后):
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
推荐阅读
- c# - 当用户在 textbox1 和 textbox2 中键入无效字符时,会出现“无效字符消息”并且 textbox1 的文本变为红色
- arrays - 打印二维数组:警告:数组初始值设定项中的多余元素
- linux - 无法完成与 2404:6800:4005:807::200e:80 的 SOCKS5 连接
- python - 如果你做太多'layers.MaxPooling2D()'会发生什么
- azure - 如何在 power shell 中使用 RM 模块获取 Azure App 配置访问密钥
- sails.js - 如何在 Sails.js 中将我们自己的自定义 JSON 消息添加到 Blueprint API?
- python - 结合CNN和双向LSTM
- r - Importing a csv file using fread loses factor order
- regex - Ansible 替换大配置文件中的值
- javascript - NodeJS Var 未定义自身?