|
28 | 28 | fig.colorbar(pcm, ax=ax)
|
29 | 29 |
|
30 | 30 | # %%
|
31 |
| -# The first column has the same type of data in both rows, so it may |
32 |
| -# be desirable to combine the colorbar which we do by calling |
33 |
| -# `.Figure.colorbar` with a list of axes instead of a single axes. |
| 31 | +# The first column has the same type of data in both rows, so it may be |
| 32 | +# desirable to have just one colorbar. We do this by passing `.Figure.colorbar` |
| 33 | +# a list of axes with the *ax* kwarg. |
34 | 34 |
|
35 | 35 | fig, axs = plt.subplots(2, 2)
|
36 | 36 | cmaps = ['RdBu_r', 'viridis']
|
|
56 | 56 | fig.colorbar(pcm, ax=[axs[2, 1]], location='left')
|
57 | 57 |
|
58 | 58 | # %%
|
59 |
| -# Colorbars with fixed-aspect-ratio axes |
60 |
| -# ====================================== |
| 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 | + |
| 83 | +# %% |
| 84 | +# Manual placement of colorbars |
| 85 | +# ============================= |
| 86 | +# |
| 87 | +# Sometimes the automatic placement provided by ``colorbar`` does not |
| 88 | +# give the desired effect. We can manually create an axes and tell |
| 89 | +# ``colorbar`` to use that axes by passing the axes to the *cax* keyword |
| 90 | +# argument. |
| 91 | +# |
| 92 | +# Using ``inset_axes`` |
| 93 | +# -------------------- |
| 94 | +# |
| 95 | +# We can manually create any type of axes for the colorbar to use, but an |
| 96 | +# `.Axes.inset_axes` is useful because it is a child of the parent axes and can |
| 97 | +# be positioned relative to the parent. Here we add a colorbar centered near |
| 98 | +# the bottom of the parent axes. |
| 99 | + |
| 100 | +fig, ax = plt.subplots(layout='constrained', figsize=(4, 4)) |
| 101 | +pcm = ax.pcolormesh(np.random.randn(20, 20), cmap='viridis') |
| 102 | +ax.set_ylim([-4, 20]) |
| 103 | +cax = ax.inset_axes([0.3, 0.07, 0.4, 0.04]) |
| 104 | +fig.colorbar(pcm, cax=cax, orientation='horizontal') |
| 105 | + |
| 106 | +# %% |
| 107 | +# `.Axes.inset_axes` can also specify its position in data coordinates |
| 108 | +# using the *transform* keyword argument if you want your axes at a |
| 109 | +# certain data position on the graph: |
| 110 | + |
| 111 | +fig, ax = plt.subplots(layout='constrained', figsize=(4, 4)) |
| 112 | +pcm = ax.pcolormesh(np.random.randn(20, 20), cmap='viridis') |
| 113 | +ax.set_ylim([-4, 20]) |
| 114 | +cax = ax.inset_axes([7.5, -1.7, 5, 1.2], transform=ax.transData) |
| 115 | +fig.colorbar(pcm, cax=cax, orientation='horizontal') |
| 116 | + |
| 117 | +# %% |
| 118 | +# .. seealso:: |
| 119 | +# |
| 120 | +# :ref:`axes_grid` has methods for creating colorbar axes as well: |
| 121 | +# |
| 122 | +# - :ref:`demo-colorbar-with-inset-locator` |
| 123 | +# - :ref:`demo-colorbar-with-axes-divider` |
| 124 | +# |
| 125 | +# Colorbars attached to fixed-aspect-ratio axes |
| 126 | +# --------------------------------------------- |
61 | 127 | #
|
62 | 128 | # Placing colorbars for axes with a fixed aspect ratio pose a particular
|
63 | 129 | # challenge as the parent axes changes size depending on the data view.
|
|
77 | 143 | fig.colorbar(pcm, ax=ax, shrink=0.6)
|
78 | 144 |
|
79 | 145 | # %%
|
80 |
| -# One way around this issue is to use an `.Axes.inset_axes` to locate the |
81 |
| -# axes in axes coordinates. Note that if you zoom in on the axes, and |
| 146 | +# We solve this problem using `.Axes.inset_axes` to locate the axes in axes |
| 147 | +# coordinates. Note that if you zoom in on the axes, and thus |
82 | 148 | # change the shape of the axes, the colorbar will also change position.
|
83 | 149 |
|
84 | 150 | fig, axs = plt.subplots(2, 2, layout='constrained')
|
|
94 | 160 | ax.set_aspect(1/2)
|
95 | 161 | if row == 1:
|
96 | 162 | cax = ax.inset_axes([1.04, 0.2, 0.05, 0.6])
|
97 |
| - fig.colorbar(pcm, ax=ax, cax=cax) |
98 |
| - |
99 |
| -plt.show() |
| 163 | + fig.colorbar(pcm, cax=cax) |
0 commit comments