|
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 by passing `.Figure.colorbar` |
| 33 | +# a list of axes to the *ax* kwarg. |
34 | 34 |
|
35 | 35 | fig, axs = plt.subplots(2, 2)
|
36 | 36 | cmaps = ['RdBu_r', 'viridis']
|
|
77 | 77 | fig.colorbar(pcm, ax=ax, shrink=0.6)
|
78 | 78 |
|
79 | 79 | # %%
|
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 |
82 |
| -# change the shape of the axes, the colorbar will also change position. |
| 80 | +# One way around this issue is to use an `.Axes.inset_axes` to locate the axes |
| 81 | +# in axes coordinates. Here instead of populating the *ax* keyword argument, |
| 82 | +# we create an axes named ``cax`` to put the colorbar in, and pass it to the |
| 83 | +# ``colorbar`` in the *cax* keyword argument. Note that if you zoom in on the |
| 84 | +# axes, and change the shape of the axes, the colorbar will also change |
| 85 | +# position. |
83 | 86 |
|
84 | 87 | fig, axs = plt.subplots(2, 2, layout='constrained')
|
85 | 88 | cmaps = ['RdBu_r', 'viridis']
|
|
94 | 97 | ax.set_aspect(1/2)
|
95 | 98 | if row == 1:
|
96 | 99 | cax = ax.inset_axes([1.04, 0.2, 0.05, 0.6])
|
97 |
| - fig.colorbar(pcm, ax=ax, cax=cax) |
| 100 | + fig.colorbar(pcm, cax=cax) |
| 101 | + |
| 102 | + |
| 103 | +# %% |
| 104 | +# Manually placing colorbars |
| 105 | +# ========================== |
| 106 | +# |
| 107 | +# As the case above indicates, sometimes we want to place a colorbar manually |
| 108 | +# on the figure, which typically involves creating a host axes manually and |
| 109 | +# passing it to the *cax* keyword argument. You can do this with any type of |
| 110 | +# axes, but an `.Axes.inset_axes` is useful because it can be positioned |
| 111 | +# relative to the parent axes. Here we add a colorbar centered near the bottom |
| 112 | +# of the parent axes. |
| 113 | + |
| 114 | +fig, ax = plt.subplots(layout='constrained') |
| 115 | +pcm = ax.pcolormesh(np.random.randn(20, 20), cmap='viridis') |
| 116 | +ax.set_ylim([-4, 20]) |
| 117 | +cax = ax.inset_axes([0.3, 0.06, 0.4, 0.04]) |
| 118 | +fig.colorbar(pcm, cax=cax, orientation='horizontal') |
| 119 | + |
| 120 | +# %% |
| 121 | +# `.Axes.inset_axes` can also specify its position in data coordinates |
| 122 | +# using the *transform* keyword argument if you want your axes at a |
| 123 | +# certain data position on the graph: |
| 124 | + |
| 125 | +fig, ax = plt.subplots(layout='constrained') |
| 126 | +pcm = ax.pcolormesh(np.random.randn(20, 20), cmap='viridis') |
| 127 | +ax.set_ylim([-4, 20]) |
| 128 | +cax = ax.inset_axes([7.5, -2, 5, 1.2], transform=ax.transData) |
| 129 | +fig.colorbar(pcm, cax=cax, orientation='horizontal') |
98 | 130 |
|
99 | 131 | plt.show()
|
| 132 | + |
| 133 | +# %% |
| 134 | +# .. seealso:: |
| 135 | +# |
| 136 | +# The :ref:`axes_grid` has methods for creating colorbar axes as well: |
| 137 | +# |
| 138 | +# - :ref:`demo-colorbar-with-inset-locator` |
| 139 | +# - :ref:`demo-colorbar-with-axes-divider` |
| 140 | +# |
0 commit comments