So, you start with a list of dates that you want to histogram:
from datetime import datetime
list_of_datetime_datetime_objects = [datetime(2010, 6, 14), datetime(1974, 2, 8), datetime(1974, 2, 8)]
Matplotlib allows you to convert a datetime.datetime
object into a simple number, as David mentioned:
from matplotlib.dates import date2num, num2date
num_dates = [date2num(d) for d in list_of_datetime_datetime_objects]
You can then calculate the histogram of your data:
import numpy
histo = numpy.histogram(num_dates) # Look at the doc for more options (number of bins, etc.)
Since you want the cumulative histogram, you add individual counts together:
cumulative_histo_counts = histo[0].cumsum()
The histogram plot will need the bin size:
from matplotlib import pyplot
You can then plot the cumulative histogram:
bin_size = histo[1][1]-histo[1][0]
pyplot.bar(histo[1][:-1], cumulative_histo_counts, width=bin_size)
Alternatively, you might want a curve instead of an histogram:
# pyplot.plot(histo[1][1:], cumulative_histo_counts)
If you want dates on the x axis instead of numbers, you can convert the numbers back to dates and ask matplotlib to use date strings as ticks, instead of numbers:
from matplotlib import ticker
# The format for the x axis is set to the chosen string, as defined from a numerical date:
pyplot.gca().xaxis.set_major_formatter(ticker.FuncFormatter(lambda numdate, _: num2date(numdate).strftime('%Y-%d-%m')))
# The formatting proper is done:
pyplot.gcf().autofmt_xdate()
# To show the result:
pyplot.show() # or draw(), if you don't want to block
Here, gca()
and gcf()
return the current axis and figure, respectively.
Of course, you can adapt the way you display dates, in the call to strftime()
above.
To go beyond your question, I would like to mention that Matplotlib's gallery is a very good source of information: you can generally quickly find what you need by just finding images that look like what you're trying to do, and looking at their source code.