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

Skip to content

Commit d792936

Browse files
committed
Fix examples in recipes too
1 parent ca40ff5 commit d792936

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

doc/users/recipes.rst

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,15 @@ you will see that the x tick labels are all squashed together.
118118

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

180188
# load up some sample financial data
181189
datafile = cbook.get_sample_data('goog.npy')
182-
r = np.load(datafile).view(np.recarray)
183-
190+
try:
191+
# Python3 cannot load python2 .npy files with datetime(object) arrays
192+
# unless the encoding is set to bytes. Hovever this option was
193+
# not added until numpy 1.10 so this example will only work with
194+
# python 2 or with numpy 1.10 and later.
195+
r = np.load(datafile, encoding='bytes').view(np.recarray)
196+
except TypeError:
197+
r = np.load(datafile).view(np.recarray)
184198
# create two subplots with the shared x and y axes
185199
fig, (ax1, ax2) = plt.subplots(1,2, sharex=True, sharey=True)
186200

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

0 commit comments

Comments
 (0)