|
| 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 pandas as pd |
| 16 | +import urllib.request |
| 17 | +import json |
| 18 | + |
| 19 | +# Grab a list of Matplotlib releases |
| 20 | +url = 'https://api.github.com/repos/matplotlib/matplotlib/releases' |
| 21 | +data = json.loads(urllib.request.urlopen(url).read().decode()) |
| 22 | + |
| 23 | +releases = [] |
| 24 | +for irelease in data: |
| 25 | + releases.append((irelease['tag_name'], irelease['published_at'])) |
| 26 | +releases = pd.DataFrame(releases, columns=['name', 'date']) |
| 27 | +releases['date'] = pd.to_datetime(releases['date']) |
| 28 | +# Remove release candidates and betas |
| 29 | +releases = releases.loc[['rc' not in nm for nm in releases['name']]] |
| 30 | +releases = releases.loc[['b' not in nm for nm in releases['name']]] |
| 31 | + |
| 32 | +############################################################################## |
| 33 | +# Next, we'll iterate through each date and plot it on a horizontal line. |
| 34 | +# We'll add some styling to the text so that overlaps aren't as strong. |
| 35 | +# |
| 36 | +# Note that Matplotlib will automatically plot datetime inputs. |
| 37 | + |
| 38 | +levels = np.array([-5, 5, -3, 3, -1, 1]) |
| 39 | +fig, ax = plt.subplots(figsize=(20, 5)) |
| 40 | + |
| 41 | +# Create the base line |
| 42 | +start = releases['date'].min() |
| 43 | +stop = releases['date'].max() |
| 44 | +ax.plot((start, stop), (0, 0), 'k', alpha=.5) |
| 45 | + |
| 46 | +# Iterate through releases annotating each one |
| 47 | +for ix, (iname, idate) in releases.iterrows(): |
| 48 | + level = levels[ix % 6] |
| 49 | + vert = 'top' if level < 0 else 'bottom' |
| 50 | + |
| 51 | + ax.scatter(idate, 0, s=100, facecolor='w', edgecolor='k', zorder=9999) |
| 52 | + # Plot a line up to the text |
| 53 | + ax.plot((idate, idate), (0, level), |
| 54 | + 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 | +xticks = pd.date_range(start, stop, freq='3M') |
| 62 | +ax.set_xticks(xticks) |
| 63 | +ax.set_xticklabels(xticks.strftime("%b %Y"), |
| 64 | + rotation=45, horizontalalignment='right', fontsize=14) |
| 65 | +# Remove components for a cleaner look |
| 66 | +plt.setp((ax.get_yticklabels() + ax.get_yticklines() + |
| 67 | + list(ax.spines.values())), visible=False) |
| 68 | +plt.show() |
0 commit comments