Skip to content

Commit a1c51a0

Browse files
authored
Merge pull request #15641 from anntzer/get_sample_data-npy
API: Make get_sample_data autoload npy/npz files.
2 parents 56d509f + d7d11a7 commit a1c51a0

File tree

24 files changed

+102
-130
lines changed

24 files changed

+102
-130
lines changed

doc/api/api_changes_3.3/behaviour.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,3 +294,10 @@ FT_LOAD_DEFAULT, etc. The old synonyms (respectively "either", "native",
294294
"auto", and "none") are still supported, but their use is discouraged. To get
295295
normalized values, use `.backend_agg.get_hinting_flag`, which returns integer
296296
flag values.
297+
298+
`.cbook.get_sample_data` auto-loads numpy arrays
299+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
300+
When `.cbook.get_sample_data` is used to load a npy or npz file and the
301+
keyword-only parameter ``np_load`` is True, the file is automatically loaded
302+
using `numpy.load`. ``np_load`` defaults to False for backwards compatibility,
303+
but will become True in a later release.

examples/axes_grid1/demo_axes_divider.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,13 @@
66
Axes divider to calculate location of axes and
77
create a divider for them using existing axes instances.
88
"""
9+
10+
from matplotlib import cbook
911
import matplotlib.pyplot as plt
1012

1113

1214
def get_demo_image():
13-
import numpy as np
14-
from matplotlib.cbook import get_sample_data
15-
f = get_sample_data("axes_grid/bivariate_normal.npy", asfileobj=False)
16-
z = np.load(f)
15+
z = cbook.get_sample_data("axes_grid/bivariate_normal.npy", np_load=True)
1716
# z is a numpy array of 15x15
1817
return z, (-3, 4, -4, 3)
1918

examples/axes_grid1/demo_axes_grid.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
Grid of 2x2 images with single or own colorbar.
77
"""
88

9+
from matplotlib import cbook
910
import matplotlib.pyplot as plt
1011
from mpl_toolkits.axes_grid1 import ImageGrid
1112

@@ -14,10 +15,7 @@
1415

1516

1617
def get_demo_image():
17-
import numpy as np
18-
from matplotlib.cbook import get_sample_data
19-
f = get_sample_data("axes_grid/bivariate_normal.npy", asfileobj=False)
20-
z = np.load(f)
18+
z = cbook.get_sample_data("axes_grid/bivariate_normal.npy", np_load=True)
2119
# z is a numpy array of 15x15
2220
return z, (-3, 4, -4, 3)
2321

examples/axes_grid1/demo_axes_grid2.py

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,15 @@
77
"""
88

99
import numpy as np
10-
10+
from matplotlib import cbook
11+
import matplotlib.colors
1112
import matplotlib.pyplot as plt
1213
from mpl_toolkits.axes_grid1 import ImageGrid
13-
import matplotlib.colors
1414

1515

1616
plt.rcParams["mpl_toolkits.legacy_colorbar"] = False
1717

1818

19-
def get_demo_image():
20-
from matplotlib.cbook import get_sample_data
21-
f = get_sample_data("axes_grid/bivariate_normal.npy", asfileobj=False)
22-
z = np.load(f)
23-
# z is a numpy array of 15x15
24-
return z, (-3, 4, -4, 3)
25-
26-
2719
def add_inner_title(ax, title, loc, **kwargs):
2820
from matplotlib.offsetbox import AnchoredText
2921
from matplotlib.patheffects import withStroke
@@ -39,7 +31,8 @@ def add_inner_title(ax, title, loc, **kwargs):
3931
fig = plt.figure(figsize=(6, 6))
4032

4133
# Prepare images
42-
Z, extent = get_demo_image()
34+
Z = cbook.get_sample_data("axes_grid/bivariate_normal.npy", np_load=True)
35+
extent = (-3, 4, -4, 3)
4336
ZS = [Z[i::3, :] for i in range(3)]
4437
extent = extent[0], extent[1]/3., extent[2], extent[3]
4538

examples/axes_grid1/demo_axes_rgb.py

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

1414

1515
def get_rgb():
16-
f = cbook.get_sample_data("axes_grid/bivariate_normal.npy")
17-
Z = np.load(f) # 15x15 numpy array.
18-
16+
Z = cbook.get_sample_data("axes_grid/bivariate_normal.npy", np_load=True)
1917
Z[Z < 0] = 0.
2018
Z = Z / Z.max()
2119

examples/axes_grid1/demo_colorbar_of_inset_axes.py

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,36 +4,25 @@
44
===========================
55
66
"""
7-
import matplotlib.pyplot as plt
87

8+
from matplotlib import cbook
9+
import matplotlib.pyplot as plt
910
from mpl_toolkits.axes_grid1.inset_locator import inset_axes, zoomed_inset_axes
1011

1112

12-
def get_demo_image():
13-
from matplotlib.cbook import get_sample_data
14-
import numpy as np
15-
f = get_sample_data("axes_grid/bivariate_normal.npy", asfileobj=False)
16-
z = np.load(f)
17-
# z is a numpy array of 15x15
18-
return z, (-3, 4, -4, 3)
19-
20-
2113
fig, ax = plt.subplots(figsize=[5, 4])
2214

23-
Z, extent = get_demo_image()
24-
25-
ax.set(aspect=1,
26-
xlim=(-15, 15),
27-
ylim=(-20, 5))
15+
Z = cbook.get_sample_data("axes_grid/bivariate_normal.npy", np_load=True)
16+
extent = (-3, 4, -4, 3)
2817

