Add Colorbar to Matplotlib Plot

PythonPythonBeginner
Practice Now

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

Introduction

Matplotlib is a Python library used for data visualization. In this lab, we will learn how to add a colorbar to a plot in Matplotlib. Colorbars are useful for indicating the range of values that a colormap represents.

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 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`"]) 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/DataScienceandMachineLearningGroup -.-> python/data_visualization("`Data Visualization`") subgraph Lab Skills matplotlib/importing_matplotlib -.-> lab-48669{{"`Add Colorbar to Matplotlib Plot`"}} matplotlib/figures_axes -.-> lab-48669{{"`Add Colorbar to Matplotlib Plot`"}} matplotlib/heatmaps -.-> lab-48669{{"`Add Colorbar to Matplotlib Plot`"}} python/lists -.-> lab-48669{{"`Add Colorbar to Matplotlib Plot`"}} python/tuples -.-> lab-48669{{"`Add Colorbar to Matplotlib Plot`"}} python/importing_modules -.-> lab-48669{{"`Add Colorbar to Matplotlib Plot`"}} python/data_visualization -.-> lab-48669{{"`Add Colorbar to Matplotlib Plot`"}} end

Import necessary libraries

We will start by importing the necessary libraries. We will be using Matplotlib's pyplot module, which provides an interface for creating plots.

import matplotlib.pyplot as plt

Create a plot

Next, we will create a plot using Matplotlib's imshow function. This function displays an image on the plot. We will also create a figure with two subplots.

fig, (ax1, ax2) = plt.subplots(1, 2)
fig.subplots_adjust(wspace=0.5)

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

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

Add a colorbar to the plot

Now, we will add a colorbar to each subplot using Matplotlib's make_axes_locatable function. This function takes an existing axes, adds it to a new AxesDivider and returns the AxesDivider. The append_axes method of the AxesDivider can then be used to create a new axes on a given side ("top", "right", "bottom", or "left") of the original axes.

ax1_divider = make_axes_locatable(ax1)
cax1 = ax1_divider.append_axes("right", size="7%", pad="2%")
cb1 = fig.colorbar(im1, cax=cax1)

ax2_divider = make_axes_locatable(ax2)
cax2 = ax2_divider.append_axes("top", size="7%", pad="2%")
cb2 = fig.colorbar(im2, cax=cax2, orientation="horizontal")
cax2.xaxis.set_ticks_position("top")

Display the plot

Finally, we will display the plot using Matplotlib's show function.

plt.show()

Summary

In this lab, we learned how to add a colorbar to a plot in Matplotlib. We used the make_axes_locatable function to add an additional axes to the plot and the colorbar function to create the colorbar. We also learned how to change the orientation and position of the colorbar.

Other Python Tutorials you may like