-
-
Save trieb/57c5b506fc89e2c97b775c585d996f61 to your computer and use it in GitHub Desktop.
Plot the difference in days between dates in a list
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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('2015-12-15') | |
dates.append('2016-01-10') | |
dates.append('2016-02-01') | |
dates.append('2016-02-21') | |
dates.append('2016-03-14') | |
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