From d1ae68571b20c86eda94f74dc035b5c22cc8e5bd Mon Sep 17 00:00:00 2001 From: Tim Hoffmann <2836374+timhoffm@users.noreply.github.com> Date: Sun, 2 Sep 2018 02:58:04 +0200 Subject: [PATCH] Cleanup some axes_grid1 examples --- examples/axes_grid1/demo_axes_grid.py | 46 +++--- examples/axes_grid1/demo_axes_grid2.py | 168 +++++++++++----------- examples/axes_grid1/demo_edge_colorbar.py | 19 ++- examples/axes_grid1/simple_axesgrid.py | 20 ++- examples/axes_grid1/simple_axesgrid2.py | 20 +-- 5 files changed, 135 insertions(+), 138 deletions(-) diff --git a/examples/axes_grid1/demo_axes_grid.py b/examples/axes_grid1/demo_axes_grid.py index fafac4f1ea49..b21b288c355e 100644 --- a/examples/axes_grid1/demo_axes_grid.py +++ b/examples/axes_grid1/demo_axes_grid.py @@ -30,8 +30,8 @@ def demo_simple_grid(fig): ) Z, extent = get_demo_image() - for i in range(4): - im = grid[i].imshow(Z, extent=extent, interpolation="nearest") + for ax in grid: + im = ax.imshow(Z, extent=extent, interpolation="nearest") # This only affects axes in first column and second row as share_all = # False. @@ -53,8 +53,8 @@ def demo_grid_with_single_cbar(fig): ) Z, extent = get_demo_image() - for i in range(4): - im = grid[i].imshow(Z, extent=extent, interpolation="nearest") + for ax in grid: + im = ax.imshow(Z, extent=extent, interpolation="nearest") grid.cbar_axes[0].colorbar(im) for cax in grid.cbar_axes: @@ -69,7 +69,6 @@ def demo_grid_with_each_cbar(fig): """ A grid of 2x2 images. Each image has its own colorbar. """ - grid = ImageGrid(fig, 143, # similar to subplot(143) nrows_ncols=(2, 2), axes_pad=0.1, @@ -81,11 +80,9 @@ def demo_grid_with_each_cbar(fig): cbar_pad="2%", ) Z, extent = get_demo_image() - for i in range(4): - im = grid[i].imshow(Z, extent=extent, interpolation="nearest") - grid.cbar_axes[i].colorbar(im) - - for cax in grid.cbar_axes: + for ax, cax in zip(grid, grid.cbar_axes): + im = ax.imshow(Z, extent=extent, interpolation="nearest") + cax.colorbar(im) cax.toggle_label(False) # This affects all axes because we set share_all = True. @@ -97,7 +94,6 @@ def demo_grid_with_each_cbar_labelled(fig): """ A grid of 2x2 images. Each image has its own colorbar. """ - grid = ImageGrid(fig, 144, # similar to subplot(144) nrows_ncols=(2, 2), axes_pad=(0.45, 0.15), @@ -112,27 +108,23 @@ def demo_grid_with_each_cbar_labelled(fig): # Use a different colorbar range every time limits = ((0, 1), (-2, 2), (-1.7, 1.4), (-1.5, 1)) - for i in range(4): - im = grid[i].imshow(Z, extent=extent, interpolation="nearest", - vmin=limits[i][0], vmax=limits[i][1]) - grid.cbar_axes[i].colorbar(im) - - for i, cax in enumerate(grid.cbar_axes): - cax.set_yticks((limits[i][0], limits[i][1])) + for ax, cax, vlim in zip(grid, grid.cbar_axes, limits): + im = ax.imshow(Z, extent=extent, interpolation="nearest", + vmin=vlim[0], vmax=vlim[1]) + cax.colorbar(im) + cax.set_yticks((vlim[0], vlim[1])) # This affects all axes because we set share_all = True. grid.axes_llc.set_xticks([-2, 0, 2]) grid.axes_llc.set_yticks([-2, 0, 2]) -if 1: - F = plt.figure(1, (10.5, 2.5)) - - F.subplots_adjust(left=0.05, right=0.95) +fig = plt.figure(figsize=(10.5, 2.5)) +fig.subplots_adjust(left=0.05, right=0.95) - demo_simple_grid(F) - demo_grid_with_single_cbar(F) - demo_grid_with_each_cbar(F) - demo_grid_with_each_cbar_labelled(F) +demo_simple_grid(fig) +demo_grid_with_single_cbar(fig) +demo_grid_with_each_cbar(fig) +demo_grid_with_each_cbar_labelled(fig) - plt.show() +plt.show() diff --git a/examples/axes_grid1/demo_axes_grid2.py b/examples/axes_grid1/demo_axes_grid2.py index ed1af8529b98..c1159b342583 100644 --- a/examples/axes_grid1/demo_axes_grid2.py +++ b/examples/axes_grid1/demo_axes_grid2.py @@ -5,9 +5,11 @@ Grid of images with shared xaxis and yaxis. """ +import numpy as np + import matplotlib.pyplot as plt from mpl_toolkits.axes_grid1 import ImageGrid -import numpy as np +import matplotlib.colors def get_demo_image(): @@ -30,90 +32,86 @@ def add_inner_title(ax, title, loc, size=None, **kwargs): at.txt._text.set_path_effects([withStroke(foreground="w", linewidth=3)]) return at -if 1: - F = plt.figure(1, (6, 6)) - F.clf() - - # prepare images - Z, extent = get_demo_image() - ZS = [Z[i::3, :] for i in range(3)] - extent = extent[0], extent[1]/3., extent[2], extent[3] - - # demo 1 : colorbar at each axes - - grid = ImageGrid(F, 211, # similar to subplot(111) - nrows_ncols=(1, 3), - direction="row", - axes_pad=0.05, - add_all=True, - label_mode="1", - share_all=True, - cbar_location="top", - cbar_mode="each", - cbar_size="7%", - cbar_pad="1%", - ) - - for ax, z in zip(grid, ZS): - im = ax.imshow( - z, origin="lower", extent=extent, interpolation="nearest") - ax.cax.colorbar(im) - - for ax, im_title in zip(grid, ["Image 1", "Image 2", "Image 3"]): - t = add_inner_title(ax, im_title, loc='lower left') - t.patch.set_alpha(0.5) - - for ax, z in zip(grid, ZS): - ax.cax.toggle_label(True) - #axis = ax.cax.axis[ax.cax.orientation] - #axis.label.set_text("counts s$^{-1}$") - #axis.label.set_size(10) - #axis.major_ticklabels.set_size(6) - - # changing the colorbar ticks - grid[1].cax.set_xticks([-1, 0, 1]) - grid[2].cax.set_xticks([-1, 0, 1]) - - grid[0].set_xticks([-2, 0]) - grid[0].set_yticks([-2, 0, 2]) - - # demo 2 : shared colorbar - - grid2 = ImageGrid(F, 212, - nrows_ncols=(1, 3), - direction="row", - axes_pad=0.05, - add_all=True, - label_mode="1", - share_all=True, - cbar_location="right", - cbar_mode="single", - cbar_size="10%", - cbar_pad=0.05, - ) - - grid2[0].set_xlabel("X") - grid2[0].set_ylabel("Y") - - vmax, vmin = np.max(ZS), np.min(ZS) - import matplotlib.colors - norm = matplotlib.colors.Normalize(vmax=vmax, vmin=vmin) - - for ax, z in zip(grid2, ZS): - im = ax.imshow(z, norm=norm, - origin="lower", extent=extent, - interpolation="nearest") - - # With cbar_mode="single", cax attribute of all axes are identical. - ax.cax.colorbar(im) - ax.cax.toggle_label(True) - for ax, im_title in zip(grid2, ["(a)", "(b)", "(c)"]): - t = add_inner_title(ax, im_title, loc='upper left') - t.patch.set_ec("none") - t.patch.set_alpha(0.5) +fig = plt.figure(figsize=(6, 6)) + +# Prepare images +Z, extent = get_demo_image() +ZS = [Z[i::3, :] for i in range(3)] +extent = extent[0], extent[1]/3., extent[2], extent[3] + +# *** Demo 1: colorbar at each axes *** +grid = ImageGrid(fig, 211, # similar to subplot(211) + nrows_ncols=(1, 3), + direction="row", + axes_pad=0.05, + add_all=True, + label_mode="1", + share_all=True, + cbar_location="top", + cbar_mode="each", + cbar_size="7%", + cbar_pad="1%", + ) + +for ax, z in zip(grid, ZS): + im = ax.imshow( + z, origin="lower", extent=extent, interpolation="nearest") + ax.cax.colorbar(im) - grid2[0].set_xticks([-2, 0]) - grid2[0].set_yticks([-2, 0, 2]) +for ax, im_title in zip(grid, ["Image 1", "Image 2", "Image 3"]): + t = add_inner_title(ax, im_title, loc='lower left') + t.patch.set_alpha(0.5) - plt.show() +for ax, z in zip(grid, ZS): + ax.cax.toggle_label(True) + #axis = ax.cax.axis[ax.cax.orientation] + #axis.label.set_text("counts s$^{-1}$") + #axis.label.set_size(10) + #axis.major_ticklabels.set_size(6) + +# Changing the colorbar ticks +grid[1].cax.set_xticks([-1, 0, 1]) +grid[2].cax.set_xticks([-1, 0, 1]) + +grid[0].set_xticks([-2, 0]) +grid[0].set_yticks([-2, 0, 2]) + +# *** Demo 2: shared colorbar *** +grid2 = ImageGrid(fig, 212, + nrows_ncols=(1, 3), + direction="row", + axes_pad=0.05, + add_all=True, + label_mode="1", + share_all=True, + cbar_location="right", + cbar_mode="single", + cbar_size="10%", + cbar_pad=0.05, + ) + +grid2[0].set_xlabel("X") +grid2[0].set_ylabel("Y") + +vmax, vmin = np.max(ZS), np.min(ZS) +norm = matplotlib.colors.Normalize(vmax=vmax, vmin=vmin) + +for ax, z in zip(grid2, ZS): + im = ax.imshow(z, norm=norm, + origin="lower", extent=extent, + interpolation="nearest") + +# With cbar_mode="single", cax attribute of all axes are identical. +ax.cax.colorbar(im) +ax.cax.toggle_label(True) + +for ax, im_title in zip(grid2, ["(a)", "(b)", "(c)"]): + t = add_inner_title(ax, im_title, loc='upper left') + t.patch.set_ec("none") + t.patch.set_alpha(0.5) + +grid2[0].set_xticks([-2, 0]) +grid2[0].set_yticks([-2, 0, 2]) + +plt.show() diff --git a/examples/axes_grid1/demo_edge_colorbar.py b/examples/axes_grid1/demo_edge_colorbar.py index b7540b261226..313790a9edfc 100644 --- a/examples/axes_grid1/demo_edge_colorbar.py +++ b/examples/axes_grid1/demo_edge_colorbar.py @@ -3,6 +3,8 @@ Demo Edge Colorbar ================== +This example shows how to use one common colorbar for each row or column +of an image grid. """ import matplotlib.pyplot as plt from mpl_toolkits.axes_grid1 import AxesGrid @@ -21,7 +23,7 @@ def demo_bottom_cbar(fig): """ A grid of 2x2 images with a colorbar for each column. """ - grid = AxesGrid(fig, 121, # similar to subplot(132) + grid = AxesGrid(fig, 121, # similar to subplot(121) nrows_ncols=(2, 2), axes_pad=0.10, share_all=True, @@ -54,8 +56,7 @@ def demo_right_cbar(fig): """ A grid of 2x2 images. Each row has its own colorbar. """ - - grid = AxesGrid(F, 122, # similar to subplot(122) + grid = AxesGrid(fig, 122, # similar to subplot(122) nrows_ncols=(2, 2), axes_pad=0.10, label_mode="1", @@ -82,12 +83,10 @@ def demo_right_cbar(fig): grid.axes_llc.set_yticks([-2, 0, 2]) -if 1: - F = plt.figure(1, (5.5, 2.5)) - - F.subplots_adjust(left=0.05, right=0.93) +fig = plt.figure(figsize=(5.5, 2.5)) +fig.subplots_adjust(left=0.05, right=0.93) - demo_bottom_cbar(F) - demo_right_cbar(F) +demo_bottom_cbar(fig) +demo_right_cbar(fig) - plt.show() +plt.show() diff --git a/examples/axes_grid1/simple_axesgrid.py b/examples/axes_grid1/simple_axesgrid.py index 9641f633be82..fdd94cd32244 100644 --- a/examples/axes_grid1/simple_axesgrid.py +++ b/examples/axes_grid1/simple_axesgrid.py @@ -1,22 +1,28 @@ """ -=============== -Simple Axesgrid -=============== +================ +Simple ImageGrid +================ +Align multiple images using `~mpl_toolkits.axes_grid1.axes_grid.ImageGrid`. """ + import matplotlib.pyplot as plt from mpl_toolkits.axes_grid1 import ImageGrid import numpy as np -im = np.arange(100).reshape((10, 10)) +im1 = np.arange(100).reshape((10, 10)) +im2 = im1.T +im3 = np.flipud(im1) +im4 = np.fliplr(im2) -fig = plt.figure(1, (4., 4.)) +fig = plt.figure(figsize=(4., 4.)) grid = ImageGrid(fig, 111, # similar to subplot(111) nrows_ncols=(2, 2), # creates 2x2 grid of axes axes_pad=0.1, # pad between axes in inch. ) -for i in range(4): - grid[i].imshow(im) # The AxesGrid object work as a list of axes. +for ax, im in zip(grid, [im1, im2, im3, im4]): + # Iterating over the grid returns the Axes. + ax.imshow(im) plt.show() diff --git a/examples/axes_grid1/simple_axesgrid2.py b/examples/axes_grid1/simple_axesgrid2.py index 51783b58dc3c..e07ff9c6a118 100644 --- a/examples/axes_grid1/simple_axesgrid2.py +++ b/examples/axes_grid1/simple_axesgrid2.py @@ -1,8 +1,10 @@ """ -================ -Simple Axesgrid2 -================ +================== +Simple ImageGrid 2 +================== +Align multiple images of different sizes using +`~mpl_toolkits.axes_grid1.axes_grid.ImageGrid`. """ import matplotlib.pyplot as plt from mpl_toolkits.axes_grid1 import ImageGrid @@ -16,8 +18,9 @@ def get_demo_image(): # z is a numpy array of 15x15 return z, (-3, 4, -4, 3) -F = plt.figure(1, (5.5, 3.5)) -grid = ImageGrid(F, 111, # similar to subplot(111) + +fig = plt.figure(figsize=(5.5, 3.5)) +grid = ImageGrid(fig, 111, # similar to subplot(111) nrows_ncols=(1, 3), axes_pad=0.1, add_all=True, @@ -30,9 +33,8 @@ def get_demo_image(): im2 = Z[:, :10] im3 = Z[:, 10:] vmin, vmax = Z.min(), Z.max() -for i, im in enumerate([im1, im2, im3]): - ax = grid[i] - ax.imshow(im, origin="lower", vmin=vmin, - vmax=vmax, interpolation="nearest") +for ax, im in zip(grid, [im1, im2, im3]): + ax.imshow(im, origin="lower", vmin=vmin, vmax=vmax, + interpolation="nearest") plt.show()