| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -294,3 +294,10 @@ FT_LOAD_DEFAULT, etc. The old synonyms (respectively "either", "native", | |||
| 294 | 294 | "auto", and "none") are still supported, but their use is discouraged. To get | |
| 295 | 295 | normalized values, use `.backend_agg.get_hinting_flag`, which returns integer | |
| 296 | 296 | flag values. | |
| 297 | + | ||
| 298 | + `.cbook.get_sample_data` auto-loads numpy arrays | ||
| 299 | + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
| 300 | + When `.cbook.get_sample_data` is used to load a npy or npz file and the | ||
| 301 | + keyword-only parameter ``np_load`` is True, the file is automatically loaded | ||
| 302 | + using `numpy.load`. ``np_load`` defaults to False for backwards compatibility, | ||
| 303 | + but will become True in a later release. | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -6,14 +6,13 @@ | |||
| 6 | 6 | Axes divider to calculate location of axes and | |
| 7 | 7 | create a divider for them using existing axes instances. | |
| 8 | 8 | """ | |
| 9 | + | ||
| 10 | + from matplotlib import cbook | ||
| 9 | 11 | import matplotlib.pyplot as plt | |
| 10 | 12 | ||
| 11 | 13 | ||
| 12 | 14 | def get_demo_image(): | |
| 13 | - import numpy as np | ||
| 14 | - from matplotlib.cbook import get_sample_data | ||
| 15 | - f = get_sample_data("axes_grid/bivariate_normal.npy", asfileobj=False) | ||
| 16 | - z = np.load(f) | ||
| 15 | + z = cbook.get_sample_data("axes_grid/bivariate_normal.npy", np_load=True) | ||
| 17 | 16 | # z is a numpy array of 15x15 | |
| 18 | 17 | return z, (-3, 4, -4, 3) | |
| 19 | 18 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -6,6 +6,7 @@ | |||
| 6 | 6 | Grid of 2x2 images with single or own colorbar. | |
| 7 | 7 | """ | |
| 8 | 8 | ||
| 9 | + from matplotlib import cbook | ||
| 9 | 10 | import matplotlib.pyplot as plt | |
| 10 | 11 | from mpl_toolkits.axes_grid1 import ImageGrid | |
| 11 | 12 | ||
@@ -14,10 +15,7 @@ | |||
| 14 | 15 | ||
| 15 | 16 | ||
| 16 | 17 | def get_demo_image(): | |
| 17 | - import numpy as np | ||
| 18 | - from matplotlib.cbook import get_sample_data | ||
| 19 | - f = get_sample_data("axes_grid/bivariate_normal.npy", asfileobj=False) | ||
| 20 | - z = np.load(f) | ||
| 18 | + z = cbook.get_sample_data("axes_grid/bivariate_normal.npy", np_load=True) | ||
| 21 | 19 | # z is a numpy array of 15x15 | |
| 22 | 20 | return z, (-3, 4, -4, 3) | |
| 23 | 21 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -7,23 +7,15 @@ | |||
| 7 | 7 | """ | |
| 8 | 8 | ||
| 9 | 9 | import numpy as np | |
| 10 | - | ||
| 10 | + from matplotlib import cbook | ||
| 11 | + import matplotlib.colors | ||
| 11 | 12 | import matplotlib.pyplot as plt | |
| 12 | 13 | from mpl_toolkits.axes_grid1 import ImageGrid | |
| 13 | - import matplotlib.colors | ||
| 14 | 14 | ||
| 15 | 15 | ||
| 16 | 16 | plt.rcParams["mpl_toolkits.legacy_colorbar"] = False | |
| 17 | 17 | ||
| 18 | 18 | ||
| 19 | - def get_demo_image(): | ||
| 20 | - from matplotlib.cbook import get_sample_data | ||
| 21 | - f = get_sample_data("axes_grid/bivariate_normal.npy", asfileobj=False) | ||
| 22 | - z = np.load(f) | ||
| 23 | - # z is a numpy array of 15x15 | ||
| 24 | - return z, (-3, 4, -4, 3) | ||
| 25 | - | ||
| 26 | - | ||
| 27 | 19 | def add_inner_title(ax, title, loc, **kwargs): | |
| 28 | 20 | from matplotlib.offsetbox import AnchoredText | |
| 29 | 21 | from matplotlib.patheffects import withStroke | |
@@ -39,7 +31,8 @@ def add_inner_title(ax, title, loc, **kwargs): | |||
| 39 | 31 | fig = plt.figure(figsize=(6, 6)) | |
| 40 | 32 | ||
| 41 | 33 | # Prepare images | |
| 42 | - Z, extent = get_demo_image() | ||
| 34 | + Z = cbook.get_sample_data("axes_grid/bivariate_normal.npy", np_load=True) | ||
| 35 | + extent = (-3, 4, -4, 3) | ||
| 43 | 36 | ZS = [Z[i::3, :] for i in range(3)] | |
| 44 | 37 | extent = extent[0], extent[1]/3., extent[2], extent[3] | |
| 45 | 38 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -13,9 +13,7 @@ | |||
| 13 | 13 | ||
| 14 | 14 | ||
| 15 | 15 | def get_rgb(): | |
| 16 | - f = cbook.get_sample_data("axes_grid/bivariate_normal.npy") | ||
| 17 | - Z = np.load(f) # 15x15 numpy array. | ||
| 18 | - | ||
| 16 | + Z = cbook.get_sample_data("axes_grid/bivariate_normal.npy", np_load=True) | ||
| 19 | 17 | Z[Z < 0] = 0. | |
| 20 | 18 | Z = Z / Z.max() | |
| 21 | 19 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -4,36 +4,25 @@ | |||
| 4 | 4 | =========================== | |
| 5 | 5 | ||
| 6 | 6 | """ | |
| 7 | - import matplotlib.pyplot as plt | ||
| 8 | 7 | ||
| 8 | + from matplotlib import cbook | ||
| 9 | + import matplotlib.pyplot as plt | ||
| 9 | 10 | from mpl_toolkits.axes_grid1.inset_locator import inset_axes, zoomed_inset_axes | |
| 10 | 11 | ||
| 11 | 12 | ||
| 12 | - def get_demo_image(): | ||
| 13 | - from matplotlib.cbook import get_sample_data | ||
| 14 | - import numpy as np | ||
| 15 | - f = get_sample_data("axes_grid/bivariate_normal.npy", asfileobj=False) | ||
| 16 | - z = np.load(f) | ||
| 17 | - # z is a numpy array of 15x15 | ||
| 18 | - return z, (-3, 4, -4, 3) | ||
| 19 | - | ||
| 20 | - | ||
| 21 | 13 | fig, ax = plt.subplots(figsize=[5, 4]) | |
| 22 | 14 | ||
| 23 | - Z, extent = get_demo_image() | ||
| 24 | - | ||
| 25 | - ax.set(aspect=1, | ||
| 26 | - xlim=(-15, 15), | ||
| 27 | - ylim=(-20, 5)) | ||
| 15 | + Z = cbook.get_sample_data("axes_grid/bivariate_normal.npy", np_load=True) | ||
| 16 | + extent = (-3, 4, -4, 3) | ||
| 28 | 17 | ||
| 18 | + ax.set(aspect=1, xlim=(-15, 15), ylim=(-20, 5)) | ||
| 29 | 19 | ||
| 30 | 20 | axins = zoomed_inset_axes(ax, zoom=2, loc='upper left') | |
| 31 | 21 | im = axins.imshow(Z, extent=extent, origin="lower") | |
| 32 | 22 | ||
| 33 | 23 | plt.xticks(visible=False) | |
| 34 | 24 | plt.yticks(visible=False) | |
| 35 | 25 | ||
| 36 | - | ||
| 37 | 26 | # colorbar | |
| 38 | 27 | cax = inset_axes(axins, | |
| 39 | 28 | width="5%", # width = 10% of parent_bbox width | |
@@ -43,7 +32,6 @@ def get_demo_image(): | |||
| 43 | 32 | bbox_transform=axins.transAxes, | |
| 44 | 33 | borderpad=0, | |
| 45 | 34 | ) | |
| 46 | - | ||
| 47 | 35 | fig.colorbar(im, cax=cax) | |
| 48 | 36 | ||
| 49 | 37 | plt.show() | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -7,6 +7,7 @@ | |||
| 7 | 7 | of an image grid. | |
| 8 | 8 | """ | |
| 9 | 9 | ||
| 10 | + from matplotlib import cbook | ||
| 10 | 11 | import matplotlib.pyplot as plt | |
| 11 | 12 | from mpl_toolkits.axes_grid1 import AxesGrid | |
| 12 | 13 | ||
@@ -15,10 +16,7 @@ | |||
| 15 | 16 | ||
| 16 | 17 | ||
| 17 | 18 | def get_demo_image(): | |
| 18 | - import numpy as np | ||
| 19 | - from matplotlib.cbook import get_sample_data | ||
| 20 | - f = get_sample_data("axes_grid/bivariate_normal.npy", asfileobj=False) | ||
| 21 | - z = np.load(f) | ||
| 19 | + z = cbook.get_sample_data("axes_grid/bivariate_normal.npy", np_load=True) | ||
| 22 | 20 | # z is a numpy array of 15x15 | |
| 23 | 21 | return z, (-3, 4, -4, 3) | |
| 24 | 22 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -9,19 +9,16 @@ | |||
| 9 | 9 | created via `~.mark_inset`. | |
| 10 | 10 | """ | |
| 11 | 11 | ||
| 12 | + from matplotlib import cbook | ||
| 12 | 13 | import matplotlib.pyplot as plt | |
| 13 | - | ||
| 14 | 14 | from mpl_toolkits.axes_grid1.inset_locator import zoomed_inset_axes, mark_inset | |
| 15 | 15 | from mpl_toolkits.axes_grid1.anchored_artists import AnchoredSizeBar | |
| 16 | 16 | ||
| 17 | 17 | import numpy as np | |
| 18 | 18 | ||
| 19 | 19 | ||
| 20 | 20 | def get_demo_image(): | |
| 21 | - from matplotlib.cbook import get_sample_data | ||
| 22 | - import numpy as np | ||
| 23 | - f = get_sample_data("axes_grid/bivariate_normal.npy", asfileobj=False) | ||
| 24 | - z = np.load(f) | ||
| 21 | + z = cbook.get_sample_data("axes_grid/bivariate_normal.npy", np_load=True) | ||
| 25 | 22 | # z is a numpy array of 15x15 | |
| 26 | 23 | return z, (-3, 4, -4, 3) | |
| 27 | 24 | ||
@@ -60,11 +57,9 @@ def add_sizebar(ax, size): | |||
| 60 | 57 | ny, nx = Z.shape | |
| 61 | 58 | Z2[30:30+ny, 30:30+nx] = Z | |
| 62 | 59 | ||
| 63 | - # extent = [-3, 4, -4, 3] | ||
| 64 | 60 | ax2.imshow(Z2, extent=extent, origin="lower") | |
| 65 | 61 | ||
| 66 | - | ||
| 67 | - axins2 = zoomed_inset_axes(ax2, 6, loc=1) # zoom = 6 | ||
| 62 | + axins2 = zoomed_inset_axes(ax2, zoom=6, loc=1) | ||
| 68 | 63 | axins2.imshow(Z2, extent=extent, origin="lower") | |
| 69 | 64 | ||
| 70 | 65 | # sub region of the original image | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -6,28 +6,21 @@ | |||
| 6 | 6 | Align multiple images of different sizes using | |
| 7 | 7 | `~mpl_toolkits.axes_grid1.axes_grid.ImageGrid`. | |
| 8 | 8 | """ | |
| 9 | + | ||
| 10 | + from matplotlib import cbook | ||
| 9 | 11 | import matplotlib.pyplot as plt | |
| 10 | 12 | from mpl_toolkits.axes_grid1 import ImageGrid | |
| 11 | 13 | ||
| 12 | 14 | ||
| 13 | - def get_demo_image(): | ||
| 14 | - import numpy as np | ||
| 15 | - from matplotlib.cbook import get_sample_data | ||
| 16 | - f = get_sample_data("axes_grid/bivariate_normal.npy", asfileobj=False) | ||
| 17 | - z = np.load(f) | ||
| 18 | - # z is a numpy array of 15x15 | ||
| 19 | - return z, (-3, 4, -4, 3) | ||
| 20 | - | ||
| 21 | - | ||
| 22 | 15 | fig = plt.figure(figsize=(5.5, 3.5)) | |
| 23 | 16 | grid = ImageGrid(fig, 111, # similar to subplot(111) | |
| 24 | 17 | nrows_ncols=(1, 3), | |
| 25 | 18 | axes_pad=0.1, | |
| 26 | 19 | label_mode="L", | |
| 27 | 20 | ) | |
| 28 | 21 | ||
| 29 | - Z, extent = get_demo_image() # demo image | ||
| 30 | - | ||
| 22 | + # demo image | ||
| 23 | + Z = cbook.get_sample_data("axes_grid/bivariate_normal.npy", np_load=True) | ||
| 31 | 24 | im1 = Z | |
| 32 | 25 | im2 = Z[:, :10] | |
| 33 | 26 | im3 = Z[:, 10:] | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -12,13 +12,12 @@ | |||
| 12 | 12 | import matplotlib.pyplot as plt | |
| 13 | 13 | import numpy as np | |
| 14 | 14 | ||
| 15 | - with cbook.get_sample_data('jacksboro_fault_dem.npz') as file, \ | ||
| 16 | - np.load(file) as dem: | ||
| 17 | - z = dem['elevation'] | ||
| 18 | - nrows, ncols = z.shape | ||
| 19 | - x = np.linspace(dem['xmin'], dem['xmax'], ncols) | ||
| 20 | - y = np.linspace(dem['ymin'], dem['ymax'], nrows) | ||
| 21 | - x, y = np.meshgrid(x, y) | ||
| 15 | + dem = cbook.get_sample_data('jacksboro_fault_dem.npz', np_load=True) | ||
| 16 | + z = dem['elevation'] | ||
| 17 | + nrows, ncols = z.shape | ||
| 18 | + x = np.linspace(dem['xmin'], dem['xmax'], ncols) | ||
| 19 | + y = np.linspace(dem['ymin'], dem['ymax'], nrows) | ||
| 20 | + x, y = np.meshgrid(x, y) | ||
| 22 | 21 | ||
| 23 | 22 | region = np.s_[5:50, 5:50] | |
| 24 | 23 | x, y, z = x[region], y[region], z[region] | |
| Back | FazBrowse Home | New Git URL |
0 commit comments