Adding a Colorbar to Inset Axes

PythonPythonBeginner
Practice Now

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

Introduction

In this lab, you will learn how to add a colorbar to inset axes using Matplotlib in Python. A colorbar is a visual representation of the mapping of a range of colors to a range of numerical values. An inset axis is a smaller axis that is placed within the larger axis of a plot.

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/PythonStandardLibraryGroup(["`Python Standard Library`"]) python(("`Python`")) -.-> python/DataScienceandMachineLearningGroup(["`Data Science and Machine Learning`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) 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/BasicConceptsGroup -.-> python/variables_data_types("`Variables and Data Types`") 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/PythonStandardLibraryGroup -.-> python/data_collections("`Data Collections`") python/DataScienceandMachineLearningGroup -.-> python/data_visualization("`Data Visualization`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills python/comments -.-> lab-48668{{"`Adding a Colorbar to Inset Axes`"}} matplotlib/importing_matplotlib -.-> lab-48668{{"`Adding a Colorbar to Inset Axes`"}} matplotlib/figures_axes -.-> lab-48668{{"`Adding a Colorbar to Inset Axes`"}} matplotlib/heatmaps -.-> lab-48668{{"`Adding a Colorbar to Inset Axes`"}} python/variables_data_types -.-> lab-48668{{"`Adding a Colorbar to Inset Axes`"}} python/lists -.-> lab-48668{{"`Adding a Colorbar to Inset Axes`"}} python/tuples -.-> lab-48668{{"`Adding a Colorbar to Inset Axes`"}} python/importing_modules -.-> lab-48668{{"`Adding a Colorbar to Inset Axes`"}} python/using_packages -.-> lab-48668{{"`Adding a Colorbar to Inset Axes`"}} python/data_collections -.-> lab-48668{{"`Adding a Colorbar to Inset Axes`"}} python/data_visualization -.-> lab-48668{{"`Adding a Colorbar to Inset Axes`"}} python/build_in_functions -.-> lab-48668{{"`Adding a Colorbar to Inset Axes`"}} end

Import Libraries and Data

First, import the necessary libraries and data that will be used in the plot.

import matplotlib.pyplot as plt
from matplotlib import cbook
from mpl_toolkits.axes_grid1.inset_locator import inset_axes, zoomed_inset_axes

fig, ax = plt.subplots(figsize=[5, 4])

Z = cbook.get_sample_data("axes_grid/bivariate_normal.npy")
extent = (-3, 4, -4, 3)

Set the Main Plot

Set the main plot by adjusting the aspect ratio and the limits of the x and y axes.

ax.set(aspect=1, xlim=(-15, 15), ylim=(-20, 5))

Create an Inset Axis

Create an inset axis using the zoomed_inset_axes function. Set the zoom level and the location of the inset axis within the main plot.

axins = zoomed_inset_axes(ax, zoom=2, loc='upper left')
axins.set(xticks=[], yticks=[])

Add an Image to the Inset Axis

Add an image to the inset axis using the imshow function. Set the extent and the origin of the image.

im = axins.imshow(Z, extent=extent, origin="lower")

Add a Colorbar

Add a colorbar to the inset axis using the inset_axes function. Set the width, height, location, and bounding box of the colorbar.

cax = inset_axes(axins,
                 width="5%",  ## width = 10% of parent_bbox width
                 height="100%",  ## height : 50%
                 loc='lower left',
                 bbox_to_anchor=(1.05, 0., 1, 1),
                 bbox_transform=axins.transAxes,
                 borderpad=0,
                 )
fig.colorbar(im, cax=cax)

Display the Plot

Display the plot using the show function.

plt.show()

Summary

Congratulations! You have successfully learned how to add a colorbar to inset axes using Matplotlib in Python. This is a useful technique for visualizing data in a more detailed and informative way. Remember to adjust the parameters according to your specific needs and preferences.

Other Python Tutorials you may like