|
| 1 | +""" |
| 2 | +=============================================== |
| 3 | +Creating a timeline with lines, dates, and text |
| 4 | +=============================================== |
| 5 | +
|
| 6 | +How to create a simple timeline using Matplotlib release dates. |
| 7 | +
|
| 8 | +Timelines can be created with a collection of dates and text. In this example, |
| 9 | +we show how to create a simple timeline using the dates for recent releases |
| 10 | +of Matplotlib. First, we'll pull the data from GitHub. |
| 11 | +""" |
| 12 | + |
| 13 | +import matplotlib.pyplot as plt |
| 14 | +import numpy as np |
| 15 | +import matplotlib.dates as mdates |
| 16 | +from datetime import datetime |
| 17 | +import urllib.request |
| 18 | +import json |
| 19 | + |
| 20 | +# Grab a list of Matplotlib releases |
| 21 | +url = 'https://api.github.com/repos/matplotlib/matplotlib/releases' |
| 22 | +data = json.loads(urllib.request.urlopen(url).read().decode()) |
| 23 | + |
| 24 | +names = [] |
| 25 | +dates = [] |
| 26 | +for irelease in data: |
| 27 | + if 'rc' not in irelease['tag_name'] and 'b' not in irelease['tag_name']: |
| 28 | + names.append(irelease['tag_name']) |
| 29 | + # Convert date strings (e.g. 2014-10-18T18:56:23Z) to datetime |
| 30 | + dates.append(datetime.strptime(irelease['published_at'], |
| 31 | + "%Y-%m-%dT%H:%M:%SZ")) |
| 32 | + |
| 33 | +############################################################################## |
| 34 | +# Next, we'll iterate through each date and plot it on a horizontal line. |
| 35 | +# We'll add some styling to the text so that overlaps aren't as strong. |
| 36 | +# |
| 37 | +# Note that Matplotlib will automatically plot datetime inputs. |
| 38 | + |
| 39 | +levels = np.array([-5, 5, -3, 3, -1, 1]) |
| 40 | +fig, ax = plt.subplots(figsize=(20, 5)) |
| 41 | + |
| 42 | +# Create the base line |
| 43 | +start = min(dates) |
| 44 | +stop = max(dates) |
| 45 | +ax.plot((start, stop), (0, 0), 'k', alpha=.5) |
| 46 | + |
| 47 | +# Iterate through releases annotating each one |
| 48 | +for ii, (iname, idate) in enumerate(zip(names, dates)): |
| 49 | + level = levels[ii % 6] |
| 50 | + vert = 'top' if level < 0 else 'bottom' |
| 51 | + |
| 52 | + ax.scatter(idate, 0, s=100, facecolor='w', edgecolor='k', zorder=9999) |
| 53 | + # Plot a line up to the text |
| 54 | + ax.plot((idate, idate), (0, level), c='r', alpha=.7) |
| 55 | + # Give the text a faint background and align it properly |
| 56 | + ax.text(idate, level, iname, |
| 57 | + horizontalalignment='right', verticalalignment=vert, fontsize=14, |
| 58 | + backgroundcolor=(1., 1., 1., .3)) |
| 59 | +ax.set(title="Matplotlib release dates") |
| 60 | +# Set the xticks formatting |
| 61 | +# format xaxis with 3 month intervals |
| 62 | +ax.get_xaxis().set_major_locator(mdates.MonthLocator(interval=3)) |
| 63 | +ax.get_xaxis().set_major_formatter(mdates.DateFormatter("%b %Y")) |
| 64 | +fig.autofmt_xdate() |
| 65 | + |
| 66 | +# Remove components for a cleaner look |
| 67 | +plt.setp((ax.get_yticklabels() + ax.get_yticklines() + |
| 68 | + list(ax.spines.values())), visible=False) |
| 69 | +plt.show() |
0 commit comments