Skip to content

Instantly share code, notes, and snippets.

Created March 15, 2016 14:59
Show Gist options
  • Save anonymous/028a151b276a8d55a174 to your computer and use it in GitHub Desktop.
Save anonymous/028a151b276a8d55a174 to your computer and use it in GitHub Desktop.
Plot the difference in days between dates in a list
from datetime import datetime
import matplotlib.pyplot as plt
# TODO: Use plotly instead of matplotlib
# See: https://plot.ly/python/bar-charts/
date_format = '%Y-%m-%d'
dates = []
dates.append('2016-01-01')
dates.append('2016-01-07')
dates.append('2016-01-15')
dates.append('2016-01-20')
dates.append('2016-02-01')
diffs = []
for i in range(len(dates)):
if i < len(dates) -1:
d0 = datetime.strptime(dates[i], date_format)
d1 = datetime.strptime(dates[i+1], date_format)
else:
d0 = datetime.strptime(dates[-1], date_format)
d1 = datetime.now()
diff = d1 - d0
print(diff.days)
diffs.append(int(diff.days))
x = [diff for diff in diffs]
y = [datetime.strptime(dates[date], date_format) for date in range(len(dates))]
print(len(x), len(y))
plt.bar(y, x)
plt.title('My Title')
plt.gcf().autofmt_xdate()
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment