Matplotlib Shared Axis

PythonPythonBeginner
Practice Now

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

Introduction

In this lab, you will learn how to create plots with shared axes using the Matplotlib library in Python. Shared axes can be useful when you want to compare different data sets with the same scale.

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`"]) python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) 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/line_plots("`Line Plots`") matplotlib/AdvancedPlottingGroup -.-> matplotlib/subplots("`Subplots`") python/BasicConceptsGroup -.-> python/booleans("`Booleans`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/DataScienceandMachineLearningGroup -.-> python/numerical_computing("`Numerical Computing`") python/DataScienceandMachineLearningGroup -.-> python/data_visualization("`Data Visualization`") subgraph Lab Skills matplotlib/importing_matplotlib -.-> lab-48926{{"`Matplotlib Shared Axis`"}} matplotlib/figures_axes -.-> lab-48926{{"`Matplotlib Shared Axis`"}} matplotlib/line_plots -.-> lab-48926{{"`Matplotlib Shared Axis`"}} matplotlib/subplots -.-> lab-48926{{"`Matplotlib Shared Axis`"}} python/booleans -.-> lab-48926{{"`Matplotlib Shared Axis`"}} python/tuples -.-> lab-48926{{"`Matplotlib Shared Axis`"}} python/importing_modules -.-> lab-48926{{"`Matplotlib Shared Axis`"}} python/numerical_computing -.-> lab-48926{{"`Matplotlib Shared Axis`"}} python/data_visualization -.-> lab-48926{{"`Matplotlib Shared Axis`"}} end

Import Matplotlib and NumPy Libraries

We need to import the Matplotlib and NumPy libraries to create the plots. NumPy is a library for the Python programming language, adding support for large, multi-dimensional arrays and matrices.

import matplotlib.pyplot as plt
import numpy as np

Create Data for the Plots

We need to create data for the plots to visualize. In this example, we will create three different datasets using NumPy.

t = np.arange(0.01, 5.0, 0.01)
s1 = np.sin(2 * np.pi * t)
s2 = np.exp(-t)
s3 = np.sin(4 * np.pi * t)

Create the Subplots

We can create subplots using the plt.subplot() method. In this example, we will create three subplots with the first subplot taking up the first row and all three columns, and the second and third subplots taking up the second and third row, respectively, and sharing the x-axis with the first subplot.

ax1 = plt.subplot(311)
ax2 = plt.subplot(312, sharex=ax1)
ax3 = plt.subplot(313, sharex=ax1, sharey=ax1)

Plot the Data

We can now plot the data on each of the subplots using the plt.plot() method.

ax1.plot(t, s1)
ax2.plot(t, s2)
ax3.plot(t, s3)

Customize the Tick Labels

We can customize the tick labels on the various axes using the plt.tick_params() method. In this example, we will customize the tick labels on the x-axis of the first subplot to be smaller.

plt.tick_params('x', labelsize=6)

Remove Tick Labels

We can remove tick labels from a specific subplot by altering the visibility of the labels using the ax.get_xticklabels() method. In this example, we will remove the tick labels on the x-axis of the second subplot.

plt.tick_params('x', labelbottom=False)

Set the Axis Limits

We can set the axis limits on each subplot using the plt.xlim() method. In this example, we will set the x-axis limits on the third subplot to be from 0.01 to 5.0.

plt.xlim(0.01, 5.0)

Display the Plots

We can now display the plots using the plt.show() method.

plt.show()

Summary

In this lab, we learned how to create plots with shared axes using the Matplotlib library in Python. We created subplots, plotted data on each subplot, customized tick labels, removed tick labels, set the axis limits, and displayed the plots. Shared axes can be useful when you want to compare different data sets with the same scale.

Other Python Tutorials you may like