|
55 | 55 | fig.colorbar(pcm, ax=axs[1:, :], location='right', shrink=0.6)
|
56 | 56 | fig.colorbar(pcm, ax=[axs[2, 1]], location='left')
|
57 | 57 |
|
| 58 | +# %% |
| 59 | +# Adjusting the spacing between colorbars and parent axes |
| 60 | +# ======================================================= |
| 61 | +# |
| 62 | +# The distance a colorbar is from the parent axes can be adjusted with the |
| 63 | +# *pad* keyword argument. This is in units of fraction of the parent axes |
| 64 | +# width, and the default for a vertical axes is 0.05 (or 0.15 for a horizontal |
| 65 | +# axes). |
| 66 | + |
| 67 | +fig, axs = plt.subplots(3, 1, layout='constrained', figsize=(5, 5)) |
| 68 | +for ax, pad in zip(axs, [0.025, 0.05, 0.1]): |
| 69 | + pcm = ax.pcolormesh(np.random.randn(20, 20), cmap='viridis') |
| 70 | + fig.colorbar(pcm, ax=ax, pad=pad, label=f'pad: {pad}') |
| 71 | +fig.suptitle("layout='constrained'") |
| 72 | + |
| 73 | +# %% |
| 74 | +# Note that if you do not use constrained layout, the pad command makes the |
| 75 | +# parent axes shrink: |
| 76 | + |
| 77 | +fig, axs = plt.subplots(3, 1, figsize=(5, 5)) |
| 78 | +for ax, pad in zip(axs, [0.025, 0.05, 0.1]): |
| 79 | + pcm = ax.pcolormesh(np.random.randn(20, 20), cmap='viridis') |
| 80 | + fig.colorbar(pcm, ax=ax, pad=pad, label=f'pad: {pad}') |
| 81 | +fig.suptitle("No layout manager") |
| 82 | + |
58 | 83 | # %%
|
59 | 84 | # Colorbars with fixed-aspect-ratio axes
|
60 | 85 | # ======================================
|
|
99 | 124 | cax = ax.inset_axes([1.04, 0.2, 0.05, 0.6])
|
100 | 125 | fig.colorbar(pcm, cax=cax)
|
101 | 126 |
|
102 |
| - |
103 | 127 | # %%
|
104 | 128 | # Manually placing colorbars
|
105 | 129 | # ==========================
|
|
111 | 135 | # relative to the parent axes. Here we add a colorbar centered near the bottom
|
112 | 136 | # of the parent axes.
|
113 | 137 |
|
114 |
| -fig, ax = plt.subplots(layout='constrained') |
| 138 | +fig, ax = plt.subplots(layout='constrained', figsize=(4, 4)) |
115 | 139 | pcm = ax.pcolormesh(np.random.randn(20, 20), cmap='viridis')
|
116 | 140 | ax.set_ylim([-4, 20])
|
117 |
| -cax = ax.inset_axes([0.3, 0.06, 0.4, 0.04]) |
| 141 | +cax = ax.inset_axes([0.3, 0.07, 0.4, 0.04]) |
118 | 142 | fig.colorbar(pcm, cax=cax, orientation='horizontal')
|
119 | 143 |
|
120 | 144 | # %%
|
121 | 145 | # `.Axes.inset_axes` can also specify its position in data coordinates
|
122 | 146 | # using the *transform* keyword argument if you want your axes at a
|
123 | 147 | # certain data position on the graph:
|
124 | 148 |
|
125 |
| -fig, ax = plt.subplots(layout='constrained') |
| 149 | +fig, ax = plt.subplots(layout='constrained', figsize=(4, 4)) |
126 | 150 | pcm = ax.pcolormesh(np.random.randn(20, 20), cmap='viridis')
|
127 | 151 | ax.set_ylim([-4, 20])
|
128 |
| -cax = ax.inset_axes([7.5, -2, 5, 1.2], transform=ax.transData) |
| 152 | +cax = ax.inset_axes([7.5, -1.7, 5, 1.2], transform=ax.transData) |
129 | 153 | fig.colorbar(pcm, cax=cax, orientation='horizontal')
|
130 | 154 |
|
131 | 155 | plt.show()
|
|
0 commit comments