|
3 | 3 | Demo Axes Grid |
4 | 4 | ============== |
5 | 5 |
|
6 | | -Grid of 2x2 images with single or own colorbar. |
| 6 | +Grid of 2x2 images with a single colorbar or with one colorbar per axes. |
7 | 7 | """ |
8 | 8 |
|
9 | 9 | from matplotlib import cbook |
10 | 10 | import matplotlib.pyplot as plt |
11 | 11 | from mpl_toolkits.axes_grid1 import ImageGrid |
12 | 12 |
|
13 | 13 |
|
14 | | -def get_demo_image(): |
15 | | - z = cbook.get_sample_data("axes_grid/bivariate_normal.npy", np_load=True) |
16 | | - # z is a numpy array of 15x15 |
17 | | - return z, (-3, 4, -4, 3) |
18 | | - |
19 | | - |
20 | | -def demo_simple_grid(fig): |
21 | | - """ |
22 | | - A grid of 2x2 images with 0.05 inch pad between images and only |
23 | | - the lower-left axes is labeled. |
24 | | - """ |
25 | | - grid = ImageGrid(fig, 141, # similar to subplot(141) |
26 | | - nrows_ncols=(2, 2), |
27 | | - axes_pad=0.05, |
28 | | - label_mode="1", |
29 | | - ) |
30 | | - Z, extent = get_demo_image() |
31 | | - for ax in grid: |
32 | | - ax.imshow(Z, extent=extent) |
33 | | - # This only affects axes in first column and second row as share_all=False. |
34 | | - grid.axes_llc.set_xticks([-2, 0, 2]) |
35 | | - grid.axes_llc.set_yticks([-2, 0, 2]) |
36 | | - |
37 | | - |
38 | | -def demo_grid_with_single_cbar(fig): |
39 | | - """ |
40 | | - A grid of 2x2 images with a single colorbar |
41 | | - """ |
42 | | - grid = ImageGrid(fig, 142, # similar to subplot(142) |
43 | | - nrows_ncols=(2, 2), |
44 | | - axes_pad=0.0, |
45 | | - share_all=True, |
46 | | - label_mode="L", |
47 | | - cbar_location="top", |
48 | | - cbar_mode="single", |
49 | | - ) |
50 | | - |
51 | | - Z, extent = get_demo_image() |
52 | | - for ax in grid: |
53 | | - im = ax.imshow(Z, extent=extent) |
54 | | - grid.cbar_axes[0].colorbar(im) |
55 | | - |
56 | | - for cax in grid.cbar_axes: |
57 | | - cax.toggle_label(False) |
58 | | - |
59 | | - # This affects all axes as share_all = True. |
60 | | - grid.axes_llc.set_xticks([-2, 0, 2]) |
61 | | - grid.axes_llc.set_yticks([-2, 0, 2]) |
62 | | - |
63 | | - |
64 | | -def demo_grid_with_each_cbar(fig): |
65 | | - """ |
66 | | - A grid of 2x2 images. Each image has its own colorbar. |
67 | | - """ |
68 | | - grid = ImageGrid(fig, 143, # similar to subplot(143) |
69 | | - nrows_ncols=(2, 2), |
70 | | - axes_pad=0.1, |
71 | | - label_mode="1", |
72 | | - share_all=True, |
73 | | - cbar_location="top", |
74 | | - cbar_mode="each", |
75 | | - cbar_size="7%", |
76 | | - cbar_pad="2%", |
77 | | - ) |
78 | | - Z, extent = get_demo_image() |
79 | | - for ax, cax in zip(grid, grid.cbar_axes): |
80 | | - im = ax.imshow(Z, extent=extent) |
81 | | - cax.colorbar(im) |
82 | | - cax.toggle_label(False) |
83 | | - |
84 | | - # This affects all axes because we set share_all = True. |
85 | | - grid.axes_llc.set_xticks([-2, 0, 2]) |
86 | | - grid.axes_llc.set_yticks([-2, 0, 2]) |
87 | | - |
88 | | - |
89 | | -def demo_grid_with_each_cbar_labelled(fig): |
90 | | - """ |
91 | | - A grid of 2x2 images. Each image has its own colorbar. |
92 | | - """ |
93 | | - grid = ImageGrid(fig, 144, # similar to subplot(144) |
94 | | - nrows_ncols=(2, 2), |
95 | | - axes_pad=(0.45, 0.15), |
96 | | - label_mode="1", |
97 | | - share_all=True, |
98 | | - cbar_location="right", |
99 | | - cbar_mode="each", |
100 | | - cbar_size="7%", |
101 | | - cbar_pad="2%", |
102 | | - ) |
103 | | - Z, extent = get_demo_image() |
104 | | - |
105 | | - # Use a different colorbar range every time |
106 | | - limits = ((0, 1), (-2, 2), (-1.7, 1.4), (-1.5, 1)) |
107 | | - for ax, cax, vlim in zip(grid, grid.cbar_axes, limits): |
108 | | - im = ax.imshow(Z, extent=extent, vmin=vlim[0], vmax=vlim[1]) |
109 | | - cb = cax.colorbar(im) |
110 | | - cb.set_ticks((vlim[0], vlim[1])) |
111 | | - |
112 | | - # This affects all axes because we set share_all = True. |
113 | | - grid.axes_llc.set_xticks([-2, 0, 2]) |
114 | | - grid.axes_llc.set_yticks([-2, 0, 2]) |
| 14 | +Z = cbook.get_sample_data( # (15, 15) array |
| 15 | + "axes_grid/bivariate_normal.npy", np_load=True) |
| 16 | +extent = (-3, 4, -4, 3) |
115 | 17 |
|
116 | 18 |
|
117 | 19 | fig = plt.figure(figsize=(10.5, 2.5)) |
118 | 20 | fig.subplots_adjust(left=0.05, right=0.95) |
119 | 21 |
|
120 | | -demo_simple_grid(fig) |
121 | | -demo_grid_with_single_cbar(fig) |
122 | | -demo_grid_with_each_cbar(fig) |
123 | | -demo_grid_with_each_cbar_labelled(fig) |
| 22 | + |
| 23 | +# A grid of 2x2 images with 0.05 inch pad between images and only the |
| 24 | +# lower-left axes is labeled. |
| 25 | +grid = ImageGrid( |
| 26 | + fig, 141, # similar to fig.add_subplot(141). |
| 27 | + nrows_ncols=(2, 2), axes_pad=0.05, label_mode="1") |
| 28 | +for ax in grid: |
| 29 | + ax.imshow(Z, extent=extent) |
| 30 | +# This only affects axes in first column and second row as share_all=False. |
| 31 | +grid.axes_llc.set_xticks([-2, 0, 2]) |
| 32 | +grid.axes_llc.set_yticks([-2, 0, 2]) |
| 33 | + |
| 34 | + |
| 35 | +# A grid of 2x2 images with a single colorbar |
| 36 | +grid = ImageGrid( |
| 37 | + fig, 142, # similar to fig.add_subplot(142). |
| 38 | + nrows_ncols=(2, 2), axes_pad=0.0, label_mode="L", share_all=True, |
| 39 | + cbar_location="top", cbar_mode="single") |
| 40 | +for ax in grid: |
| 41 | + im = ax.imshow(Z, extent=extent) |
| 42 | +grid.cbar_axes[0].colorbar(im) |
| 43 | +for cax in grid.cbar_axes: |
| 44 | + cax.toggle_label(False) |
| 45 | +# This affects all axes as share_all = True. |
| 46 | +grid.axes_llc.set_xticks([-2, 0, 2]) |
| 47 | +grid.axes_llc.set_yticks([-2, 0, 2]) |
| 48 | + |
| 49 | + |
| 50 | +# A grid of 2x2 images. Each image has its own colorbar. |
| 51 | +grid = ImageGrid( |
| 52 | + fig, 143, # similar to fig.add_subplot(143). |
| 53 | + nrows_ncols=(2, 2), axes_pad=0.1, label_mode="1", share_all=True, |
| 54 | + cbar_location="top", cbar_mode="each", cbar_size="7%", cbar_pad="2%") |
| 55 | +for ax, cax in zip(grid, grid.cbar_axes): |
| 56 | + im = ax.imshow(Z, extent=extent) |
| 57 | + cax.colorbar(im) |
| 58 | + cax.toggle_label(False) |
| 59 | +# This affects all axes because we set share_all = True. |
| 60 | +grid.axes_llc.set_xticks([-2, 0, 2]) |
| 61 | +grid.axes_llc.set_yticks([-2, 0, 2]) |
| 62 | + |
| 63 | + |
| 64 | +# A grid of 2x2 images. Each image has its own colorbar. |
| 65 | +grid = ImageGrid( |
| 66 | + fig, 144, # similar to fig.add_subplot(144). |
| 67 | + nrows_ncols=(2, 2), axes_pad=(0.45, 0.15), label_mode="1", share_all=True, |
| 68 | + cbar_location="right", cbar_mode="each", cbar_size="7%", cbar_pad="2%") |
| 69 | +# Use a different colorbar range every time |
| 70 | +limits = ((0, 1), (-2, 2), (-1.7, 1.4), (-1.5, 1)) |
| 71 | +for ax, cax, vlim in zip(grid, grid.cbar_axes, limits): |
| 72 | + im = ax.imshow(Z, extent=extent, vmin=vlim[0], vmax=vlim[1]) |
| 73 | + cb = cax.colorbar(im) |
| 74 | + cb.set_ticks((vlim[0], vlim[1])) |
| 75 | +# This affects all axes because we set share_all = True. |
| 76 | +grid.axes_llc.set_xticks([-2, 0, 2]) |
| 77 | +grid.axes_llc.set_yticks([-2, 0, 2]) |
| 78 | + |
124 | 79 |
|
125 | 80 | plt.show() |
0 commit comments