1

I have time data like below.

      time
0  0:00:30
1  0:01:34
.
.

And I want to draw histogram depends on the time.
For example, this is the x-axis of histogram like 0-8, 9-16, 17-24.
How can I divide and draw it?

I used matplotlib histogram.

plt.hist(dailyData['time'])
plt.show()
martineau
  • 119,623
  • 25
  • 170
  • 301
DD DD
  • 1,148
  • 1
  • 10
  • 33
  • https://stackoverflow.com/questions/8369584/plot-histogram-of-datetime-time-python-matplotlib I've already read this, but it doesn't work for me. – DD DD Jul 15 '19 at 04:37
  • 1
    Why does it not work for you? – BenT Jul 15 '19 at 04:41
  • It says that 'str' object has no attribute 'hour'... – DD DD Jul 15 '19 at 04:44
  • 1
    Edit your question and include how you used the code. To use that code you need to first convert your data to `datetime` objects. – BenT Jul 15 '19 at 04:47
  • without that code, isn't there the other way? – DD DD Jul 15 '19 at 04:50
  • 1
    You will want to convert to `datetime` objects regardless, unless you want to parse out the first 2 digits of every string, convert them to ints and then group them. Use `pd.to_datetime()` – BenT Jul 15 '19 at 05:00

1 Answers1

1

For reference, first convert your data to datetime objects. Then get only the hours in a list using a list comprehension. Next select your bins and plot.

data = pd.to_datetime(dailyData['time'], format="%H:%M:%S") 

hour_list = [t.hour for t in data]
bins = [0,9,17,24]

plt.hist(hour_list,bins)
plt.show()
BenT
  • 3,172
  • 3
  • 18
  • 38
  • One more thing please. When the time is 2:10:20, I want to get the full minute like ' 2*60+10" = 130. How can I get this from datetime objects? – DD DD Jul 15 '19 at 05:20
  • 1
    To get a similar list of minutes data you would do something like this: `minute_list = [(t.hour*60+t.minute) for t in data]` – BenT Jul 15 '19 at 05:23