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

Skip to content

Commit 01e1bd9

Browse files
committed
Autoload numpy arrays in get_sample_data.
This makes the examples cleaner (the focus should be on plotting, not on loading data), and it's also somewhat unlikely that one really wants to have a open()ed npy file without calling np.load anyways.
1 parent 43c35e7 commit 01e1bd9

File tree

22 files changed

+43
-63
lines changed

22 files changed

+43
-63
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
*np_load* parameter of ``cbook.get_sample_data``
2+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3+
This parameter is deprecated; `.get_sample_data` now auto-loads numpy arrays.
4+
Use ``get_sample_data(..., asfileobj=False)`` instead to get the filename of
5+
the data file, which can then be passed to `open`, if desired.

galleries/examples/axes_grid1/demo_axes_divider.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@
1313

1414

1515
def get_demo_image():
16-
z = cbook.get_sample_data("axes_grid/bivariate_normal.npy", np_load=True)
17-
# z is a numpy array of 15x15
16+
z = cbook.get_sample_data("axes_grid/bivariate_normal.npy") # 15x15 array
1817
return z, (-3, 4, -4, 3)
1918

2019

galleries/examples/axes_grid1/demo_axes_grid.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@
1111
from matplotlib import cbook
1212
from mpl_toolkits.axes_grid1 import ImageGrid
1313

14-
Z = cbook.get_sample_data( # (15, 15) array
15-
"axes_grid/bivariate_normal.npy", np_load=True)
14+
Z = cbook.get_sample_data("axes_grid/bivariate_normal.npy") # 15x15 array
1615
extent = (-3, 4, -4, 3)
1716

1817

galleries/examples/axes_grid1/demo_axes_grid2.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def add_inner_title(ax, title, loc, **kwargs):
2929
fig = plt.figure(figsize=(6, 6))
3030

3131
# Prepare images
32-
Z = cbook.get_sample_data("axes_grid/bivariate_normal.npy", np_load=True)
32+
Z = cbook.get_sample_data("axes_grid/bivariate_normal.npy")
3333
extent = (-3, 4, -4, 3)
3434
ZS = [Z[i::3, :] for i in range(3)]
3535
extent = extent[0], extent[1]/3., extent[2], extent[3]

galleries/examples/axes_grid1/demo_axes_rgb.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717

1818
def get_rgb():
19-
Z = cbook.get_sample_data("axes_grid/bivariate_normal.npy", np_load=True)
19+
Z = cbook.get_sample_data("axes_grid/bivariate_normal.npy")
2020
Z[Z < 0] = 0.
2121
Z = Z / Z.max()
2222

galleries/examples/axes_grid1/demo_colorbar_of_inset_axes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
fig, ax = plt.subplots(figsize=[5, 4])
1313
ax.set(aspect=1, xlim=(-15, 15), ylim=(-20, 5))
1414

15-
Z = cbook.get_sample_data("axes_grid/bivariate_normal.npy", np_load=True)
15+
Z = cbook.get_sample_data("axes_grid/bivariate_normal.npy")
1616
extent = (-3, 4, -4, 3)
1717

1818
axins = zoomed_inset_axes(ax, zoom=2, loc='upper left')

galleries/examples/axes_grid1/demo_edge_colorbar.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@
1414

1515

1616
def get_demo_image():
17-
z = cbook.get_sample_data("axes_grid/bivariate_normal.npy", np_load=True)
18-
# z is a numpy array of 15x15
17+
z = cbook.get_sample_data("axes_grid/bivariate_normal.npy") # 15x15 array
1918
return z, (-3, 4, -4, 3)
2019

2120

galleries/examples/axes_grid1/inset_locator_demo2.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,6 @@
1919
from mpl_toolkits.axes_grid1.anchored_artists import AnchoredSizeBar
2020
from mpl_toolkits.axes_grid1.inset_locator import mark_inset, zoomed_inset_axes
2121

22-
23-
def get_demo_image():
24-
z = cbook.get_sample_data("axes_grid/bivariate_normal.npy", np_load=True)
25-
# z is a numpy array of 15x15
26-
return z, (-3, 4, -4, 3)
27-
2822
fig, (ax, ax2) = plt.subplots(ncols=2, figsize=[6, 3])
2923

