Creating Matplotlib Horizontal and Vertical Axes

PythonPythonBeginner
Practice Now

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

Introduction

In this tutorial, we will learn how to create lines and rectangles that span the axes in either the horizontal or vertical direction, and lines than span the axes with an arbitrary orientation using Matplotlib library in Python.

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 python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) matplotlib(("`Matplotlib`")) -.-> matplotlib/BasicConceptsGroup(["`Basic Concepts`"]) matplotlib(("`Matplotlib`")) -.-> matplotlib/PlottingDataGroup(["`Plotting Data`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/DataScienceandMachineLearningGroup(["`Data Science and Machine Learning`"]) python/BasicConceptsGroup -.-> python/comments("`Comments`") matplotlib/BasicConceptsGroup -.-> matplotlib/importing_matplotlib("`Importing Matplotlib`") matplotlib/BasicConceptsGroup -.-> matplotlib/figures_axes("`Understanding Figures and Axes`") matplotlib/PlottingDataGroup -.-> matplotlib/line_plots("`Line Plots`") 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 python/comments -.-> lab-48566{{"`Creating Matplotlib Horizontal and Vertical Axes`"}} matplotlib/importing_matplotlib -.-> lab-48566{{"`Creating Matplotlib Horizontal and Vertical Axes`"}} matplotlib/figures_axes -.-> lab-48566{{"`Creating Matplotlib Horizontal and Vertical Axes`"}} matplotlib/line_plots -.-> lab-48566{{"`Creating Matplotlib Horizontal and Vertical Axes`"}} python/tuples -.-> lab-48566{{"`Creating Matplotlib Horizontal and Vertical Axes`"}} python/importing_modules -.-> lab-48566{{"`Creating Matplotlib Horizontal and Vertical Axes`"}} python/numerical_computing -.-> lab-48566{{"`Creating Matplotlib Horizontal and Vertical Axes`"}} python/data_visualization -.-> lab-48566{{"`Creating Matplotlib Horizontal and Vertical Axes`"}} end

Import Libraries

Firstly, we need to import the required libraries which are Matplotlib and NumPy. NumPy is used to generate the data.

import matplotlib.pyplot as plt
import numpy as np

Generate Data

We will use NumPy to generate data which we will use to plot the graph.

t = np.arange(-1, 2, .01)
s = np.sin(2 * np.pi * t)

Create a Figure and Axes

We need to create a figure and axes object to plot the graph.

fig, ax = plt.subplots()

Plot Data

Plot the data using the plot() function.

ax.plot(t, s)

Add Horizontal Line

Add horizontal lines using the axhline() function.

## Thick red horizontal line at y=0 that spans the xrange.
ax.axhline(linewidth=8, color='#d62728')
## Horizontal line at y=1 that spans the xrange.
ax.axhline(y=1)

Add Vertical Line

Add vertical lines using the axvline() function.

## Vertical line at x=1 that spans the yrange.
ax.axvline(x=1)
## Thick blue vertical line at x=0 that spans the upper quadrant of the yrange.
ax.axvline(x=0, ymin=0.75, linewidth=8, color='#1f77b4')

Add Infinite Line

Add an infinite line going through (0, 0) to (1, 1) using the axline() function.

## Infinite black line going through (0, 0) to (1, 1).
ax.axline((0, 0), (1, 1), color='k')

Add Rectangle

Add a rectangle using the axhspan() and axvspan() functions.

## 50%-gray rectangle spanning the axes' width from y=0.25 to y=0.75.
ax.axhspan(0.25, 0.75, facecolor='0.5')
## Green rectangle spanning the axes' height from x=1.25 to x=1.55.
ax.axvspan(1.25, 1.55, facecolor='#2ca02c')

Show Plot

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

plt.show()

Summary

In this tutorial, we learned how to create lines and rectangles that span the axes in either the horizontal or vertical direction, and lines than span the axes with an arbitrary orientation using Matplotlib library in Python. We learned how to import the required libraries, generate data, create a figure and axes, plot data, and add horizontal and vertical lines, infinite line, and rectangle to the graph.

Other Python Tutorials you may like