Skip to content

Make examples that load npy files with datetime(objects) py3 compatible #5292

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 2 commits into from
Oct 27, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions doc/users/recipes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,15 @@ you will see that the x tick labels are all squashed together.

import matplotlib.cbook as cbook
datafile = cbook.get_sample_data('goog.npy')
r = np.load(datafile).view(np.recarray)
try:
# Python3 cannot load python2 .npy files with datetime(object) arrays
# unless the encoding is set to bytes. Hovever this option was
# not added until numpy 1.10 so this example will only work with
# python 2 or with numpy 1.10 and later.
r = np.load(datafile, encoding='bytes').view(np.recarray)
except TypeError:
# Old Numpy
r = np.load(datafile).view(np.recarray)
plt.figure()
plt.plot(r.date, r.close)
plt.title('Default date handling can cause overlapping labels')
Expand Down Expand Up @@ -179,8 +187,14 @@ right.

# load up some sample financial data
datafile = cbook.get_sample_data('goog.npy')
r = np.load(datafile).view(np.recarray)

try:
# Python3 cannot load python2 .npy files with datetime(object) arrays
# unless the encoding is set to bytes. Hovever this option was
# not added until numpy 1.10 so this example will only work with
# python 2 or with numpy 1.10 and later.
r = np.load(datafile, encoding='bytes').view(np.recarray)
except TypeError:
r = np.load(datafile).view(np.recarray)
# create two subplots with the shared x and y axes
fig, (ax1, ax2) = plt.subplots(1,2, sharex=True, sharey=True)

Expand Down Expand Up @@ -363,4 +377,3 @@ argument takes a dictionary with keys that are Patch properties.
# place a text box in upper left in axes coords
ax.text(0.05, 0.95, textstr, transform=ax.transAxes, fontsize=14,
verticalalignment='top', bbox=props)

9 changes: 8 additions & 1 deletion examples/api/date_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,14 @@
# The record array stores python datetime.date as an object array in
# the date column
datafile = cbook.get_sample_data('goog.npy')
r = np.load(datafile).view(np.recarray)
try:
# Python3 cannot load python2 .npy files with datetime(object) arrays
# unless the encoding is set to bytes. Hovever this option was
# not added until numpy 1.10 so this example will only work with
# python 2 or with numpy 1.10 and later.
r = np.load(datafile, encoding='bytes').view(np.recarray)
except TypeError:
r = np.load(datafile).view(np.recarray)

fig, ax = plt.subplots()
ax.plot(r.date, r.adj_close)
Expand Down
9 changes: 8 additions & 1 deletion examples/pylab_examples/centered_ticklabels.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,14 @@

# load some financial data; apple's stock price
fh = cbook.get_sample_data('aapl.npy.gz')
r = np.load(fh)
try:
# Python3 cannot load python2 .npy files with datetime(object) arrays
# unless the encoding is set to bytes. Hovever this option was
# not added until numpy 1.10 so this example will only work with
# python 2 or with numpy 1.10 and later.
r = np.load(fh, encoding='bytes')
except TypeError:
r = np.load(fh)
fh.close()
r = r[-250:] # get the last 250 days

Expand Down
9 changes: 8 additions & 1 deletion examples/pylab_examples/scatter_demo2.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,14 @@
# The record array stores python datetime.date as an object array in
# the date column
datafile = cbook.get_sample_data('goog.npy')
price_data = np.load(datafile).view(np.recarray)
try:
# Python3 cannot load python2 .npy files with datetime(object) arrays
# unless the encoding is set to bytes. Hovever this option was
# not added until numpy 1.10 so this example will only work with
# python 2 or with numpy 1.10 and later
price_data = np.load(datafile, encoding='bytes').view(np.recarray)
except TypeError:
price_data = np.load(datafile).view(np.recarray)
price_data = price_data[-250:] # get the most recent 250 trading days

delta1 = np.diff(price_data.adj_close)/price_data.adj_close[:-1]
Expand Down