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.


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/line_plots("`Line Plots`") matplotlib/PlottingDataGroup -.-> matplotlib/histograms("`Histograms`") python/BasicConceptsGroup -.-> python/variables_data_types("`Variables and Data Types`") python/BasicConceptsGroup -.-> python/booleans("`Booleans`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/ModulesandPackagesGroup -.-> python/standard_libraries("`Common Standard Libraries`") python/PythonStandardLibraryGroup -.-> python/math_random("`Math and Random`") python/PythonStandardLibraryGroup -.-> python/data_collections("`Data Collections`") python/DataScienceandMachineLearningGroup -.-> python/numerical_computing("`Numerical Computing`") python/DataScienceandMachineLearningGroup -.-> python/data_visualization("`Data Visualization`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills python/comments -.-> lab-48562{{"`Creating Inset Axes`"}} matplotlib/importing_matplotlib -.-> lab-48562{{"`Creating Inset Axes`"}} matplotlib/figures_axes -.-> lab-48562{{"`Creating Inset Axes`"}} matplotlib/line_plots -.-> lab-48562{{"`Creating Inset Axes`"}} matplotlib/histograms -.-> lab-48562{{"`Creating Inset Axes`"}} python/variables_data_types -.-> lab-48562{{"`Creating Inset Axes`"}} python/booleans -.-> lab-48562{{"`Creating Inset Axes`"}} python/lists -.-> lab-48562{{"`Creating Inset Axes`"}} python/tuples -.-> lab-48562{{"`Creating Inset Axes`"}} python/importing_modules -.-> lab-48562{{"`Creating Inset Axes`"}} python/standard_libraries -.-> lab-48562{{"`Creating Inset Axes`"}} python/math_random -.-> lab-48562{{"`Creating Inset Axes`"}} python/data_collections -.-> lab-48562{{"`Creating Inset Axes`"}} python/numerical_computing -.-> lab-48562{{"`Creating Inset Axes`"}} python/data_visualization -.-> lab-48562{{"`Creating Inset Axes`"}} python/build_in_functions -.-> lab-48562{{"`Creating Inset Axes`"}} end

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