|
12 | 12 |
|
13 | 13 | import matplotlib.pyplot as plt
|
14 | 14 | import numpy as np
|
15 |
| -import pandas as pd |
| 15 | +import matplotlib.dates as mdates |
| 16 | +from datetime import datetime |
16 | 17 | import urllib.request
|
17 | 18 | import json
|
18 | 19 |
|
19 | 20 | # Grab a list of Matplotlib releases
|
20 | 21 | url = 'https://api.github.com/repos/matplotlib/matplotlib/releases'
|
21 | 22 | data = json.loads(urllib.request.urlopen(url).read().decode())
|
22 | 23 |
|
23 |
| -releases = [] |
| 24 | +names = [] |
| 25 | +dates = [] |
24 | 26 | 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']]] |
| 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")) |
31 | 32 |
|
32 | 33 | ##############################################################################
|
33 | 34 | # Next, we'll iterate through each date and plot it on a horizontal line.
|
|
39 | 40 | fig, ax = plt.subplots(figsize=(20, 5))
|
40 | 41 |
|
41 | 42 | # Create the base line
|
42 |
| -start = releases['date'].min() |
43 |
| -stop = releases['date'].max() |
| 43 | +start = min(dates) |
| 44 | +stop = max(dates) |
44 | 45 | ax.plot((start, stop), (0, 0), 'k', alpha=.5)
|
45 | 46 |
|
46 | 47 | # Iterate through releases annotating each one
|
47 |
| -for ix, (iname, idate) in releases.iterrows(): |
48 |
| - level = levels[ix % 6] |
| 48 | +for ii, (iname, idate) in enumerate(zip(names, dates)): |
| 49 | + level = levels[ii % 6] |
49 | 50 | vert = 'top' if level < 0 else 'bottom'
|
50 | 51 |
|
51 | 52 | ax.scatter(idate, 0, s=100, facecolor='w', edgecolor='k', zorder=9999)
|
52 | 53 | # Plot a line up to the text
|
53 |
| - ax.plot((idate, idate), (0, level), |
54 |
| - c='r', alpha=.7) |
| 54 | + ax.plot((idate, idate), (0, level), c='r', alpha=.7) |
55 | 55 | # Give the text a faint background and align it properly
|
56 | 56 | ax.text(idate, level, iname,
|
57 | 57 | horizontalalignment='right', verticalalignment=vert, fontsize=14,
|
58 | 58 | backgroundcolor=(1., 1., 1., .3))
|
59 | 59 | ax.set(title="Matplotlib release dates")
|
60 | 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) |
| 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 | +# ax.set_xticklabels(xticks.strftime("%b %Y"), |
| 66 | +# rotation=45, horizontalalignment='right', fontsize=14) |
65 | 67 | # Remove components for a cleaner look
|
66 | 68 | plt.setp((ax.get_yticklabels() + ax.get_yticklines() +
|
67 | 69 | list(ax.spines.values())), visible=False)
|
|
0 commit comments