Skip to content

Commit e8f6813

Browse files
authored
Merge pull request #13636 from meeseeksmachine/auto-backport-of-pr-13621-on-v3.1.x
Backport PR #13621 on branch v3.1.x (Remove `asfileobj=False` from a bunch of examples loading sample_data.)
2 parents bcfcea9 + 655b588 commit e8f6813

File tree

7 files changed

+30
-35
lines changed

7 files changed

+30
-35
lines changed

examples/frontpage/3D.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
====================
55
66
This example reproduces the frontpage 3D example.
7-
87
"""
98
# This import registers the 3D projection, but is otherwise unused.
109
from mpl_toolkits.mplot3d import Axes3D # noqa: F401 unused import
@@ -15,8 +14,8 @@
1514
import matplotlib.pyplot as plt
1615
import numpy as np
1716

18-
filename = cbook.get_sample_data('jacksboro_fault_dem.npz', asfileobj=False)
19-
with np.load(filename) as dem:
17+
with cbook.get_sample_data('jacksboro_fault_dem.npz') as file, \
18+
np.load(file) as dem:
2019
z = dem['elevation']
2120
nrows, ncols = z.shape
2221
x = np.linspace(dem['xmin'], dem['xmax'], ncols)

examples/images_contours_and_fields/image_demo.py

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

5252
w, h = 512, 512
5353

54-
with cbook.get_sample_data('ct.raw.gz', asfileobj=True) as datafile:
54+
with cbook.get_sample_data('ct.raw.gz') as datafile:
5555
s = datafile.read()
5656
A = np.frombuffer(s, np.uint16).astype(float).reshape((w, h))
5757
A /= A.max()

examples/images_contours_and_fields/shading_example.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,28 @@
11
"""
22
===============
3-
Shading Example
3+
Shading example
44
===============
55
6-
Example showing how to make shaded relief plots
7-
like Mathematica
8-
(http://reference.wolfram.com/mathematica/ref/ReliefPlot.html)
9-
or Generic Mapping Tools
10-
(https://gmt.soest.hawaii.edu/)
6+
Example showing how to make shaded relief plots like Mathematica_ or
7+
`Generic Mapping Tools`_.
8+
9+
.. _Mathematica: http://reference.wolfram.com/mathematica/ref/ReliefPlot.html
10+
.. _Generic Mapping Tools: https://gmt.soest.hawaii.edu/
1111
"""
12+
1213
import numpy as np
14+
from matplotlib import cbook
1315
import matplotlib.pyplot as plt
1416
from matplotlib.colors import LightSource
15-
from matplotlib.cbook import get_sample_data
1617

1718

1819
def main():
1920
# Test data
2021
x, y = np.mgrid[-5:5:0.05, -5:5:0.05]
2122
z = 5 * (np.sqrt(x**2 + y**2) + np.sin(x**2 + y**2))
2223

23-
filename = get_sample_data('jacksboro_fault_dem.npz', asfileobj=False)
24-
with np.load(filename) as dem:
24+
with cbook.get_sample_data('jacksboro_fault_dem.npz') as file, \
25+
np.load(file) as dem:
2526
elev = dem['elevation']
2627

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

examples/images_contours_and_fields/watermark_image.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,21 @@
55
66
Using a PNG file as a watermark.
77
"""
8+
89
import numpy as np
910
import matplotlib.cbook as cbook
1011
import matplotlib.image as image
1112
import matplotlib.pyplot as plt
1213

13-
# Fixing random state for reproducibility
14-
np.random.seed(19680801)
15-
1614

17-
datafile = cbook.get_sample_data('logo2.png', asfileobj=False)
18-
print('loading %s' % datafile)
19-
im = image.imread(datafile)
20-
im[:, :, -1] = 0.5 # set the alpha channel
15+
with cbook.get_sample_data('logo2.png') as file:
16+
im = image.imread(file)
2117

2218
fig, ax = plt.subplots()
2319

24-
ax.plot(np.random.rand(20), '-o', ms=20, lw=2, alpha=0.7, mfc='orange')
20+
ax.plot(np.sin(10 * np.linspace(0, 1)), '-o', ms=20, alpha=0.7, mfc='orange')
2521
ax.grid()
26-
fig.figimage(im, 10, 10, zorder=3)
22+
fig.figimage(im, 10, 10, zorder=3, alpha=.5)
2723

2824
plt.show()
2925

examples/mplot3d/custom_shaded_3d_surface.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
import numpy as np
1717

1818
# Load and format data
19-
filename = cbook.get_sample_data('jacksboro_fault_dem.npz', asfileobj=False)
20-
with np.load(filename) as dem:
19+
with cbook.get_sample_data('jacksboro_fault_dem.npz') as file, \
20+
np.load(file) as dem:
2121
z = dem['elevation']
2222
nrows, ncols = z.shape
2323
x = np.linspace(dem['xmin'], dem['xmax'], ncols)

examples/text_labels_and_annotations/demo_annotation_box.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,8 @@
7777
ax.add_artist(ab)
7878

7979
# Annotate the 2nd position with another image (a Grace Hopper portrait)
80-
fn = get_sample_data("grace_hopper.png", asfileobj=False)
81-
arr_img = plt.imread(fn, format='png')
80+
with get_sample_data("grace_hopper.png") as file:
81+
arr_img = plt.imread(file, format='png')
8282

8383
imagebox = OffsetImage(arr_img, zoom=0.2)
8484
imagebox.image.axes = ax

lib/matplotlib/tests/test_colors.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -391,15 +391,14 @@ def test_colors_no_float():
391391
extensions=['png'])
392392
def test_light_source_topo_surface():
393393
"""Shades a DEM using different v.e.'s and blend modes."""
394-
fname = cbook.get_sample_data('jacksboro_fault_dem.npz', asfileobj=False)
395-
dem = np.load(fname)
396-
elev = dem['elevation']
397-
# Get the true cellsize in meters for accurate vertical exaggeration
398-
# Convert from decimal degrees to meters
399-
dx, dy = dem['dx'], dem['dy']
400-
dx = 111320.0 * dx * np.cos(dem['ymin'])
401-
dy = 111320.0 * dy
402-
dem.close()
394+
with cbook.get_sample_data('jacksboro_fault_dem.npz') as file, \
395+
np.load(file) as dem:
396+
elev = dem['elevation']
397+
dx, dy = dem['dx'], dem['dy']
398+
# Get the true cellsize in meters for accurate vertical exaggeration
399+
# Convert from decimal degrees to meters
400+
dx = 111320.0 * dx * np.cos(dem['ymin'])
401+
dy = 111320.0 * dy
403402

404403
ls = mcolors.LightSource(315, 45)
405404
cmap = cm.gist_earth

0 commit comments

Comments
 (0)