Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 992bc7f

Browse files
committed
timeline example
1 parent b841556 commit 992bc7f

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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
29+
releases = releases.loc[['rc' not in nm for nm in releases['name']]]
30+
31+
##############################################################################
32+
# Next, we'll iterate through each date and plot it on a horizontal line.
33+
# We'll add some styling to the text so that overlaps aren't as strong.
34+
#
35+
# Note that Matplotlib will automatically plot datetime inputs.
36+
37+
levels = np.array([-5, 5, -3, 3, -1, 1])
38+
fig, ax = plt.subplots(figsize=(20, 5))
39+
40+
# Create the base line
41+
start = releases['date'].min()
42+
stop = releases['date'].max()
43+
ax.plot((start, stop), (0, 0), 'k', alpha=.5)
44+
45+
# Iterate through releases annotating each one
46+
for ix, (iname, idate) in releases.iterrows():
47+
level = levels[ix % 6]
48+
vert = 'top' if level < 0 else 'bottom'
49+
50+
ax.scatter(idate, 0, s=100, facecolor='w', edgecolor='k', zorder=9999)
51+
# Plot a line up to the text
52+
ax.plot((idate, idate), (0, level),
53+
c='r', alpha=.7)
54+
# Give the text a faint background and align it properly
55+
ax.text(idate, level, iname,
56+
horizontalalignment='right', verticalalignment=vert, fontsize=14,
57+
backgroundcolor=(1., 1., 1., .3))
58+
ax.set(title="Matplotlib release dates")
59+
# Set the xticks formatting
60+
xticks = pd.date_range(start, stop, freq='3M')
61+
ax.set_xticks(xticks)
62+
ax.set_xticklabels(xticks.strftime("%b %Y"),
63+
rotation=45, horizontalalignment='right')
64+
# Remove components for a cleaner look
65+
plt.setp((ax.get_yticklabels() + ax.get_yticklines() +
66+
list(ax.spines.values())), visible=False)
67+
plt.show()

0 commit comments

Comments
 (0)