Creating Inset Axes

PythonPythonBeginner
Practice Now

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

Introduction

Matplotlib is a powerful data visualization library in Python. It provides a variety of plots and charts to visualize data in a meaningful way. In this lab, we will learn how to create inset axes within the main plot axes using fig.add_axes in Matplotlib.

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.

Import Required Libraries

We start by importing the necessary libraries, which include Matplotlib and NumPy.

import matplotlib.pyplot as plt
import numpy as np

Generate Data

In this step, we generate some data to use for the plot. We will create a Gaussian colored noise using NumPy's convolve function and plot it using Matplotlib.

np.random.seed(19680801)
dt = 0.001
t = np.arange(0.0, 10.0, dt)
r = np.exp(-t[:1000] / 0.05)
x = np.random.randn(len(t))
s = np.convolve(x, r)[:len(x)] * dt

fig, main_ax = plt.subplots()
main_ax.plot(t, s)

Set Limits and Labels

In this step, we set the limits and labels for the main plot axes.

main_ax.set_xlim(0, 1)
main_ax.set_ylim(1.1 * np.min(s), 2 * np.max(s))
main_ax.set_xlabel('time (s)')
main_ax.set_ylabel('current (nA)')
main_ax.set_title('Gaussian colored noise')

Create Inset Axes

In this step, we create two inset axes within the main plot axes using fig.add_axes. One will display a histogram of the data, and the other will display the impulse response.

## Create right inset axes
right_inset_ax = fig.add_axes([.65, .6, .2, .2], facecolor='k')
right_inset_ax.hist(s, 400, density=True)
right_inset_ax.set(title='Probability', xticks=[], yticks=[])

## Create left inset axes
left_inset_ax = fig.add_axes([.2, .6, .2, .2], facecolor='k')
left_inset_ax.plot(t[:len(r)], r)
left_inset_ax.set(title='Impulse response', xlim=(0, .2), xticks=[], yticks=[])

Display the Plot

In this step, we display the plot using the plt.show() function.

plt.show()

Summary

In this lab, we learned how to create inset axes within the main plot axes using fig.add_axes in Matplotlib. We generated data, set limits and labels, created two inset axes, and displayed the plot. This technique can be useful when we want to zoom in on a particular area of the plot or display additional information related to the data.

Other Python Tutorials you may like