The world’s leading publication for data science, AI, and ML professionals.

How To Plot Vertical And Horizontal Lines in Matplotlib

Drawing horizontal and vertical lines in Python plots with matplotlib

Photo by Kelly Sikkema on Unsplash
Photo by Kelly Sikkema on Unsplash

Matplotlib is the most commonly used library for plotting static or interactive visualizations in Python. One common task when working with plots and graphs is the need to draw lines at specific locations in the plot. For example, you may want to draw a horizontal or vertical line to mark a threshold value or simply to highlight a particular data point.

In this tutorial, we will demonstrate how to use matplotlib functions to plot vertical and horizontal lines in an existing plot. We will also discuss some of the options and considerations you should keep in mind when adding lines to your plots.


First, let’s create a base plot that visualises sin – a well known trigonometric function – that we will be referencing throughout this article in order to demonstrate how to plot additional lines.

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 20, 100)
y = np.sin(x)

fig, ax = plt.subplots()
ax.plot(x, y)
plt.show()

And here’s the output plot (I hope it looks familiar to you!):

Using matplotlib to visualise the sin (trigonometric function) - Source: Author
Using matplotlib to visualise the sin (trigonometric function) – Source: Author

Plot a horizontal line

Now in order to plot a horizontal line across the axis, we can make use of the [matplotlib.pyplot.axhline()](https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.axhline.html) function that takes the following three arguments:

  • y : This is the exact data point on the y-axis where the horizontal line will be positioned.
  • xmin: This is a float taking values between 0 and 1 and denotes the line’s starting point with respect to the x-axis. For example, if set to 0.5, the horizontal line will start from the middle of the plot, at the specified y location. The value of 0 denotes teh far left of the plot, whereas 1 corresponds to the far left of the plot.
  • xmax: Similarly, this is a float parameter ranging from 0 and 1 and denotes the endpoint of the plotted horizontal line. In the same way, the value of 0 denotes the far left of the plot, whereas 1 corresponds to the far left of the plot

In the example below, we add a horizontal line at the 0.75 y-axis point, starting and ending from the specified xmin and xmax values:

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 20, 100)
y = np.sin(x)

fig, ax = plt.subplots()
ax.plot(x, y)

ax.axhline(y=0.75, color='r')

plt.show()

The resulting plot should look like the one shared below:

The sin graph with a horizontal line - Source: Author
The sin graph with a horizontal line – Source: Author

If we would like to plot a line without limiting it to the xmin and xmax positions (they default to 0 and 1 respectively), we could simply rearrange the function call into

ax.axhline(y=0.75, color='r')
Horizontal line with no xmin and xmax - Source: Author
Horizontal line with no xmin and xmax – Source: Author

Plot a vertical line

Likewise, to plot a vertical line across the axis we need to call the [matplotlib.pyplot.axvline()](https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.axvline.html) function that takes the following three arguments:

  • x: This is the exact data point on the y-axis where the horizontal line will be positioned.
  • ymin: This is a float taking values between 0 and 1 and denotes the line’s starting point with respect to the y-axis. The value of 0 denotes bottom of the plot, whereas 1 corresponds to the top of the plot.
  • ymax: Similarly, this is a float parameter ranging from 0 and 1 and denotes the endpoint of the plotted vertical line. In the same way, the value of 0 denotes the bottom of the plot, whereas 1 corresponds to the top of the plot

Now let’s assume that we would like to plot a vertical line to our existing graph that would cross the x-axis at the 7.5 value.

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 20, 100)
y = np.sin(x)

fig, ax = plt.subplots()
ax.plot(x, y)

ax.axvline(x=7.5, color='y')

plt.show()

The axvline() function will create a vertical line at the specified point, from the top to the very bottom of the graph (since we haven’t specified ymin and ymax):

A vertical line plotted with axvline - Source: Author
A vertical line plotted with axvline - Source: Author

Final Thoughts

In conclusion, the Matplotlib library in Python allows for the creation of horizontal and vertical lines in plots and graphs through the use of the axhline() and axvline() functions, respectively.

These functions take arguments for the position of the line on the x or y axis, as well as optional arguments for the starting and ending points of the line relative to the plot. By using these functions, it is easy to highlight specific data points or add thresholds to plots and graphs in Python.


Become a member and read every story on Medium. Your membership fee directly supports me and other writers you read. You’ll also get full access to every story on Medium.

Join Medium with my referral link – Giorgos Myrianthous


Related articles you may also like

How to Place the Legend Outside the Plot in Matplotlib


How to Plot Logarithmic Axes With Matplotlib in Python


How to Save Plots To Image Files Using Matplotlib


Related Articles