Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitcd1ae80

Browse files
authored
Merge pull request#11403 from choldgraf/timeline
timeline example
2 parents33d3fea +4e60dfb commitcd1ae80

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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+
importmatplotlib.pyplotasplt
14+
importnumpyasnp
15+
importmatplotlib.datesasmdates
16+
fromdatetimeimportdatetime
17+
importurllib.request
18+
importjson
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+
forireleaseindata:
27+
if'rc'notinirelease['tag_name']and'b'notinirelease['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+
forii, (iname,idate)inenumerate(zip(names,dates)):
49+
level=levels[ii%6]
50+
vert='top'iflevel<0else'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

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp