Skip to content

Commit 848d9fc

Browse files
authored
Merge pull request #11997 from timhoffm/axesgrid_example_cleanup
Cleanup some axes_grid1 examples
2 parents ab6f6f9 + d1ae685 commit 848d9fc

File tree

5 files changed

+135
-138
lines changed

5 files changed

+135
-138
lines changed

examples/axes_grid1/demo_axes_grid.py

+19-27
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ def demo_simple_grid(fig):
3030
)
3131

3232
Z, extent = get_demo_image()
33-
for i in range(4):
34-
im = grid[i].imshow(Z, extent=extent, interpolation="nearest")
33+
for ax in grid:
34+
im = ax.imshow(Z, extent=extent, interpolation="nearest")
3535

3636
# This only affects axes in first column and second row as share_all =
3737
# False.
@@ -53,8 +53,8 @@ def demo_grid_with_single_cbar(fig):
5353
)
5454

5555
Z, extent = get_demo_image()
56-
for i in range(4):
57-
im = grid[i].imshow(Z, extent=extent, interpolation="nearest")
56+
for ax in grid:
57+
im = ax.imshow(Z, extent=extent, interpolation="nearest")
5858
grid.cbar_axes[0].colorbar(im)
5959

6060
for cax in grid.cbar_axes:
@@ -69,7 +69,6 @@ def demo_grid_with_each_cbar(fig):
6969
"""
7070
A grid of 2x2 images. Each image has its own colorbar.
7171
"""
72-
7372
grid = ImageGrid(fig, 143, # similar to subplot(143)
7473
nrows_ncols=(2, 2),
7574
axes_pad=0.1,
@@ -81,11 +80,9 @@ def demo_grid_with_each_cbar(fig):
8180
cbar_pad="2%",
8281
)
8382
Z, extent = get_demo_image()
84-
for i in range(4):
85-
im = grid[i].imshow(Z, extent=extent, interpolation="nearest")
86-
grid.cbar_axes[i].colorbar(im)
87-
88-
for cax in grid.cbar_axes:
83+
for ax, cax in zip(grid, grid.cbar_axes):
84+
im = ax.imshow(Z, extent=extent, interpolation="nearest")
85+
cax.colorbar(im)
8986
cax.toggle_label(False)
9087

