Skip to content

Commit bdbc7df

Browse files
committed
Avoid leaking file descriptors in tests and examples
1 parent b3e2e3d commit bdbc7df

File tree

3 files changed

+22
-17
lines changed

3 files changed

+22
-17
lines changed

examples/mplot3d/custom_shaded_3d_surface.py

+7-6
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@
88
import matplotlib.pyplot as plt
99
import numpy as np
1010

11-
dem = np.load(cbook.get_sample_data('jacksboro_fault_dem.npz'))
12-
z = dem['elevation']
13-
nrows, ncols = z.shape
14-
x = np.linspace(dem['xmin'], dem['xmax'], ncols)
15-
y = np.linspace(dem['ymin'], dem['ymax'], nrows)
16-
x, y = np.meshgrid(x, y)
11+
filename = cbook.get_sample_data('jacksboro_fault_dem.npz', asfileobj=False)
12+
with np.load(filename) as dem:
13+
z = dem['elevation']
14+
nrows, ncols = z.shape
15+
x = np.linspace(dem['xmin'], dem['xmax'], ncols)
16+
y = np.linspace(dem['ymin'], dem['ymax'], nrows)
17+
x, y = np.meshgrid(x, y)
1718

1819
region = np.s_[5:50, 5:50]
1920
x, y, z = x[region], y[region], z[region]

examples/pylab_examples/shading_example.py

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import numpy as np
22
import matplotlib.pyplot as plt
33
from matplotlib.colors import LightSource
4-
from matplotlib import cbook
4+
from matplotlib.cbook import get_sample_data
55

66
# Example showing how to make shaded relief plots
77
# like Mathematica
@@ -13,11 +13,15 @@ def main():
1313
# Test data
1414
x, y = np.mgrid[-5:5:0.05, -5:5:0.05]
1515
z = 5 * (np.sqrt(x**2 + y**2) + np.sin(x**2 + y**2))
16+
17+
filename = get_sample_data('jacksboro_fault_dem.npz', asfileobj=False)
18+
with np.load(filename) as dem:
19+
elev = dem['elevation']
20+
1621
fig = compare(z, plt.cm.copper)
1722
fig.suptitle('HSV Blending Looks Best with Smooth Surfaces', y=0.95)
1823

19-
dem = np.load(cbook.get_sample_data('jacksboro_fault_dem.npz'))
20-
fig = compare(dem['elevation'], plt.cm.gist_earth, ve=0.05)
24+
fig = compare(elev, plt.cm.gist_earth, ve=0.05)
2125
fig.suptitle('Overlay Blending Looks Best with Rough Surfaces', y=0.95)
2226

2327
plt.show()

lib/matplotlib/tests/test_colors.py

+8-8
Original file line numberDiff line numberDiff line change
@@ -234,16 +234,16 @@ def gray_from_float_rgba():
234234
extensions=['png'])
235235
def test_light_source_topo_surface():
236236
"""Shades a DEM using different v.e.'s and blend modes."""
237-
dem = np.load(cbook.get_sample_data('jacksboro_fault_dem.npz'))
238-
239-
# Get the true cellsize in meters for accurate vertical exaggeration
240-
# Convert from decimal degrees to meters
241-
dx, dy = dem['dx'], dem['dy']
242-
dx = 111320.0 * dx * np.cos(dem['ymin'])
243-
dy = 111320.0 * dy
237+
fname = cbook.get_sample_data('jacksboro_fault_dem.npz', asfileobj=False)
238+
with np.load(fname) as dem:
239+
elev = dem['elevation']
240+
# Get the true cellsize in meters for accurate vertical exaggeration
241+
# Convert from decimal degrees to meters
242+
dx, dy = dem['dx'], dem['dy']
243+
dx = 111320.0 * dx * np.cos(dem['ymin'])
244+
dy = 111320.0 * dy
244245

245246
ls = mcolors.LightSource(315, 45)
246-
elev = dem['elevation']
247247
cmap = cm.gist_earth
248248

249249
fig, axes = plt.subplots(nrows=3, ncols=3)

0 commit comments

Comments
 (0)