From 655b588bd9dff86993b4f5a24c44c81f2465b022 Mon Sep 17 00:00:00 2001 From: Jody Klymak Date: Fri, 8 Mar 2019 11:13:38 -0800 Subject: [PATCH] Backport PR #13621: Remove `asfileobj=False` from a bunch of examples loading sample_data. --- examples/frontpage/3D.py | 5 ++--- .../images_contours_and_fields/image_demo.py | 2 +- .../shading_example.py | 19 ++++++++++--------- .../watermark_image.py | 14 +++++--------- examples/mplot3d/custom_shaded_3d_surface.py | 4 ++-- .../demo_annotation_box.py | 4 ++-- lib/matplotlib/tests/test_colors.py | 17 ++++++++--------- 7 files changed, 30 insertions(+), 35 deletions(-) diff --git a/examples/frontpage/3D.py b/examples/frontpage/3D.py index ee93026ed06e..8c2da0c96f04 100644 --- a/examples/frontpage/3D.py +++ b/examples/frontpage/3D.py @@ -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 @@ -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) diff --git a/examples/images_contours_and_fields/image_demo.py b/examples/images_contours_and_fields/image_demo.py index ba5ac2920f5b..58ae0c9e2903 100644 --- a/examples/images_contours_and_fields/image_demo.py +++ b/examples/images_contours_and_fields/image_demo.py @@ -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() diff --git a/examples/images_contours_and_fields/shading_example.py b/examples/images_contours_and_fields/shading_example.py index f97aa8efe12f..95f7e59f5a7c 100644 --- a/examples/images_contours_and_fields/shading_example.py +++ b/examples/images_contours_and_fields/shading_example.py @@ -1,18 +1,19 @@ """ =============== -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(): @@ -20,8 +21,8 @@ def main(): 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) diff --git a/examples/images_contours_and_fields/watermark_image.py b/examples/images_contours_and_fields/watermark_image.py index 91cf43302668..38a3aa0d9656 100644 --- a/examples/images_contours_and_fields/watermark_image.py +++ b/examples/images_contours_and_fields/watermark_image.py @@ -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() diff --git a/examples/mplot3d/custom_shaded_3d_surface.py b/examples/mplot3d/custom_shaded_3d_surface.py index 366658856470..e165c59ef28f 100644 --- a/examples/mplot3d/custom_shaded_3d_surface.py +++ b/examples/mplot3d/custom_shaded_3d_surface.py @@ -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) diff --git a/examples/text_labels_and_annotations/demo_annotation_box.py b/examples/text_labels_and_annotations/demo_annotation_box.py index e4576d3e6559..9fa69dd77623 100644 --- a/examples/text_labels_and_annotations/demo_annotation_box.py +++ b/examples/text_labels_and_annotations/demo_annotation_box.py @@ -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 diff --git a/lib/matplotlib/tests/test_colors.py b/lib/matplotlib/tests/test_colors.py index 2517cbd989b8..107162bb0a22 100644 --- a/lib/matplotlib/tests/test_colors.py +++ b/lib/matplotlib/tests/test_colors.py @@ -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 pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy