How can I draw a horizontal line in Matplotlib? 2

How can I draw a horizontal line in Matplotlib?

To draw a horizontal line in Matplotlib, one can use the “axhline” function which takes in the desired y-value and can be customized with parameters such as the line color, style, and width. This function allows for easy creation of horizontal lines on a graph or plot in Matplotlib, making it a useful tool for data visualization. By specifying the y-value, users can accurately place the horizontal line at any desired location on the plot. Additionally, this function can be combined with other Matplotlib features such as subplots and legends to further enhance the visual presentation.

Draw a Horizontal Line in Matplotlib (With Examples)


You can use the following syntax to draw a horizontal line in Matplotlib:

import matplotlib.pyplotas plt

#draw vertical line at y=10
plt.axhline(y=10)

The following examples show how to use this syntax in practice with the following pandas DataFrame:

import pandas as pd

#create DataFrame
df = pd.DataFrame({'x': [1, 2, 3, 4, 5, 6, 7, 8],
                   'y': [5, 7, 8, 15, 26, 39, 45, 40]})

#view DataFrame
df

	x	y
0	1	5
1	2	7
2	3	8
3	4	15
4	5	26
5	6	39
6	7	45
7	8	40

Example 1: Draw One Horizontal Line

The following code shows how to draw one horizontal line on a Matplotlib plot:

import matplotlib.pyplotas plt

#create line plot
plt.plot(df.x, df.y)

#add horizontal line at y=10
plt.axhline(y=10, color='red', linestyle='--')

How

Example 2: Draw Multiple Horizontal Lines

The following code shows how to draw multiple horizontal lines on a Matplotlib plot:

import matplotlib.pyplotas plt

#create line plot
plt.plot(df.x, df.y)

#add horizontal line at y=10
plt.axhline(y=10, color='red', linestyle='--')

#add horizontal line at y=30
plt.axhline(y=30, color='black', linestyle='-')

Multiple horizontal lines in Matplotlib

Example 3: Draw Multiple Horizontal Lines with Legend

The following code shows how to draw multiple horizontal lines on a Matplotlib plot and add a legend to make the lines easier to interpret:

import matplotlib.pyplotas plt

#create line plot
plt.plot(df.x, df.y)

#add horizontal line at y=10
plt.axhline(y=10, color='red', linestyle='--', label='First Line')

#add horizontal line at y=30
plt.axhline(y=30, color='black', linestyle='-', label='Second Line')

#add legend
plt.legend()

Horizontal

Note: Refer to the for a complete list of colors and linestyles you can apply to horizontal lines.

Cite this article

stats writer (2024). How can I draw a horizontal line in Matplotlib?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-draw-a-horizontal-line-in-matplotlib/

stats writer. "How can I draw a horizontal line in Matplotlib?." PSYCHOLOGICAL SCALES, 2 May. 2024, https://scales.arabpsychology.com/stats/how-can-i-draw-a-horizontal-line-in-matplotlib/.

stats writer. "How can I draw a horizontal line in Matplotlib?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-draw-a-horizontal-line-in-matplotlib/.

stats writer (2024) 'How can I draw a horizontal line in Matplotlib?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-draw-a-horizontal-line-in-matplotlib/.

[1] stats writer, "How can I draw a horizontal line in Matplotlib?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, May, 2024.

stats writer. How can I draw a horizontal line in Matplotlib?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top