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

Appearance settings

Commit 4e60dfb

Browse filesBrowse files
committed
removing pandas dependency
1 parent 9ad39b0 commit 4e60dfb
Copy full SHA for 4e60dfb

File tree

Expand file treeCollapse file tree

1 file changed

+19
-18
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+19
-18
lines changed

‎examples/lines_bars_and_markers/timeline.py

Copy file name to clipboardExpand all lines: examples/lines_bars_and_markers/timeline.py
+19-18Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,23 @@
1212

1313
import matplotlib.pyplot as plt
1414
import numpy as np
15-
import pandas as pd
15+
import matplotlib.dates as mdates
16+
from datetime import datetime
1617
import urllib.request
1718
import json
1819

1920
# Grab a list of Matplotlib releases
2021
url = 'https://api.github.com/repos/matplotlib/matplotlib/releases'
2122
data = json.loads(urllib.request.urlopen(url).read().decode())
2223

23-
releases = []
24+
names = []
25+
dates = []
2426
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"))
3132

3233
##############################################################################
3334
# Next, we'll iterate through each date and plot it on a horizontal line.
@@ -39,29 +40,29 @@
3940
fig, ax = plt.subplots(figsize=(20, 5))
4041

4142
# Create the base line
42-
start = releases['date'].min()
43-
stop = releases['date'].max()
43+
start = min(dates)
44+
stop = max(dates)
4445
ax.plot((start, stop), (0, 0), 'k', alpha=.5)
4546

4647
# 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]
4950
vert = 'top' if level < 0 else 'bottom'
5051

5152
ax.scatter(idate, 0, s=100, facecolor='w', edgecolor='k', zorder=9999)
5253
# 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)
5555
# Give the text a faint background and align it properly
5656
ax.text(idate, level, iname,
5757
horizontalalignment='right', verticalalignment=vert, fontsize=14,
5858
backgroundcolor=(1., 1., 1., .3))
5959
ax.set(title="Matplotlib release dates")
6060
# 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+
6566
# Remove components for a cleaner look
6667
plt.setp((ax.get_yticklabels() + ax.get_yticklines() +
6768
list(ax.spines.values())), visible=False)

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.