Repository: matplotlib/matplotlib
File: galleries/users_explain/axes/colorbar_placement.py (Python)

Code before:
fig, ax = plt.subplots(layout='compressed', figsize=(4, 4))
pcm = ax.imshow(np.random.randn(10, 10), cmap='viridis')
ax.set_title("Colorbar with layout='compressed'", fontsize='medium')
fig.colorbar(pcm, ax=ax)

# %%
# Alternatively, we can manually position the colorbar using `.Axes.inset_axes`
# with axes-relative coordinates. This approach provides precise control over
# the colorbar's placement. However, without a layout engine, the colorbar
# might be clipped if it extends beyond the figure boundaries.

fig, ax = plt.subplots(layout='constrained', figsize=(4, 4))
pcm = ax.imshow(np.random.randn(10, 10), cmap='viridis')
cax = ax.inset_axes([1.04, 0.0, 0.05, 1.0])  # Positioning the colorbar
ax.set_title('Colorbar with inset_axes', fontsize='medium')
fig.colorbar(pcm, cax=cax)

# %%
# We can also do this manually using an `.Axes.inset_axes` using axes-relative
# coordinates (see :ref:`transforms_tutorial`).  Note that if we do not use a
# layout engine, the colorbar will be clipped off the right side of the figure.

fig, ax = plt.subplots(layout='constrained', figsize=(4, 4))
pcm = ax.imshow(np.random.randn(10, 10), cmap='viridis')
cax = ax.inset_axes([1.04, 0.0, 0.05, 1.0])
ax.set_title('colorbar with inset_axes', fontsize='medium')
fig.colorbar(pcm, cax=cax)

# %%
# .. seealso::
#
#  :ref:`axes_grid` has methods for manually creating colorbar Axes as well:
#
#  - :ref:`demo-colorbar-with-inset-locator`
#  - :ref:`demo-colorbar-with-axes-divider`


Review comment (suggestion):
[nitpick] The title string uses lowercase 'colorbar' whereas a similar example earlier uses 'Colorbar' with an uppercase 'C'. Consider standardizing the capitalization for a consistent user experience.
```suggestion
ax.set_title('Colorbar with inset_axes', fontsize='medium')
```

Code after:
fig, ax = plt.subplots(layout='compressed', figsize=(4, 4))
pcm = ax.imshow(np.random.randn(10, 10), cmap='viridis')
ax.set_title("Colorbar with layout='compressed'", fontsize='medium')
fig.colorbar(pcm, ax=ax)

# %%
# Alternatively, we can manually position the colorbar using `.Axes.inset_axes`
# with axes-relative coordinates. This approach provides precise control over
# the colorbar's placement. However, without a layout engine, the colorbar
# might be clipped if it extends beyond the figure boundaries.

fig, ax = plt.subplots(layout='constrained', figsize=(4, 4))
pcm = ax.imshow(np.random.randn(10, 10), cmap='viridis')
cax = ax.inset_axes([1.04, 0.0, 0.05, 1.0])  # Positioning the colorbar
ax.set_title('Colorbar with inset_axes', fontsize='medium')
fig.colorbar(pcm, cax=cax)

# %%
# We can also do this manually using an `.Axes.inset_axes` using axes-relative
# coordinates (see :ref:`transforms_tutorial`).  Note that if we do not use a
# layout engine, the colorbar will be clipped off the right side of the figure.

fig, ax = plt.subplots(layout='constrained', figsize=(4, 4))
pcm = ax.imshow(np.random.randn(10, 10), cmap='viridis')
cax = ax.inset_axes([1.04, 0.0, 0.05, 1.0])
ax.set_title('Colorbar with inset_axes', fontsize='medium')
fig.colorbar(pcm, cax=cax)

# %%
# .. seealso::
#
#  :ref:`axes_grid` has methods for manually creating colorbar Axes as well:
#
#  - :ref:`demo-colorbar-with-inset-locator`
#  - :ref:`demo-colorbar-with-axes-divider`