如何使用matplotlib直方图进行绘图,其中x轴是每日时间戳范围

时间:2018-03-07 07:45:20

标签: python-3.x matplotlib

我的数据记录了工人完成日常任务的时间。 该列表有100,000个条目。时间在15:00:00到19:00:00之间

由于x轴的格式不同,我很难将数据绘制为直方图。(我的直方图将缺少61到99分钟的空间)

Data=['16:24:00',
     '17:48:00',
     '16:10:00',
     '16:46:00',
     '17:13:00',
     '15:31:00',
     '16:23:00',
     '16:53:00',
     '16:28:00',
     '16:33:00',
     '17:38:00',
     '17:08:00',
     '16:29:00',
     '16:25:00',
     '16:17:00',
     '17:38:00',
     '16:29:00',
     ...]

我尝试使用matplotlib.dates来格式化轴但遇到ValueError:ordinal必须是> = 1

尝试1

fig, ax = plt.subplots(1,1)
ax.hist(Data ,bins=50)
ax.xaxis.set_major_locator(mdates.DayLocator())
ax.xaxis.set_major_formatter(mdates.DateFormatter('%H:%M:%S'))
ax.xaxis.set_minor_locator(mdates.MinuteLocator())
plt.show()

尝试2

fig, ax = plt.subplots(1,1)
locator = mdates.AutoDateLocator()
ax.hist(Data ,bins=50)
ax.xaxis.set_major_locator(locator)
ax.xaxis.set_major_formatter(mdates.AutoDateFormatter(locator))
ax.xaxis.set_minor_locator(mdates.MinuteLocator())
plt.show()

I hope to get something like this but with x-axis printed and y-axis represents occurrences

1 个答案:

答案 0 :(得分:0)

Imho的问题是你的Data是字符串,因此被视为分类数据。将它们转换为datetime对象:

from matplotlib import pyplot as plt 
from matplotlib import dates as mdates

Data=['16:24:00',
     '17:48:00',
     '16:10:00',
     '16:46:00',
     '17:13:00',
     '15:31:00',
     '16:23:00',
     '16:53:00',
     '16:28:00',
     '16:33:00',
     '17:38:00',
     '17:08:00',
     '16:29:00',
     '16:25:00',
     '16:17:00',
     '17:38:00',
     '16:29:00']
#convert strings into datetime objects
conv_time = [datetime.strptime(i, "%H:%M:%S") for i in Data]
#define bin number
bin_nr = 7
fig, ax = plt.subplots(1,1)
#create histogram, get bin position for label
_counts, bins, _patches = ax.hist(conv_time, bins = bin_nr)
#set xticks at bin edges
plt.xticks(bins)
#reformat bin label into format hour:minute
ax.xaxis.set_major_formatter(mdates.DateFormatter("%H:%M"))
plt.show()

输出:
enter image description here