Custom Spines With Axisartist

PythonPythonBeginner
Practice Now

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

Introduction

This tutorial will show you how to use the Matplotlib library to create custom spines at specific positions.

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`"]) 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`") 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/DataScienceandMachineLearningGroup -.-> python/numerical_computing("`Numerical Computing`") python/DataScienceandMachineLearningGroup -.-> python/data_visualization("`Data Visualization`") subgraph Lab Skills matplotlib/importing_matplotlib -.-> lab-48936{{"`Custom Spines With Axisartist`"}} matplotlib/figures_axes -.-> lab-48936{{"`Custom Spines With Axisartist`"}} matplotlib/line_plots -.-> lab-48936{{"`Custom Spines With Axisartist`"}} python/booleans -.-> lab-48936{{"`Custom Spines With Axisartist`"}} python/lists -.-> lab-48936{{"`Custom Spines With Axisartist`"}} python/tuples -.-> lab-48936{{"`Custom Spines With Axisartist`"}} python/importing_modules -.-> lab-48936{{"`Custom Spines With Axisartist`"}} python/using_packages -.-> lab-48936{{"`Custom Spines With Axisartist`"}} python/numerical_computing -.-> lab-48936{{"`Custom Spines With Axisartist`"}} python/data_visualization -.-> lab-48936{{"`Custom Spines With Axisartist`"}} end

Import Libraries

First, we need to import the necessary libraries for this tutorial. We will be using Matplotlib and NumPy.

import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits import axisartist

Create Figure and Subplots

We will create a figure with two subplots using the add_gridspec method.

fig = plt.figure(figsize=(6, 3), layout="constrained")
gs = fig.add_gridspec(1, 2)

Create Subplot 1

In the first subplot, we will create a new axis that passes through y=0 using axisartist.Axes. We will also make the other spines invisible.

ax0 = fig.add_subplot(gs[0, 0], axes_class=axisartist.Axes)
ax0.axis["y=0"] = ax0.new_floating_axis(nth_coord=0, value=0, axis_direction="bottom")
ax0.axis["y=0"].toggle(all=True)
ax0.axis["y=0"].label.set_text("y = 0")
ax0.axis["bottom", "top", "right"].set_visible(False)

Create Subplot 2

In the second subplot, we will use axisartist.axislines.AxesZero to automatically create xzero and yzero axes. We will make the other spines invisible and set the xzero axis visible.

ax1 = fig.add_subplot(gs[0, 1], axes_class=axisartist.axislines.AxesZero)
ax1.axis["xzero"].set_visible(True)
ax1.axis["xzero"].label.set_text("Axis Zero")
ax1.axis["bottom", "top", "right"].set_visible(False)

Plot Data

Now that we have created our subplots, we can plot our data using np.sin(x).

x = np.arange(0, 2*np.pi, 0.01)
ax0.plot(x, np.sin(x))
ax1.plot(x, np.sin(x))

Show Plot

Finally, we can display our plot using plt.show().

plt.show()

Summary

In this tutorial, we learned how to create custom spines at specific positions using the Matplotlib library. We created a figure with two subplots and used axisartist.Axes and axisartist.axislines.AxesZero to create our spines. We also made the other spines invisible and set the xzero axis visible. Finally, we plotted our data and displayed our plot.

Other Python Tutorials you may like