9188
# This affects all axes because we set share_all = True.
@@ -97,7 +94,6 @@ def demo_grid_with_each_cbar_labelled(fig):
9794
"""
9895
A grid of 2x2 images. Each image has its own colorbar.
9996
"""
100-
10197
grid = ImageGrid(fig, 144, # similar to subplot(144)
10298
nrows_ncols=(2, 2),
10399
axes_pad=(0.45, 0.15),
@@ -112,27 +108,23 @@ def demo_grid_with_each_cbar_labelled(fig):
112108

113109
# Use a different colorbar range every time
114110
limits = ((0, 1), (-2, 2), (-1.7, 1.4), (-1.5, 1))
115-
for i in range(4):
116-
im = grid[i].imshow(Z, extent=extent, interpolation="nearest",
117-
vmin=limits[i][0], vmax=limits[i][1])
118-
grid.cbar_axes[i].colorbar(im)
119-
120-
for i, cax in enumerate(grid.cbar_axes):
121-
cax.set_yticks((limits[i][0], limits[i][1]))
111+
for ax, cax, vlim in zip(grid, grid.cbar_axes, limits):
112+
im = ax.imshow(Z, extent=extent, interpolation="nearest",
113+
vmin=vlim[0], vmax=vlim[1])
114+
cax.colorbar(im)
115+
cax.set_yticks((vlim[0], vlim[1]))
122116

123117
# This affects all axes because we set share_all = True.
124118
grid.axes_llc.set_xticks([-2, 0, 2])
125119
grid.axes_llc.set_yticks([-2, 0, 2])
126120

127121

128-
if 1:
129-
F = plt.figure(1, (10.5, 2.5))
130-
131-
F.subplots_adjust(left=0.05, right=0.95)
122+
fig = plt.figure(figsize=(10.5, 2.5))
123+
fig.subplots_adjust(left=0.05, right=0.95)
132124

133-
demo_simple_grid(F)
134-
demo_grid_with_single_cbar(F)
135-
demo_grid_with_each_cbar(F)
136-
demo_grid_with_each_cbar_labelled(F)
125+
demo_simple_grid(fig)
126+
demo_grid_with_single_cbar(fig)
127+
demo_grid_with_each_cbar(fig)
128+
demo_grid_with_each_cbar_labelled(fig)
137129

138-
plt.show()
130+
plt.show()

examples/axes_grid1/demo_axes_grid2.py

+83-85
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@
55
66
Grid of images with shared xaxis and yaxis.
77
"""
8+
import numpy as np
9+
810
import matplotlib.pyplot as plt
911
from mpl_toolkits.axes_grid1 import ImageGrid
10-
import numpy as np
12+
import matplotlib.colors
1113

1214

1315
def get_demo_image():
@@ -30,90 +32,86 @@ def add_inner_title(ax, title, loc, size=None, **kwargs):
3032
at.txt._text.set_path_effects([withStroke(foreground="w", linewidth=3)])
3133
return at
3234

33-
if 1:
34-
F = plt.figure(1, (6, 6))
35-
F.clf()
36-
37-
# prepare images
38-
Z, extent = get_demo_image()
39-
ZS = [Z[i::3, :] for i in range(3)]
40-
extent = extent[0], extent[1]/3., extent[2], extent[3]
41-
42-
# demo 1 : colorbar at each axes
43-
44-
grid = ImageGrid(F, 211, # similar to subplot(111)
45-
nrows_ncols=(1, 3),
46-
direction="row",
47-
axes_pad=0.05,
48-
add_all=True,
49-
label_mode="1",
50-
share_all=True,
51-
cbar_location="top",
52-
cbar_mode="each",
53-
cbar_size="7%",
54-
cbar_pad="1%",
55-
)
56-
57-
for ax, z in zip(grid, ZS):
58-
im = ax.imshow(
59-
z, origin="lower", extent=extent, interpolation="nearest")
60-
ax.cax.colorbar(im)
61-
62-
for ax, im_title in zip(grid, ["Image 1", "Image 2", "Image 3"]):
63-
t = add_inner_title(ax, im_title, loc='lower left')
64-
t.patch.set_alpha(0.5)
65-
66-
for ax, z in zip(grid, ZS):
67-
ax.cax.toggle_label(True)
68-
#axis = ax.cax.axis[ax.cax.orientation]
69-
#axis.label.set_text("counts s$^{-1}$")
70-
#axis.label.set_size(10)
71-
#axis.major_ticklabels.set_size(6)
72-
73-
# changing the colorbar ticks
74-
grid[1].cax.set_xticks([-1, 0, 1])
75-
grid[2].cax.set_xticks([-1, 0, 1])
76-
77-
grid[0].set_xticks([-2, 0])
78-
grid[0].set_yticks([-2, 0, 2])
79-
80-
# demo 2 : shared colorbar
81-
82-
grid2 = ImageGrid(F, 212,
83-
nrows_ncols=(1, 3),
84-
direction="row",
85-
axes_pad=0.05,
86-
add_all=True,
87-
label_mode="1",
88-
share_all=True,
89-
cbar_location="right",
90-
cbar_mode="single",
91-
cbar_size="10%",
92-
cbar_pad=0.05,
93-
)
94-
95-
grid2[0].set_xlabel("X")
96-
grid2[0].set_ylabel("Y")
97-
98-
vmax, vmin = np.max(ZS), np.min(ZS)
99-
import matplotlib.colors
100-
norm = matplotlib.colors.Normalize(vmax=vmax, vmin=vmin)
101-
102-
for ax, z in zip(grid2, ZS):
103-
im = ax.imshow(z, norm=norm,
104-
origin="lower", extent=extent,
105-
interpolation="nearest")
106-
107-
# With cbar_mode="single", cax attribute of all axes are identical.
108-
ax.cax.colorbar(im)
109-
ax.cax.toggle_label(True)
11035

111-
for ax, im_title in zip(grid2, ["(a)", "(b)", "(c)"]):
112-
t = add_inner_title(ax, im_title, loc='upper left')
113-
t.patch.set_ec("none")
114-
t.patch.set_alpha(0.5)
36+
fig = plt.figure(figsize=(6, 6))
37+
38+
# Prepare images
39+
Z, extent = get_demo_image()
40+
ZS = [Z[i::3, :] for i in range(3)]
41+
extent = extent[0], extent[1]/3., extent[2], extent[3]
42+
43+
# *** Demo 1: colorbar at each axes ***
44+
grid = ImageGrid(fig, 211, # similar to subplot(211)
45+
nrows_ncols=(1, 3),
46+
direction="row",
47+
axes_pad=0.05,
48+
add_all=True,
49+
label_mode="1",
50+
share_all=True,
51+
cbar_location="top",
52+
cbar_mode="each",
53+
cbar_size="7%",
54+
cbar_pad="1%",
55+
)
56+
57+
for ax, z in zip(grid, ZS):
58+
im = ax.imshow(
59+
z, origin="lower", extent=extent, interpolation="nearest")
60+
ax.cax.colorbar(im)
11561

116-
grid2[0].set_xticks([-2, 0])
117-
grid2[0].set_yticks([-2, 0, 2])
62+
for ax, im_title in zip(grid, ["Image 1", "Image 2", "Image 3"]):
63+
t = add_inner_title(ax, im_title, loc='lower left')
64+
t.patch.set_alpha(0.5)
11865

119-
plt.show()
66+
for ax, z in zip(grid, ZS):
67+
ax.cax.toggle_label(True)
68+
#axis = ax.cax.axis[ax.cax.orientation]
69+
#axis.label.set_text("counts s$^{-1}$")
70+
#axis.label.set_size(10)
71+
#axis.major_ticklabels.set_size(6)
72+
73+
# Changing the colorbar ticks
74+
grid[1].cax.set_xticks([-1, 0, 1])
75+
grid[2].cax.set_xticks([-1, 0, 1])
76+
77+
grid[0].set_xticks([-2, 0])
78+
grid[0].set_yticks([-2, 0, 2])
79+
80+
# *** Demo 2: shared colorbar ***
81+
grid2 = ImageGrid(fig, 212,
82+
nrows_ncols=(1, 3),
83+
direction="row",
84+
axes_pad=0.05,
85+
add_all=True,
86+
label_mode="1",
87+
share_all=True,
88+
cbar_location="right",
89+
cbar_mode="single",
90+
cbar_size="10%",
91+
cbar_pad=0.05,
92+
)
93+
94+
grid2[0].set_xlabel("X")
95+
grid2[0].set_ylabel("Y")
96+
97+
vmax, vmin = np.max(ZS), np.min(ZS)
98+
norm = matplotlib.colors.Normalize(vmax=vmax, vmin=vmin)
99+
100+
for ax, z in zip(grid2, ZS):
101+
im = ax.imshow(z, norm=norm,
102+
origin="lower", extent=extent,
103+
interpolation="nearest")
104+
105+
# With cbar_mode="single", cax attribute of all axes are identical.
106+
ax.cax.colorbar(im)
107+
ax.cax.toggle_label(True)
108+
109+
for ax, im_title in zip(grid2, ["(a)", "(b)", "(c)"]):
110+
t = add_inner_title(ax, im_title, loc='upper left')
111+
t.patch.set_ec("none")
112+
t.patch.set_alpha(0.5)
113+
114+
grid2[0].set_xticks([-2, 0])
115+
grid2[0].set_yticks([-2, 0, 2])
116+
117+
plt.show()

examples/axes_grid1/demo_edge_colorbar.py

+9-10
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
Demo Edge Colorbar
44
==================
55
6+
This example shows how to use one common colorbar for each row or column
7+
of an image grid.
68
"""
79
import matplotlib.pyplot as plt
810
from mpl_toolkits.axes_grid1 import AxesGrid
@@ -21,7 +23,7 @@ def demo_bottom_cbar(fig):
2123
"""
2224
A grid of 2x2 images with a colorbar for each column.
2325
"""
24-
grid = AxesGrid(fig, 121, # similar to subplot(132)
26+
grid = AxesGrid(fig, 121, # similar to subplot(121)
2527
nrows_ncols=(2, 2),
2628
axes_pad=0.10,
2729
share_all=True,
@@ -54,8 +56,7 @@ def demo_right_cbar(fig):
5456
"""
5557
A grid of 2x2 images. Each row has its own colorbar.
5658
"""
57-
58-
grid = AxesGrid(F, 122, # similar to subplot(122)
59+
grid = AxesGrid(fig, 122, # similar to subplot(122)
5960
nrows_ncols=(2, 2),
6061
axes_pad=0.10,
6162
label_mode="1",
@@ -82,12 +83,10 @@ def demo_right_cbar(fig):
8283
grid.axes_llc.set_yticks([-2, 0, 2])
8384

8485

85-
if 1:
86-
F = plt.figure(1, (5.5, 2.5))
87-
88-
F.subplots_adjust(left=0.05, right=0.93)
86+
fig = plt.figure(figsize=(5.5, 2.5))
87+
fig.subplots_adjust(left=0.05, right=0.93)
8988

90-
demo_bottom_cbar(F)
91-
demo_right_cbar(F)
89+
demo_bottom_cbar(fig)
90+
demo_right_cbar(fig)
9291

93-
plt.show()
92+
plt.show()
+13-7
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,28 @@
11
"""
2-
===============
3-
Simple Axesgrid
4-
===============
2+
================
3+
Simple ImageGrid
4+
================
55
6+
Align multiple images using `~mpl_toolkits.axes_grid1.axes_grid.ImageGrid`.
67
"""
8+
79
import matplotlib.pyplot as plt
810
from mpl_toolkits.axes_grid1 import ImageGrid
911
import numpy as np
1012

11-
im = np.arange(100).reshape((10, 10))
13+
im1 = np.arange(100).reshape((10, 10))
14+
im2 = im1.T
15+
im3 = np.flipud(im1)
16+
im4 = np.fliplr(im2)
1217

13-
fig = plt.figure(1, (4., 4.))
18+
fig = plt.figure(figsize=(4., 4.))
1419
grid = ImageGrid(fig, 111, # similar to subplot(111)
1520
nrows_ncols=(2, 2), # creates 2x2 grid of axes
1621
axes_pad=0.1, # pad between axes in inch.
1722
)
1823

19-
for i in range(4):
20-
grid[i].imshow(im) # The AxesGrid object work as a list of axes.
24+
for ax, im in zip(grid, [im1, im2, im3, im4]):
25+
# Iterating over the grid returns the Axes.
26+
ax.imshow(im)
2127

2228
plt.show()

examples/axes_grid1/simple_axesgrid2.py

+11-9
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
"""
2-
================
3-
Simple Axesgrid2
4-
================
2+
==================
3+
Simple ImageGrid 2
4+
==================
55
6+
Align multiple images of different sizes using
7+
`~mpl_toolkits.axes_grid1.axes_grid.ImageGrid`.
68
"""
79
import matplotlib.pyplot as plt
810
from mpl_toolkits.axes_grid1 import ImageGrid
@@ -16,8 +18,9 @@ def get_demo_image():
1618
# z is a numpy array of 15x15
1719
return z, (-3, 4, -4, 3)
1820

19-
F = plt.figure(1, (5.5, 3.5))
20-
grid = ImageGrid(F, 111, # similar to subplot(111)
21+
22+
fig = plt.figure(figsize=(5.5, 3.5))
23+
grid = ImageGrid(fig, 111, # similar to subplot(111)
2124
nrows_ncols=(1, 3),
2225
axes_pad=0.1,
2326
add_all=True,
@@ -30,9 +33,8 @@ def get_demo_image():
3033
im2 = Z[:, :10]
3134
im3 = Z[:, 10:]
3235
vmin, vmax = Z.min(), Z.max()
33-
for i, im in enumerate([im1, im2, im3]):
34-
ax = grid[i]
35-
ax.imshow(im, origin="lower", vmin=vmin,
36-
vmax=vmax, interpolation="nearest")
36+
for ax, im in zip(grid, [im1, im2, im3]):
37+
ax.imshow(im, origin="lower", vmin=vmin, vmax=vmax,
38+
interpolation="nearest")
3739

3840
plt.show()

0 commit comments

Comments
 (0)