18+
ax.set(aspect=1, xlim=(-15, 15), ylim=(-20, 5))
2919

3020
axins = zoomed_inset_axes(ax, zoom=2, loc='upper left')
3121
im = axins.imshow(Z, extent=extent, origin="lower")
3222

3323
plt.xticks(visible=False)
3424
plt.yticks(visible=False)
3525

36-
3726
# colorbar
3827
cax = inset_axes(axins,
3928
width="5%", # width = 10% of parent_bbox width
@@ -43,7 +32,6 @@ def get_demo_image():
4332
bbox_transform=axins.transAxes,
4433
borderpad=0,
4534
)
46-
4735
fig.colorbar(im, cax=cax)
4836

4937
plt.show()

examples/axes_grid1/demo_edge_colorbar.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
of an image grid.
88
"""
99

10+
from matplotlib import cbook
1011
import matplotlib.pyplot as plt
1112
from mpl_toolkits.axes_grid1 import AxesGrid
1213

@@ -15,10 +16,7 @@
1516

1617

1718
def get_demo_image():
18-
import numpy as np
19-
from matplotlib.cbook import get_sample_data
20-
f = get_sample_data("axes_grid/bivariate_normal.npy", asfileobj=False)
21-
z = np.load(f)
19+
z = cbook.get_sample_data("axes_grid/bivariate_normal.npy", np_load=True)
2220
# z is a numpy array of 15x15
2321
return z, (-3, 4, -4, 3)
2422

examples/axes_grid1/inset_locator_demo2.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,16 @@
99
created via `~.mark_inset`.
1010
"""
1111

12+
from matplotlib import cbook
1213
import matplotlib.pyplot as plt
13-
1414
from mpl_toolkits.axes_grid1.inset_locator import zoomed_inset_axes, mark_inset
1515
from mpl_toolkits.axes_grid1.anchored_artists import AnchoredSizeBar
1616

1717
import numpy as np
1818

1919

2020
def get_demo_image():
21-
from matplotlib.cbook import get_sample_data
22-
import numpy as np
23-
f = get_sample_data("axes_grid/bivariate_normal.npy", asfileobj=False)
24-
z = np.load(f)
21+
z = cbook.get_sample_data("axes_grid/bivariate_normal.npy", np_load=True)
2522
# z is a numpy array of 15x15
2623
return z, (-3, 4, -4, 3)
2724

@@ -60,11 +57,9 @@ def add_sizebar(ax, size):
6057
ny, nx = Z.shape
6158
Z2[30:30+ny, 30:30+nx] = Z
6259

63-
# extent = [-3, 4, -4, 3]
6460
ax2.imshow(Z2, extent=extent, origin="lower")
6561

66-
67-
axins2 = zoomed_inset_axes(ax2, 6, loc=1) # zoom = 6
62+
axins2 = zoomed_inset_axes(ax2, zoom=6, loc=1)
6863
axins2.imshow(Z2, extent=extent, origin="lower")
6964

7065
# sub region of the original image

examples/axes_grid1/simple_axesgrid2.py

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,21 @@
66
Align multiple images of different sizes using
77
`~mpl_toolkits.axes_grid1.axes_grid.ImageGrid`.
88
"""
9+
10+
from matplotlib import cbook
911
import matplotlib.pyplot as plt
1012
from mpl_toolkits.axes_grid1 import ImageGrid
1113

1214

13-
def get_demo_image():
14-
import numpy as np
15-
from matplotlib.cbook import get_sample_data
16-
f = get_sample_data("axes_grid/bivariate_normal.npy", asfileobj=False)
17-
z = np.load(f)
18-
# z is a numpy array of 15x15
19-
return z, (-3, 4, -4, 3)
20-
21-
2215
fig = plt.figure(figsize=(5.5, 3.5))
2316
grid = ImageGrid(fig, 111, # similar to subplot(111)
2417
nrows_ncols=(1, 3),
2518
axes_pad=0.1,
2619
label_mode="L",
2720
)
2821

29-
Z, extent = get_demo_image() # demo image
30-
22+
# demo image
23+
Z = cbook.get_sample_data("axes_grid/bivariate_normal.npy", np_load=True)
3124
im1 = Z
3225
im2 = Z[:, :10]
3326
im3 = Z[:, 10:]

examples/frontpage/3D.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,12 @@
1212
import matplotlib.pyplot as plt
1313
import numpy as np
1414

15-
with cbook.get_sample_data('jacksboro_fault_dem.npz') as file, \
16-
np.load(file) as dem:
17-
z = dem['elevation']
18-
nrows, ncols = z.shape
19-
x = np.linspace(dem['xmin'], dem['xmax'], ncols)
20-
y = np.linspace(dem['ymin'], dem['ymax'], nrows)
21-
x, y = np.meshgrid(x, y)
15+
dem = cbook.get_sample_data('jacksboro_fault_dem.npz', np_load=True)
16+
z = dem['elevation']
17+
nrows, ncols = z.shape
18+
x = np.linspace(dem['xmin'], dem['xmax'], ncols)
19+
y = np.linspace(dem['ymin'], dem['ymax'], nrows)
20+
x, y = np.meshgrid(x, y)
2221

2322
region = np.s_[5:50, 5:50]
2423
x, y, z = x[region], y[region], z[region]

0 commit comments

Comments
 (0)