So I am trying to plot a histogram of some datetime objects, but i can never get the bins to line up with the bars. My code is below:
I start by importing my stuff and declaring the start, end and a one day object:
import datetime
import matplotlib.pyplot as plt
end = datetime.date(2017,5,14)
start = datetime.date(2017,5,8)
one_day = datetime.timedelta(days = 1)
Then I declare an arbitrary list of dates:
date_list = [datetime.date(2017,5,14), datetime.date(2017,5,14),
datetime.date(2017,5,14), datetime.date(2017,5,9), datetime.date(2017,5,13),
datetime.date(2017,5,12), datetime.date(2017,5,11),
datetime.date(2017,5,11), datetime.date(2017,5,9)]
Then I go through the range of days between my start and end (in this case a week), adding each date to a list:
week = []
for i in range((end-start).days+1):
week.append(start + (i)*one_day)
The reason for this is that some of the days in the week don't come up in the date_list (I know I could just skip this for this case, but I want it to be expandable to other date_lists).
Then I plot with hist():
plt.hist(date_list, bins = len(week)+1)
plt.show()
I've tried all manner of combinations of bin formats with various +1's and ranges and aligns but the date never sit in a consistent place in the bar.