8000 Cleanup demo_axes_grid{,2}. · matplotlib/matplotlib@de4e43c · GitHub
[go: up one dir, main page]

Skip to content

Commit de4e43c

Browse files
committed
Cleanup demo_axes_grid{,2}.
- No point in tweaking the patch of the inner_titles because the patch is set to invisible (frameon=False). - No need to toggle the colorbar labels on (they are on by default). - Use clim=(min, max) instead of constructing an explicit Normalize instance. - Group axes tweaking in set() calls. - Remove unnecessary subplots_adjust. - Some reformatting.
1 parent b61bb0b commit de4e43c

File tree

2 files changed

+21
-58
lines changed

2 files changed

+21
-58
lines changed

galleries/examples/axes_grid1/demo_axes_grid.py

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,11 @@
1111
from matplotlib import cbook
1212
from mpl_toolkits.axes_grid1 import ImageGrid
1313

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

1718

18-
fig = plt.figure(figsize=(10.5, 2.5))
19-
fig.subplots_adjust(left=0.05, right=0.95)
20-
21-
2219
# A grid of 2x2 images with 0.05 inch pad between images and only the
2320
# lower-left axes is labeled.
2421
grid = ImageGrid(
@@ -27,11 +24,10 @@
2724
for ax in grid:
2825
ax.imshow(Z, extent=extent)
2926
# This only affects axes in first column and second row as share_all=False.
30-
grid.axes_llc.set_xticks([-2, 0, 2])
31-
grid.axes_llc.set_yticks([-2, 0, 2])
27+
grid.axes_llc.set(xticks=[-2, 0, 2], yticks=[-2, 0, 2])
3228

3329

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

4843

4944
# A grid of 2x2 images. Each image has its own colorbar.
@@ -55,9 +50,8 @@
5550
im = ax.imshow(Z, extent=extent)
5651
cax.colorbar(im)
5752
cax.toggle_label(False)
58-
# This affects all axes because we set share_all = True.
59-
grid.axes_llc.set_xticks([-2, 0, 2])
60-
grid.axes_llc.set_yticks([-2, 0, 2])
53+
# This affects all axes as share_all = True.
54+
grid.axes_llc.set(xticks=[-2, 0, 2], yticks=[-2, 0, 2])
6155

6256

6357
# A grid of 2x2 images. Each image has its own colorbar.
@@ -71,9 +65,8 @@
7165
im = ax.imshow(Z, extent=extent, vmin=vlim[0], vmax=vlim[1])
7266
cb = cax.colorbar(im)
7367
cb.set_ticks((vlim[0], vlim[1]))
74-
# This affects all axes because we set share_all = True.
75-
grid.axes_llc.set_xticks([-2, 0, 2])
76-
grid.axes_llc.set_yticks([-2, 0, 2])
68+
# This affects all axes as share_all = True.
69+
grid.axes_llc.set(xticks=[-2, 0, 2], yticks=[-2, 0, 2])
7770

7871

7972
plt.show()

galleries/examples/axes_grid1/demo_axes_grid2.py

Lines changed: 13 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import numpy as np
1111

1212
from matplotlib import cbook
13-
import matplotlib.colors
1413
from mpl_toolkits.axes_grid1 import ImageGrid
1514

1615

@@ -35,16 +34,11 @@ def add_inner_title(ax, title, loc, **kwargs):
3534
extent = extent[0], extent[1]/3., extent[2], extent[3]
3635

3736
# *** Demo 1: colorbar at each axes ***
38-
grid = ImageGrid(fig, 211, # similar to subplot(211)
39-
nrows_ncols=(1, 3),
40-
axes_pad=0.05,
41-
label_mode="1",
42-
share_all=True,
43-
cbar_location="top",
44-
cbar_mode="each",
45-
cbar_size="7%",
46-
cbar_pad="1%",
47-
)
37+
grid = ImageGrid(
38+
# 211 = at the position of fig.add_subplot(211)
39+
fig, 211, nrows_ncols=(1, 3), axes_pad=0.05, label_mode="1", share_all=True,
40+
cbar_location="top", cbar_mode="each", cbar_size="7%", cbar_pad="1%")
41+
grid[0].set(xticks=[-2, 0], yticks=[-2, 0, 2])
4842

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

5650
for ax, im_title in zip(grid, ["Image 1", "Image 2", "Image 3"]):
57-
t = add_inner_title(ax, im_title, loc='lower left')
58-
t.patch.set_alpha(0.5)
59-
60-
for ax, z in zip(grid, ZS):
61-
ax.cax.toggle_label(True)
62-
63-
grid[0].set_xticks([-2, 0])
64-
grid[0].set_yticks([-2, 0, 2])
51+
add_inner_title(ax, im_title, loc='lower left')
6552

6653
# *** Demo 2: shared colorbar ***
67-
grid2 = ImageGrid(fig, 212,
68-
nrows_ncols=(1, 3),
69-
axes_pad=0.05,
70-
label_mode="1",
71-
share_all=True,
72-
cbar_location="right",
73-
cbar_mode="single",
74-
cbar_size="10%",
75-
cbar_pad=0.05,
76-
)
77-
78-
grid2[0].set_xlabel("X")
79-
grid2[0].set_ylabel("Y")
80-
81-
vmax, vmin = np.max(ZS), np.min(ZS)
82-
norm = matplotlib.colors.Normalize(vmax=vmax, vmin=vmin)
54+
grid2 = ImageGrid(
55+
fig, 212, nrows_ncols=(1, 3), axes_pad=0.05, label_mode="1", share_all=True,
56+
cbar_location="right", cbar_mode="single", cbar_size="10%", cbar_pad=0.05)
57+
grid2[0].set(xlabel="X", ylabel="Y", xticks=[-2, 0], yticks=[-2, 0, 2])
8358

59+
clim = (np.min(ZS), np.max(ZS))
8460
for ax, z in zip(grid2, ZS):
85-
im = ax.imshow(z, norm=norm, origin="lower", extent=extent)
61+
im = ax.imshow(z, clim=clim, origin="lower", extent=extent)
8662

8763
# With cbar_mode="single", cax attribute of all axes are identical.
8864
ax.cax.colorbar(im)
89-
ax.cax.toggle_label(True)
9065

9166
for ax, im_title in zip(grid2, ["(a)", "(b)", "(c)"]):
92-
t = add_inner_title(ax, im_title, loc='upper left')
93-
t.patch.set_ec("none")
94-
t.patch.set_alpha(0.5)
95-
96-
grid2[0].set_xticks([-2, 0])
97-
grid2[0].set_yticks([-2, 0, 2])
67+
add_inner_title(ax, im_title, loc='upper left')
9868

9969
plt.show()

0 commit comments

Comments
 (0)
0