3024

@@ -51,9 +45,9 @@ def add_sizebar(ax, size):
5145
add_sizebar(axins, 0.5)
5246

5347

54-
# Second subplot, showing an image with an inset zoom
55-
# and a marked inset
56-
Z, extent = get_demo_image()
48+
# Second subplot, showing an image with an inset zoom and a marked inset
49+
Z = cbook.get_sample_data("axes_grid/bivariate_normal.npy") # 15x15 array
50+
extent = (-3, 4, -4, 3)
5751
Z2 = np.zeros((150, 150))
5852
ny, nx = Z.shape
5953
Z2[30:30+ny, 30:30+nx] = Z

galleries/examples/axes_grid1/simple_axesgrid2.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
)
2121

2222
# demo image
23-
Z = cbook.get_sample_data("axes_grid/bivariate_normal.npy", np_load=True)
23+
Z = cbook.get_sample_data("axes_grid/bivariate_normal.npy")
2424
im1 = Z
2525
im2 = Z[:, :10]
2626
im3 = Z[:, 10:]

galleries/examples/images_contours_and_fields/shading_example.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def main():
2222
x, y = np.mgrid[-5:5:0.05, -5:5:0.05]
2323
z = 5 * (np.sqrt(x**2 + y**2) + np.sin(x**2 + y**2))
2424

25-
dem = cbook.get_sample_data('jacksboro_fault_dem.npz', np_load=True)
25+
dem = cbook.get_sample_data('jacksboro_fault_dem.npz')
2626
elev = dem['elevation']
2727

2828
fig = compare(z, plt.cm.copper)

galleries/examples/lines_bars_and_markers/fill_between_alpha.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@
1818
import matplotlib.cbook as cbook
1919

2020
# load up some sample financial data
21-
r = (cbook.get_sample_data('goog.npz', np_load=True)['price_data']
22-
.view(np.recarray))
21+
r = cbook.get_sample_data('goog.npz')['price_data'].view(np.recarray)
2322
# create two subplots with the shared x and y axes
2423
fig, (ax1, ax2) = plt.subplots(1, 2, sharex=True, sharey=True)
2524

galleries/examples/lines_bars_and_markers/scatter_demo2.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@
1414
# low, close, volume, adj_close from the mpl-data/sample_data directory. The
1515
# record array stores the date as an np.datetime64 with a day unit ('D') in
1616
# the date column.
17-
price_data = (cbook.get_sample_data('goog.npz', np_load=True)['price_data']
18-
.view(np.recarray))
17+
price_data = cbook.get_sample_data('goog.npz')['price_data'].view(np.recarray)
1918
price_data = price_data[-250:] # get the most recent 250 trading days
2019

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

galleries/examples/mplot3d/custom_shaded_3d_surface.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from matplotlib.colors import LightSource
1414

1515
# Load and format data
16-
dem = cbook.get_sample_data('jacksboro_fault_dem.npz', np_load=True)
16+
dem = cbook.get_sample_data('jacksboro_fault_dem.npz')
1717
z = dem['elevation']
1818
nrows, ncols = z.shape
1919
x = np.linspace(dem['xmin'], dem['xmax'], ncols)

galleries/examples/specialty_plots/topographic_hillshading.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
from matplotlib.cbook import get_sample_data
2323
from matplotlib.colors import LightSource
2424

25-
dem = get_sample_data('jacksboro_fault_dem.npz', np_load=True)
25+
dem = get_sample_data('jacksboro_fault_dem.npz')
2626
z = dem['elevation']
2727
# -- Optional dx and dy for accurate vertical exaggeration --------------------
2828
# If you need topographically accurate vertical exaggeration, or you don't want

