15

I am trying to plot a histogram of datetime.time values. Where these values are discretized into five minute slices. The data looks like this, in a list:

['17:15:00', '18:20:00', '17:15:00', '13:10:00', '17:45:00', '18:20:00']

I would like to plot a histogram, or some form of distribution graph so that the number of occurrences of each time can be examined easily.

NB. Given each time is discretised then. The maximum number of bins in a histogram would be 288 = (60 / 5 * 24)

I have looked at matplotlib.pyplot.hist. But is requires some sort of continuous scalar

Julia Meshcheryakova
  • 3,162
  • 3
  • 22
  • 42
Spacen Jasset
  • 938
  • 2
  • 11
  • 21
  • 1
    The simplest might be to convert the times to seconds and use this to make the histogram. You could then change the labels of the histogram manually. – David Zwicker Dec 03 '11 at 17:48
  • 1
    Or convert the times to bins directly = seconds/300, and plot the count of each bin. – Dave Dec 03 '11 at 18:18

2 Answers2

10

I did what David Zwicker said and used seconds, and then changed the x axis. I will look at what Dave said about 'bins'. This works roughly and gives a bar per hour plot to start with.

def chart(occurance_list):
    hour_list = [t.hour for t in occurance_list]
    print hour_list
    numbers=[x for x in xrange(0,24)]
    labels=map(lambda x: str(x), numbers)
    plt.xticks(numbers, labels)
    plt.xlim(0,24)
    plt.hist(hour_list)
    plt.show()

frequency of lowest daily exahange rate for GBPUSD

Spacen Jasset
  • 938
  • 2
  • 11
  • 21
  • Accept: what David Zwicker and @Dave said – Spacen Jasset Dec 06 '11 at 12:21
  • it is interesting that matplotlib doesn't treat this more cleanly. matplotlib has perfectly good functionality for datetimes, but for some reason `matplotlib` and `pandas` don't want to treat clock times like numbers (also in the sense of adding/subtracting etc...). – Marses Jun 22 '21 at 14:32
-10

you have to convert the data in two variable and then you can use plotlab to plot in histograms.