I have a list of dates that span several (hundred) years. I'd like to make a histogram that has 366 buckets, one for each day of the year, with the x-axis labelled in a legible way that allows me to see which date is which (I'm expecting a dip for February 29, for example).
I've made the following histogram, but easy-to-read X-axis date labels would be awesome. The following code seems cumbersome but gets me what I want (without the X-axis labels):
from datetime import date, datetime, timedelta
from collections import Counter
import pylab
def plot_data(data):
"""data is a list of dicts that contain a field "date" with a datetime."""
def get_day(d):
return d.strftime("%B %d") # e.g. January 01
days = []
n = 366
start_date = date(2020, 1, 1) # pick a leap year
for i in range(n):
d = start_date + timedelta(days=i)
days.append(get_day(d))
counts = Counter(get_day(d['date']) for d in data)
Y = [counts.get(d) for d in days]
X = list(range(len(days)))
pylab.bar(X, Y)
pylab.xlim([0, n])
pylab.title("Dates day of year")
pylab.xlabel("Day of Year (0-366)")
pylab.ylabel("Count")
pylab.savefig("Figure 1.png")
Any help to shorten this up and make for more flexible and legible x-axis dates would be much appreciated!
UPDATE
I've incorporated the ideas below into the following gist, which produces output that looks like this: