Create Multiplot with Parasite Axes in Matplotlib

PythonPythonBeginner
Practice Now

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

Introduction

In this lab, we will learn how to create a multiple dataset plot using parasite axis in Python Matplotlib. We will use the mpl_toolkits.axes_grid1.parasite_axes.host_subplot and mpl_toolkits.axisartist.axislines.Axes to create a plot with multiple y-axes.

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`"]) matplotlib(("`Matplotlib`")) -.-> matplotlib/AdvancedPlottingGroup(["`Advanced Plotting`"]) matplotlib(("`Matplotlib`")) -.-> matplotlib/PlotCustomizationGroup(["`Plot Customization`"]) python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) 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`"]) matplotlib/BasicConceptsGroup -.-> matplotlib/importing_matplotlib("`Importing Matplotlib`") matplotlib/BasicConceptsGroup -.-> matplotlib/figures_axes("`Understanding Figures and Axes`") matplotlib/PlottingDataGroup -.-> matplotlib/line_plots("`Line Plots`") matplotlib/AdvancedPlottingGroup -.-> matplotlib/secondary_axis("`Secondary Axis`") matplotlib/PlotCustomizationGroup -.-> matplotlib/legend_config("`Legend Configuration`") 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/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 matplotlib/importing_matplotlib -.-> lab-48683{{"`Create Multiplot with Parasite Axes in Matplotlib`"}} matplotlib/figures_axes -.-> lab-48683{{"`Create Multiplot with Parasite Axes in Matplotlib`"}} matplotlib/line_plots -.-> lab-48683{{"`Create Multiplot with Parasite Axes in Matplotlib`"}} matplotlib/secondary_axis -.-> lab-48683{{"`Create Multiplot with Parasite Axes in Matplotlib`"}} matplotlib/legend_config -.-> lab-48683{{"`Create Multiplot with Parasite Axes in Matplotlib`"}} python/variables_data_types -.-> lab-48683{{"`Create Multiplot with Parasite Axes in Matplotlib`"}} python/booleans -.-> lab-48683{{"`Create Multiplot with Parasite Axes in Matplotlib`"}} python/lists -.-> lab-48683{{"`Create Multiplot with Parasite Axes in Matplotlib`"}} python/tuples -.-> lab-48683{{"`Create Multiplot with Parasite Axes in Matplotlib`"}} python/importing_modules -.-> lab-48683{{"`Create Multiplot with Parasite Axes in Matplotlib`"}} python/using_packages -.-> lab-48683{{"`Create Multiplot with Parasite Axes in Matplotlib`"}} python/data_collections -.-> lab-48683{{"`Create Multiplot with Parasite Axes in Matplotlib`"}} python/data_visualization -.-> lab-48683{{"`Create Multiplot with Parasite Axes in Matplotlib`"}} python/build_in_functions -.-> lab-48683{{"`Create Multiplot with Parasite Axes in Matplotlib`"}} end

Import Libraries

First, we need to import the necessary libraries. We will use matplotlib.pyplot for plotting and mpl_toolkits for creating parasite axis.

import matplotlib.pyplot as plt
from mpl_toolkits import axisartist
from mpl_toolkits.axes_grid1 import host_subplot

Create Host and Parasite Axes

We will create a host axis and two parasite axes using the host_subplot() and twinx() functions. The host_subplot() function creates a host axis, and the twinx() function creates parasite axes that share the same x-axis with the host axis.

host = host_subplot(111, axes_class=axisartist.Axes)
plt.subplots_adjust(right=0.75)

par1 = host.twinx()
par2 = host.twinx()

Adjust Parasite Axis

We need to adjust the position of the parasite axes. The new_fixed_axis() function is used to create a new y-axis on the right side of the plot. The toggle() function is used to turn on all the ticks and labels of the right y-axis.

par2.axis["right"] = par2.new_fixed_axis(loc="right", offset=(60, 0))

par1.axis["right"].toggle(all=True)
par2.axis["right"].toggle(all=True)

Plot the Data

We will plot three datasets on the same plot: Density, Temperature, and Velocity. We will use the plot() function to plot the data.

p1, = host.plot([0, 1, 2], [0, 1, 2], label="Density")
p2, = par1.plot([0, 1, 2], [0, 3, 2], label="Temperature")
p3, = par2.plot([0, 1, 2], [50, 30, 15], label="Velocity")

Set Axis Limits and Labels

We will set the x-axis and y-axis limits and labels for each axis using the set() function.

host.set(xlim=(0, 2), ylim=(0, 2), xlabel="Distance", ylabel="Density")
par1.set(ylim=(0, 4), ylabel="Temperature")
par2.set(ylim=(1, 65), ylabel="Velocity")

Add Legend and Color

We will add a legend to the plot and color the labels of each axis to match the color of the corresponding dataset using the legend() and label.set_color() functions.

host.legend()

host.axis["left"].label.set_color(p1.get_color())
par1.axis["right"].label.set_color(p2.get_color())
par2.axis["right"].label.set_color(p3.get_color())

Display the Plot

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

plt.show()

Summary

In this lab, we learned how to create a multiple dataset plot using parasite axis in Python Matplotlib. We used the mpl_toolkits.axes_grid1.parasite_axes.host_subplot and mpl_toolkits.axisartist.axislines.Axes to create a plot with multiple y-axes. We also learned how to adjust the position of the parasite axes, plot the data, set the axis limits and labels, add legend and color to the plot, and display the plot.

Other Python Tutorials you may like