Matplotlib Infinite Line Visualization

MatplotlibMatplotlibBeginner
Practice Now

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

Introduction

Matplotlib is a Python library used to create visualizations for data analysis. In this tutorial, we will learn how to use axvline, axhline, and axline to draw infinite lines in Matplotlib.

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`"]) matplotlib(("`Matplotlib`")) -.-> matplotlib/PlotCustomizationGroup(["`Plot Customization`"]) python(("`Python`")) -.-> python/ControlFlowGroup(["`Control Flow`"]) 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`") matplotlib/PlotCustomizationGroup -.-> matplotlib/line_styles_colors("`Customizing Line Styles and Colors`") matplotlib/PlotCustomizationGroup -.-> matplotlib/titles_labels("`Adding Titles and Labels`") matplotlib/PlotCustomizationGroup -.-> matplotlib/legend_config("`Legend Configuration`") python/ControlFlowGroup -.-> python/for_loops("`For Loops`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/DataStructuresGroup -.-> python/sets("`Sets`") 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-48570{{"`Matplotlib Infinite Line Visualization`"}} matplotlib/importing_matplotlib -.-> lab-48570{{"`Matplotlib Infinite Line Visualization`"}} matplotlib/figures_axes -.-> lab-48570{{"`Matplotlib Infinite Line Visualization`"}} matplotlib/line_plots -.-> lab-48570{{"`Matplotlib Infinite Line Visualization`"}} matplotlib/line_styles_colors -.-> lab-48570{{"`Matplotlib Infinite Line Visualization`"}} matplotlib/titles_labels -.-> lab-48570{{"`Matplotlib Infinite Line Visualization`"}} matplotlib/legend_config -.-> lab-48570{{"`Matplotlib Infinite Line Visualization`"}} python/for_loops -.-> lab-48570{{"`Matplotlib Infinite Line Visualization`"}} python/lists -.-> lab-48570{{"`Matplotlib Infinite Line Visualization`"}} python/tuples -.-> lab-48570{{"`Matplotlib Infinite Line Visualization`"}} python/sets -.-> lab-48570{{"`Matplotlib Infinite Line Visualization`"}} python/importing_modules -.-> lab-48570{{"`Matplotlib Infinite Line Visualization`"}} python/numerical_computing -.-> lab-48570{{"`Matplotlib Infinite Line Visualization`"}} python/data_visualization -.-> lab-48570{{"`Matplotlib Infinite Line Visualization`"}} end

Draw vertical and horizontal lines

We can use axvline and axhline to draw vertical and horizontal lines respectively. Let's draw three horizontal lines at y=0, y=0.5, and y=1.0.

import matplotlib.pyplot as plt
import numpy as np

## Generate data
t = np.linspace(-10, 10, 100)
sig = 1 / (1 + np.exp(-t))

## Draw horizontal lines
plt.axhline(y=0, color="black", linestyle="--")
plt.axhline(y=0.5, color="black", linestyle=":")
plt.axhline(y=1.0, color="black", linestyle="--")

## Plot sigmoid function
plt.plot(t, sig, linewidth=2, label=r"$\sigma(t) = \frac{1}{1 + e^{-t}}$")
plt.xlim(-10, 10)
plt.xlabel("t")
plt.legend(fontsize=14)
plt.show()

Draw vertical line

We can use axvline to draw a vertical line at a given x position. Let's draw a vertical line at x=0.

import matplotlib.pyplot as plt
import numpy as np

## Generate data
t = np.linspace(-10, 10, 100)
sig = 1 / (1 + np.exp(-t))

## Draw horizontal lines
plt.axhline(y=0, color="black", linestyle="--")
plt.axhline(y=0.5, color="black", linestyle=":")
plt.axhline(y=1.0, color="black", linestyle="--")

## Draw vertical line
plt.axvline(color="grey")

## Plot sigmoid function
plt.plot(t, sig, linewidth=2, label=r"$\sigma(t) = \frac{1}{1 + e^{-t}}$")
plt.xlim(-10, 10)
plt.xlabel("t")
plt.legend(fontsize=14)
plt.show()

Draw arbitrary line

We can use axline to draw a line in any direction. Let's draw a line with slope 0.25 passing through the point (0, 0.5).

import matplotlib.pyplot as plt
import numpy as np

## Generate data
t = np.linspace(-10, 10, 100)
sig = 1 / (1 + np.exp(-t))

## Draw horizontal lines
plt.axhline(y=0, color="black", linestyle="--")
plt.axhline(y=0.5, color="black", linestyle=":")
plt.axhline(y=1.0, color="black", linestyle="--")

## Draw vertical line
plt.axvline(color="grey")

## Draw arbitrary line
plt.axline((0, 0.5), slope=0.25, color="black", linestyle=(0, (5, 5)))

## Plot sigmoid function
plt.plot(t, sig, linewidth=2, label=r"$\sigma(t) = \frac{1}{1 + e^{-t}}$")
plt.xlim(-10, 10)
plt.xlabel("t")
plt.legend(fontsize=14)
plt.show()

Draw diagonal lines

We can use axline with the transform parameter to draw diagonal lines with a fixed slope. Let's draw diagonal grid lines with a fixed slope of 0.5.

import matplotlib.pyplot as plt
import numpy as np

## Draw diagonal lines
for pos in np.linspace(-2, 1, 10):
    plt.axline((pos, 0), slope=0.5, color='k', transform=plt.gca().transAxes)

plt.ylim([0, 1])
plt.xlim([0, 1])
plt.show()

Summary

In this tutorial, we learned how to draw infinite lines in Matplotlib using axvline, axhline, and axline. We learned how to draw vertical and horizontal lines, arbitrary lines, and diagonal lines with a fixed slope. These functions are useful for marking special data values or for drawing grid lines to aid in data analysis.

Other Matplotlib Tutorials you may like