Backport PR #13621 on branch v3.1.x (Remove `asfileobj=False` from a bunch of examples loading sample_data.) by meeseeksmachine · Pull Request #13636 · matplotlib/matplotlib · GitHub
Skip to content
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
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