galleries/examples/subplots_axes_and_figures/zoom_inset_axes.py

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,36 +6,28 @@
66
Example of an inset axes and a rectangle showing where the zoom is located.
77
"""
88

9-
import matplotlib.pyplot as plt
109
import numpy as np
1110

1211
from matplotlib import cbook
12+
from matplotlib import pyplot as plt
1313

14-
15-
def get_demo_image():
16-
z = cbook.get_sample_data("axes_grid/bivariate_normal.npy", np_load=True)
17-
# z is a numpy array of 15x15
18-
return z, (-3, 4, -4, 3)
19-
20-
fig, ax = plt.subplots(figsize=[5, 4])
14+
fig, ax = plt.subplots()
2115

2216
# make data
23-
Z, extent = get_demo_image()
17+
Z = cbook.get_sample_data("axes_grid/bivariate_normal.npy") # 15x15 array
2418
Z2 = np.zeros((150, 150))
2519
ny, nx = Z.shape
2620
Z2[30:30+ny, 30:30+nx] = Z
21+
extent = (-3, 4, -4, 3)
2722

2823
ax.imshow(Z2, extent=extent, origin="lower")
2924

3025
# inset axes....
31-
axins = ax.inset_axes([0.5, 0.5, 0.47, 0.47])
26+
x1, x2, y1, y2 = -1.5, -0.9, -2.5, -1.9 # subregion of the original image
27+
axins = ax.inset_axes(
28+
[0.5, 0.5, 0.47, 0.47],
29+
xlim=(x1, x2), ylim=(y1, y2), xticklabels=[], yticklabels=[])
3230
axins.imshow(Z2, extent=extent, origin="lower")
33-
# subregion of the original image
34-
x1, x2, y1, y2 = -1.5, -0.9, -2.5, -1.9
35-
axins.set_xlim(x1, x2)
36-
axins.set_ylim(y1, y2)
37-
axins.set_xticklabels([])
38-
axins.set_yticklabels([])
3931

4032
ax.indicate_inset_zoom(axins, edgecolor="black")
4133

galleries/examples/text_labels_and_annotations/date.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
# low, close, volume, adj_close from the mpl-data/sample_data directory. The
3232
# record array stores the date as an np.datetime64 with a day unit ('D') in
3333
# the date column.
34-
data = cbook.get_sample_data('goog.npz', np_load=True)['price_data']
34+
data = cbook.get_sample_data('goog.npz')['price_data']
3535

3636
fig, axs = plt.subplots(3, 1, figsize=(6.4, 7), layout='constrained')
3737
# common to all three:

galleries/examples/ticks/centered_ticklabels.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@
2525
import matplotlib.ticker as ticker
2626

2727
# Load some financial data; Google's stock price
28-
r = (cbook.get_sample_data('goog.npz', np_load=True)['price_data']
29-
.view(np.recarray))
28+
r = cbook.get_sample_data('goog.npz')['price_data'].view(np.recarray)
3029
r = r[-250:] # get the last 250 days
3130

3231
fig, ax = plt.subplots()

galleries/examples/ticks/date_index_formatter.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
# low, close, volume, adj_close from the mpl-data/sample_data directory. The
2626
# record array stores the date as an np.datetime64 with a day unit ('D') in
2727
# the date column (``r.date``).
28-
r = cbook.get_sample_data('goog.npz', np_load=True)['price_data'].view(np.recarray)
28+
r = cbook.get_sample_data('goog.npz')['price_data'].view(np.recarray)
2929
r = r[:9] # get the first 9 days
3030

3131
fig, (ax1, ax2) = plt.subplots(nrows=2, figsize=(6, 6), layout='constrained')
@@ -80,3 +80,5 @@ def __call__(self, x, pos=0):
8080

8181

8282
ax2.xaxis.set_major_formatter(MyFormatter(r.date, '%a'))
83+
84+
plt.show()

galleries/tutorials/colors/colormapnorms.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@
249249
# elevation range than the water has depth range, and they are often
250250
# represented by a different colormap.
251251

252-
dem = cbook.get_sample_data('topobathy.npz', np_load=True)
252+
dem = cbook.get_sample_data('topobathy.npz')
253253
topo = dem['topo']
254254
longitude = dem['longitude']
255255
latitude = dem['latitude']

lib/matplotlib/cbook.py

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,9 @@ def is_scalar_or_string(val):
493493
return isinstance(val, str) or not np.iterable(val)
494494

495495

496-
def get_sample_data(fname, asfileobj=True, *, np_load=False):
496+
@_api.delete_parameter(
497+
"3.8", "np_load", alternative="open(get_sample_data(..., asfileobj=False))")
498+
def get_sample_data(fname, asfileobj=True, *, np_load=True):
497499
"""
498500
Return a sample data file. *fname* is a path relative to the
499501
:file:`mpl-data/sample_data` directory. If *asfileobj* is `True`
@@ -503,9 +505,8 @@ def get_sample_data(fname, asfileobj=True, *, np_load=False):
503505
the Matplotlib package.
504506
505507
If the filename ends in .gz, the file is implicitly ungzipped. If the
506-
filename ends with .npy or .npz, *asfileobj* is True, and *np_load* is
507-
True, the file is loaded with `numpy.load`. *np_load* currently defaults
508-
to False but will default to True in a future release.
508+
filename ends with .npy or .npz, and *asfileobj* is `True`, the file is
509+
loaded with `numpy.load`.
509510
"""
510511
path = _get_data_path('sample_data', fname)
511512
if asfileobj:
@@ -516,12 +517,6 @@ def get_sample_data(fname, asfileobj=True, *, np_load=False):
516517
if np_load:
517518
return np.load(path)
518519
else:
519-
_api.warn_deprecated(
520-
"3.3", message="In a future release, get_sample_data "
521-
"will automatically load numpy arrays. Set np_load to "
522-
"True to get the array and suppress this warning. Set "
523-
"asfileobj to False to get the path to the data file and "
524-
"suppress this warning.")
525520
return path.open('rb')
526521
elif suffix in ['.csv', '.xrc', '.txt']:
527522
return path.open('r')

lib/matplotlib/tests/test_colors.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -961,7 +961,7 @@ def test_autoscale_masked():
961961
@image_comparison(['light_source_shading_topo.png'])
962962
def test_light_source_topo_surface():
963963
"""Shades a DEM using different v.e.'s and blend modes."""
964-
dem = cbook.get_sample_data('jacksboro_fault_dem.npz', np_load=True)
964+
dem = cbook.get_sample_data('jacksboro_fault_dem.npz')
965965
elev = dem['elevation']
966966
dx, dy = dem['dx'], dem['dy']
967967
# Get the true cellsize in meters for accurate vertical exaggeration

