Skip to content

Commit 4388154

Browse files
committed
Make get_sample_data autoload npy/npz files.
This makes examples shorter and more to the point by simplifying the data-load part. A small (standard) backcompat-dance is needed.
1 parent fb7cf79 commit 4388154

File tree

26 files changed

+111
-155
lines changed

26 files changed

+111
-155
lines changed

doc/api/next_api_changes/behaviour.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,10 @@ instead.
7373

7474
`.Text.update_from` now copies usetex state from the source Text
7575
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
76+
77+
`.cbook.get_sample_data` auto-loads numpy arrays
78+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
79+
When `.cbook.get_sample_data` is used to load a npy or npz file and the
80+
keyword-only parameter ``np_load`` is True, the file is automatically loaded
81+
using `numpy.load`. ``np_load`` defaults to False for backwards compatibility,
82+
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: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,15 @@
55
66
RGBAxes to show RGB composite images.
77
"""
8+
89
import numpy as np
10+
from matplotlib import cbook
911
import matplotlib.pyplot as plt
10-
1112
from mpl_toolkits.axes_grid1.axes_rgb import make_rgb_axes, RGBAxes
1213

1314

14-
def get_demo_image():
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
def get_rgb():
23-
Z, extent = get_demo_image()
24-
16+
Z = cbook.get_sample_data("axes_grid/bivariate_normal.npy", np_load=True)
2517
Z[Z < 0] = 0.
2618
Z = Z / Z.max()
2719

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/axes_grid1/simple_rgb.py

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,14 @@
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.axes_rgb import RGBAxes
1011

1112

12-
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)
17-
# z is a numpy array of 15x15
18-
return z, (-3, 4, -4, 3)
19-
20-
2113
def get_rgb():
22-
Z, extent = get_demo_image()
23-
14+
Z = cbook.get_sample_data("axes_grid/bivariate_normal.npy", np_load=True)
2415
Z[Z < 0] = 0.
2516
Z = Z / Z.max()
2617

0 commit comments

Comments
 (0)