Skip to content

Backport PR #13621 on branch v3.1.x (Remove asfileobj=False from a bunch of examples loading sample_data.) #13636

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions examples/frontpage/3D.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
====================

This example reproduces the frontpage 3D example.

"""
# This import registers the 3D projection, but is otherwise unused.
from mpl_toolkits.mplot3d import Axes3D # noqa: F401 unused import
Expand All @@ -15,8 +14,8 @@
import matplotlib.pyplot as plt
import numpy as np

filename = cbook.get_sample_data('jacksboro_fault_dem.npz', asfileobj=False)
with np.load(filename) as dem:
with cbook.get_sample_data('jacksboro_fault_dem.npz') as file, \
np.load(file) as dem:
z = dem['elevation']
nrows, ncols = z.shape
x = np.linspace(dem['xmin'], dem['xmax'], ncols)
Expand Down
2 changes: 1 addition & 1 deletion examples/images_contours_and_fields/image_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@

w, h = 512, 512

with cbook.get_sample_data('ct.raw.gz', asfileobj=True) as datafile:
with cbook.get_sample_data('ct.raw.gz') as datafile:
s = datafile.read()
A = np.frombuffer(s, np.uint16).astype(float).reshape((w, h))
A /= A.max()
Expand Down
19 changes: 10 additions & 9 deletions examples/images_contours_and_fields/shading_example.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
"""
===============
Shading Example
Shading example
===============
Example showing how to make shaded relief plots
like Mathematica
(http://reference.wolfram.com/mathematica/ref/ReliefPlot.html)
or Generic Mapping Tools
(https://gmt.soest.hawaii.edu/)
Example showing how to make shaded relief plots like Mathematica_ or
`Generic Mapping Tools`_.
.. _Mathematica: http://reference.wolfram.com/mathematica/ref/ReliefPlot.html
.. _Generic Mapping Tools: https://gmt.soest.hawaii.edu/
"""

import numpy as np
from matplotlib import cbook
import matplotlib.pyplot as plt
from matplotlib.colors import LightSource
from matplotlib.cbook import get_sample_data


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

filename = get_sample_data('jacksboro_fault_dem.npz', asfileobj=False)
with np.load(filename) as dem:
with cbook.get_sample_data('jacksboro_fault_dem.npz') as file, \
np.load(file) as dem:
elev = dem['elevation']

fig = compare(z, plt.cm.copper)
Expand Down
14 changes: 5 additions & 9 deletions examples/images_contours_and_fields/watermark_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,21 @@
Using a PNG file as a watermark.
"""

import numpy as np
import matplotlib.cbook as cbook
import matplotlib.image as image
import matplotlib.pyplot as plt

# Fixing random state for reproducibility
np.random.seed(19680801)


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

fig, ax = plt.subplots()

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

plt.show()

Expand Down
4 changes: 2 additions & 2 deletions examples/mplot3d/custom_shaded_3d_surface.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
import numpy as np

# Load and format data
filename = cbook.get_sample_data('jacksboro_fault_dem.npz', asfileobj=False)
with np.load(filename) as dem:
with cbook.get_sample_data('jacksboro_fault_dem.npz') as file, \
np.load(file) as dem:
z = dem['elevation']
nrows, ncols = z.shape
x = np.linspace(dem['xmin'], dem['xmax'], ncols)
Expand Down
4 changes: 2 additions & 2 deletions examples/text_labels_and_annotations/demo_annotation_box.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@
ax.add_artist(ab)

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

imagebox = OffsetImage(arr_img, zoom=0.2)
imagebox.image.axes = ax
Expand Down
17 changes: 8 additions & 9 deletions lib/matplotlib/tests/test_colors.py
Original file line number Diff line number Diff line change
Expand Up @@ -391,15 +391,14 @@ def test_colors_no_float():
extensions=['png'])
def test_light_source_topo_surface():
"""Shades a DEM using different v.e.'s and blend modes."""
fname = cbook.get_sample_data('jacksboro_fault_dem.npz', asfileobj=False)
dem = np.load(fname)
elev = dem['elevation']
# Get the true cellsize in meters for accurate vertical exaggeration
# Convert from decimal degrees to meters
dx, dy = dem['dx'], dem['dy']
dx = 111320.0 * dx * np.cos(dem['ymin'])
dy = 111320.0 * dy
dem.close()
with cbook.get_sample_data('jacksboro_fault_dem.npz') as file, \
np.load(file) as dem:
elev = dem['elevation']
dx, dy = dem['dx'], dem['dy']
# Get the true cellsize in meters for accurate vertical exaggeration
# Convert from decimal degrees to meters
dx = 111320.0 * dx * np.cos(dem['ymin'])
dy = 111320.0 * dy

ls = mcolors.LightSource(315, 45)
cmap = cm.gist_earth
Expand Down