2

My data records the time whereby workers finished their daily task. The list has 100,000 entries. The time are within the range of 15:00:00 to 19:00:00

I have difficult plotting the data as histogram due to the x-axis are in different format.(My histogram will have missing space from 61 to 99 minutes)

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',
     ...]

I have tried using matplotlib.dates to format the axis but encounter ValueError: ordinal must be >= 1

Attempt 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()

Attempt 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

PCiah
  • 27
  • 1
  • 4

1 Answers1

0

Imho the problem is that your Data are strings and therefore treated as categorical data. Transform them into datetime objects:

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()

Output:
enter image description here

Mr. T
  • 11,960
  • 10
  • 32
  • 54