Simple Axis Direction

MatplotlibMatplotlibBeginner
Practice Now

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

Introduction

In this step-by-step lab, we will be learning how to create a simple axis direction in Python Matplotlib. Matplotlib is a data visualization library in Python that allows you to create static, animated, and interactive visualizations in Python programming. We will be using the library to create a simple axis direction diagram using the following steps.

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`"]) 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`") python/BasicConceptsGroup -.-> python/booleans("`Booleans`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/DataScienceandMachineLearningGroup -.-> python/data_visualization("`Data Visualization`") subgraph Lab Skills matplotlib/importing_matplotlib -.-> lab-48934{{"`Simple Axis Direction`"}} matplotlib/figures_axes -.-> lab-48934{{"`Simple Axis Direction`"}} python/booleans -.-> lab-48934{{"`Simple Axis Direction`"}} python/lists -.-> lab-48934{{"`Simple Axis Direction`"}} python/tuples -.-> lab-48934{{"`Simple Axis Direction`"}} python/importing_modules -.-> lab-48934{{"`Simple Axis Direction`"}} python/data_visualization -.-> lab-48934{{"`Simple Axis Direction`"}} end

Import Libraries

First, we import the necessary libraries. In this case, we will be importing matplotlib.pyplot and mpl_toolkits.axisartist.

import matplotlib.pyplot as plt
import mpl_toolkits.axisartist as axisartist

Create the Figure and Axes

Next, we create the figure and axes objects using the plt.subplots() function. We specify the figure size using the figsize parameter.

fig = plt.figure(figsize=(4, 2.5))
ax1 = fig.add_subplot(axes_class=axisartist.Axes)

Adjust the Subplot

We adjust the subplot using the fig.subplots_adjust() function to make room for the labels on the right side of the plot.

fig.subplots_adjust(right=0.8)

Set the Axis Labels

We set the axis labels for the left and right sides of the plot using the ax1.axis[] function. We also set the direction of the tick labels using the set_axis_direction() function.

ax1.axis["left"].major_ticklabels.set_axis_direction("top")
ax1.axis["left"].label.set_text("Left label")

ax1.axis["right"].label.set_visible(True)
ax1.axis["right"].label.set_text("Right label")
ax1.axis["right"].label.set_axis_direction("left")

Show the Plot

Finally, we show the plot using the plt.show() function.

plt.show()

Summary

In this lab, we have learned how to create a simple axis direction diagram using Python Matplotlib. We started by importing the necessary libraries, creating the figure and axes objects, adjusting the subplot, setting the axis labels, and finally showing the plot.

Other Matplotlib Tutorials you may like