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.

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