Matplotlib Colorbar Inset Axes

PythonPythonBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

This tutorial will show you how to control the position, height, and width of colorbars using mpl_toolkits.axes_grid1.inset_locator.inset_axes. This is useful when you want to add a colorbar to your plot but want to customize its size and position.

VM Tips

After the VM startup is done, click the top left corner to switch to the Notebook tab to access Jupyter Notebook for practice.

Sometimes, you may need to wait a few seconds for Jupyter Notebook to finish loading. The validation of operations cannot be automated because of limitations in Jupyter Notebook.

If you face issues during learning, feel free to ask Labby. Provide feedback after the session, and we will promptly resolve the problem for you.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) matplotlib(("`Matplotlib`")) -.-> matplotlib/BasicConceptsGroup(["`Basic Concepts`"]) matplotlib(("`Matplotlib`")) -.-> matplotlib/PlottingDataGroup(["`Plotting Data`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/DataScienceandMachineLearningGroup(["`Data Science and Machine Learning`"]) python/BasicConceptsGroup -.-> python/comments("`Comments`") matplotlib/BasicConceptsGroup -.-> matplotlib/importing_matplotlib("`Importing Matplotlib`") matplotlib/BasicConceptsGroup -.-> matplotlib/figures_axes("`Understanding Figures and Axes`") matplotlib/PlottingDataGroup -.-> matplotlib/heatmaps("`Heatmaps`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/ModulesandPackagesGroup -.-> python/using_packages("`Using Packages`") python/DataScienceandMachineLearningGroup -.-> python/data_visualization("`Data Visualization`") subgraph Lab Skills python/comments -.-> lab-48670{{"`Matplotlib Colorbar Inset Axes`"}} matplotlib/importing_matplotlib -.-> lab-48670{{"`Matplotlib Colorbar Inset Axes`"}} matplotlib/figures_axes -.-> lab-48670{{"`Matplotlib Colorbar Inset Axes`"}} matplotlib/heatmaps -.-> lab-48670{{"`Matplotlib Colorbar Inset Axes`"}} python/lists -.-> lab-48670{{"`Matplotlib Colorbar Inset Axes`"}} python/tuples -.-> lab-48670{{"`Matplotlib Colorbar Inset Axes`"}} python/importing_modules -.-> lab-48670{{"`Matplotlib Colorbar Inset Axes`"}} python/using_packages -.-> lab-48670{{"`Matplotlib Colorbar Inset Axes`"}} python/data_visualization -.-> lab-48670{{"`Matplotlib Colorbar Inset Axes`"}} end

Import Required Libraries

First, we need to import the required libraries: matplotlib and inset_axes from mpl_toolkits.axes_grid1.

import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1.inset_locator import inset_axes

Create Plot and Image

Next, we will create a plot and an image to show how to add a colorbar using inset axes.

fig, (ax1, ax2) = plt.subplots(1, 2, figsize=[6, 3])

im1 = ax1.imshow([[1, 2], [2, 3]])
im2 = ax2.imshow([[1, 2], [2, 3]])

Add Colorbar using Inset Axes

Now, we will add a colorbar to each of the images using inset_axes. The first colorbar will be added to ax1 and the second to ax2.

## add colorbar to ax1
axins1 = inset_axes(
    ax1,
    width="50%",  ## width: 50% of parent_bbox width
    height="5%",  ## height: 5%
    loc="upper right",
)
axins1.xaxis.set_ticks_position("bottom")
fig.colorbar(im1, cax=axins1, orientation="horizontal", ticks=[1, 2, 3])

## add colorbar to ax2
axins2 = inset_axes(
    ax2,
    width="5%",  ## width: 5% of parent_bbox width
    height="50%",  ## height: 50%
    loc="lower left",
    bbox_to_anchor=(1.05, 0., 1, 1),
    bbox_transform=ax2.transAxes,
    borderpad=0,
)
fig.colorbar(im2, cax=axins2, ticks=[1, 2, 3])

Display the Plot

Finally, we will display the plot using plt.show().

plt.show()

Summary

In this tutorial, we learned how to use mpl_toolkits.axes_grid1.inset_locator.inset_axes to add a colorbar to a plot with customized size and position. The inset_axes function allows us to control the position, height, and width of the colorbar, making it easier to create professional-looking plots.

Other Python Tutorials you may like