-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
timeline example #11403
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
timeline example #11403
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
""" | ||
=============================================== | ||
Creating a timeline with lines, dates, and text | ||
=============================================== | ||
|
||
How to create a simple timeline using Matplotlib release dates. | ||
|
||
Timelines can be created with a collection of dates and text. In this example, | ||
we show how to create a simple timeline using the dates for recent releases | ||
of Matplotlib. First, we'll pull the data from GitHub. | ||
""" | ||
|
||
import matplotlib.pyplot as plt | ||
import numpy as np | ||
import matplotlib.dates as mdates | ||
from datetime import datetime | ||
import urllib.request | ||
import json | ||
|
||
# Grab a list of Matplotlib releases | ||
url = 'https://api.github.com/repos/matplotlib/matplotlib/releases' | ||
data = json.loads(urllib.request.urlopen(url).read().decode()) | ||
|
||
names = [] | ||
dates = [] | ||
for irelease in data: | ||
if 'rc' not in irelease['tag_name'] and 'b' not in irelease['tag_name']: | ||
names.append(irelease['tag_name']) | ||
# Convert date strings (e.g. 2014-10-18T18:56:23Z) to datetime | ||
dates.append(datetime.strptime(irelease['published_at'], | ||
"%Y-%m-%dT%H:%M:%SZ")) | ||
|
||
############################################################################## | ||
# Next, we'll iterate through each date and plot it on a horizontal line. | ||
# We'll add some styling to the text so that overlaps aren't as strong. | ||
# | ||
# Note that Matplotlib will automatically plot datetime inputs. | ||
|
||
levels = np.array([-5, 5, -3, 3, -1, 1]) | ||
fig, ax = plt.subplots(figsize=(20, 5)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The maximal figure width an example should have is 900 pixels (=9 inch for default dpi). See this issue. |
||
|
||
# Create the base line | ||
start = min(dates) | ||
stop = max(dates) | ||
ax.plot((start, stop), (0, 0), 'k', alpha=.5) | ||
|
||
# Iterate through releases annotating each one | ||
for ii, (iname, idate) in enumerate(zip(names, dates)): | ||
level = levels[ii % 6] | ||
vert = 'top' if level < 0 else 'bottom' | ||
|
||
ax.scatter(idate, 0, s=100, facecolor='w', edgecolor='k', zorder=9999) | ||
# Plot a line up to the text | ||
ax.plot((idate, idate), (0, level), c='r', alpha=.7) | ||
# Give the text a faint background and align it properly | ||
ax.text(idate, level, iname, | ||
horizontalalignment='right', verticalalignment=vert, fontsize=14, | ||
backgroundcolor=(1., 1., 1., .3)) | ||
ax.set(title="Matplotlib release dates") | ||
# Set the xticks formatting | ||
# format xaxis with 3 month intervals | ||
ax.get_xaxis().set_major_locator(mdates.MonthLocator(interval=3)) | ||
ax.get_xaxis().set_major_formatter(mdates.DateFormatter("%b %Y")) | ||
fig.autofmt_xdate() | ||
|
||
# Remove components for a cleaner look | ||
plt.setp((ax.get_yticklabels() + ax.get_yticklines() + | ||
list(ax.spines.values())), visible=False) | ||
plt.show() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure if the examples should depend on web links. Or is this safe to link to github.com because the tests themselves run on github?