8000 Cleanup demo_axes_grid{,2}. by anntzer · Pull Request #25885 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

Cleanup demo_axes_grid{,2}. #25885

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
merged 1 commit into from
May 14, 2023
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
23 changes: 8 additions & 15 deletions galleries/examples/axes_grid1/demo_axes_grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,11 @@
from matplotlib import cbook
from mpl_toolkits.axes_grid1 import ImageGrid

fig = plt.figure(figsize=(10.5, 2.5))
Z = cbook.get_sample_data("axes_grid/bivariate_normal.npy") # 15x15 array
extent = (-3, 4, -4, 3)


fig = plt.figure(figsize=(10.5, 2.5))
fig.subplots_adjust(left=0.05, right=0.95)


# A grid of 2x2 images with 0.05 inch pad between images and only the
# lower-left axes is labeled.
grid = ImageGrid(
Expand All @@ -27,11 +24,10 @@
for ax in grid:
ax.imshow(Z, extent=extent)
# This only affects axes in first column and second row as share_all=False.
grid.axes_llc.set_xticks([-2, 0, 2])
grid.axes_llc.set_yticks([-2, 0, 2])
grid.axes_llc.set(xticks=[-2, 0, 2], yticks=[-2, 0, 2])


# A grid of 2x2 images with a single colorbar
# A grid of 2x2 images with a single colorbar.
grid = ImageGrid(
fig, 142, # similar to fig.add_subplot(142).
nrows_ncols=(2, 2), axes_pad=0.0, label_mode="L", share_all=True,
Expand All @@ -42,8 +38,7 @@
for cax in grid.cbar_axes:
cax.toggle_label(False)
# This affects all axes as share_all = True.
grid.axes_llc.set_xticks([-2, 0, 2])
grid.axes_llc.set_yticks([-2, 0, 2])
grid.axes_llc.set(xticks=[-2, 0, 2], yticks=[-2, 0, 2])


# A grid of 2x2 images. Each image has its own colorbar.
Expand All @@ -55,9 +50,8 @@
im = ax.imshow(Z, extent=extent)
cax.colorbar(im)
cax.toggle_label(False)
# 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])
# This affects all axes as share_all = True.
grid.axes_llc.set(xticks=[-2, 0, 2], yticks=[-2, 0, 2])


# A grid of 2x2 images. Each image has its own colorbar.
Expand All @@ -71,9 +65,8 @@
im = ax.imshow(Z, extent=extent, vmin=vlim[0], vmax=vlim[1])
cb = cax.colorbar(im)
cb.set_ticks((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])
# This affects all axes as share_all = True.
grid.axes_llc.set(xticks=[-2, 0, 2], yticks=[-2, 0, 2])


plt.show()
56 changes: 13 additions & 43 deletions galleries/examples/axes_grid1/demo_axes_grid2.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import numpy as np

from matplotlib import cbook
import matplotlib.colors
from mpl_toolkits.axes_grid1 import ImageGrid


Expand All @@ -35,16 +34,11 @@ def add_inner_title(ax, title, loc, **kwargs):
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),
axes_pad=0.05,
label_mode="1",
share_all=True,
cbar_location="top",
cbar_mode="each",
cbar_size="7%",
cbar_pad="1%",
)
grid = ImageGrid(
# 211 = at the position of fig.add_subplot(211)
fig, 211, nrows_ncols=(1, 3), axes_pad=0.05, label_mode="1", share_all=True,
cbar_location="top", cbar_mode="each", cbar_size="7%", cbar_pad="1%")
grid[0].set(xticks=[-2, 0], yticks=[-2, 0, 2])

for i, (ax, z) in enumerate(zip(grid, ZS)):
im = ax.imshow(z, origin="lower", extent=extent)
Expand All @@ -54,46 +48,22 @@ def add_inner_title(ax, title, loc, **kwargs):
cb.set_ticks([-1, 0, 1])

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)

grid[0].set_xticks([-2, 0])
grid[0].set_yticks([-2, 0, 2])
add_inner_title(ax, im_title, loc='lower left')

# *** Demo 2: shared colorbar ***
grid2 = ImageGrid(fig, 212,
nrows_ncols=(1, 3),
axes_pad=0.05,
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)
grid2 = ImageGrid(
fig, 212, nrows_ncols=(1, 3), axes_pad=0.05, label_mode="1", share_all=True,
cbar_location="right", cbar_mode="single", cbar_size="10%", cbar_pad=0.05)
grid2[0].set(xlabel="X", ylabel="Y", xticks=[-2, 0], yticks=[-2, 0, 2])

clim = (np.min(ZS), np.max(ZS))
for ax, z in zip(grid2, ZS):
im = ax.imshow(z, norm=norm, origin="lower", extent=extent)
im = ax.imshow(z, clim=clim, origin="lower", extent=extent)

# 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])
add_inner_title(ax, im_title, loc='upper left')

plt.show()
0