lib/mpl_toolkits/axes_grid1/tests/test_axes_grid1.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ def test_inset_locator():
125125

126126
# prepare the demo image
127127
# Z is a 15x15 array
128-
Z = cbook.get_sample_data("axes_grid/bivariate_normal.npy", np_load=True)
128+
Z = cbook.get_sample_data("axes_grid/bivariate_normal.npy")
129129
extent = (-3, 4, -4, 3)
130130
Z2 = np.zeros((150, 150))
131131
ny, nx = Z.shape
@@ -166,7 +166,7 @@ def test_inset_axes():
166166

167167
# prepare the demo image
168168
# Z is a 15x15 array
169-
Z = cbook.get_sample_data("axes_grid/bivariate_normal.npy", np_load=True)
169+
Z = cbook.get_sample_data("axes_grid/bivariate_normal.npy")
170170
extent = (-3, 4, -4, 3)
171171
Z2 = np.zeros((150, 150))
172172
ny, nx = Z.shape
@@ -757,9 +757,7 @@ def test_anchored_locator_base_call():
757757
ax.set(aspect=1, xlim=(-15, 15), ylim=(-20, 5))
758758
ax.set(xticks=[], yticks=[])
759759

760-
Z = cbook.get_sample_data(
761-
"axes_grid/bivariate_normal.npy", np_load=True
762-
)
760+
Z = cbook.get_sample_data("axes_grid/bivariate_normal.npy")
763761
extent = (-3, 4, -4, 3)
764762

765763
axins = zoomed_inset_axes(ax, zoom=2, loc="upper left")

0 commit comments

Comments
 (0)