Open In App

Plot a Horizontal line in Matplotlib

Last Updated : 02 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In Matplotlib, we can draw horizontal lines on a plot to indicate thresholds, reference points or important levels in the data. These lines can be used to highlight specific values for better visualization. We can create a single horizontal line, which is useful for marking a single reference level, or multiple horizontal lines, which help in highlighting multiple levels within the plot. Let’s understand the different methods to do this.

Using hlines()

The hlines() function is used to draw one or multiple horizontal lines across a specified range on the x-axis. It is particularly useful when you need to highlight multiple levels within a plot. With hlines(), you can customize various aspects of the lines, such as color, linestyle and thickness, to enhance visualization.

Example: In this example, we draw three horizontal lines at specific y-values, setting their x-range, colors, linestyles and thicknesses for customization.

Python
import matplotlib.pyplot as plt

# Draw horizontal lines at y=1, y=2 and y=3
plt.hlines(y=[1, 2, 3], xmin=0, xmax=5, colors=['b', 'g', 'r'], linestyles=['-', '--', ':'])
plt.show()

Output

Output

Using hlines()

Explanation: plt.hlines() draws lines at y=1, 2, 3 from x=0 to 5, with specified colors (blue, green, red) and styles (solid, dashed, dotted) for distinction.

Using axhline()

axhline() function is useful when you need to draw a horizontal line that spans the entire width of the plot, regardless of the x-axis limits. It is commonly used to mark thresholds, reference levels, or important divisions in data visualization.

Example 1: In this example, we draw a single horizontal line at a specific y-value, setting its color, linestyle and thickness for customization.

Python
import matplotlib.pyplot as plt

# single horizontal line at y = 2
plt.axhline(y=2, color='r', linestyle='-')
plt.show()

Output

Output

Using axhline()

Explanation: plt.axhline() draws a single horizontal line at y=2 with a red color and solid style, then plt.show() displays the plot.

Example 2: In this example, we draw multiple horizontal lines at specific y-values, setting their colors, linestyles and thicknesses for customization.

Python
import matplotlib.pyplot as plt

# Multiple horizontal lines at different y-values
plt.axhline(y=1, color='b', linestyle='-')
plt.axhline(y=2, color='g', linestyle='--')
plt.axhline(y=3, color='r', linestyle=':')

plt.show()

Output

Output

Using axhline()

Explanation: plt.axhline() draws horizontal lines at y=1, 2, 3 with different colors (blue, green, red) and styles (solid, dashed, dotted), then plt.show() displays the plot.

Using plot()

plot() function can also be used to create horizontal lines by specifying a range for the x-values while keeping the y-values constant. This method provides greater flexibility in customizing individual line properties, such as markers, colors and line styles.

Example: In this example, we draw three horizontal lines at specific y-values, setting their x-range, colors, linestyles and thicknesses for customization.

Python
import matplotlib.pyplot as plt

x = [0, 1]  # x-range for each line
plt.plot(x, [1, 1], color='b', linestyle='-', label='y=1')
plt.plot(x, [2, 2], color='g', linestyle='--', label='y=2')
plt.plot(x, [3, 3], color='r', linestyle=':', label='y=3')

plt.legend()
plt.show()

Output

Output

Using plot()

Explanation: plt.plot() draws horizontal lines at y=1, 2, 3 over x=0 to 1 with different colors (blue, green, red) and styles (solid, dashed, dotted). plt.legend() adds labels, and plt.show() displays the plot.

Using axhspan()

axhspan() function is used to highlight a horizontal band across a specific y-range, creating a shaded region in the plot. This method is particularly useful for emphasizing areas of interest, such as confidence intervals, thresholds or regions that require special attention.

Example: In this example, we highlight a horizontal band across a specific y-range, setting its color and transparency for customization.

Python
import matplotlib.pyplot as plt

# Highlight a horizontal band from y=1 to y=2
plt.axhspan(1, 2, color='gray', alpha=0.3)
plt.show()

Output

Output

Using axhspan()

Explanation: plt.axhspan(1, 2) highlights the region between y=1 and y=2 with a gray shade (color=’gray’) and alpha=0.3 for transparency, then plt.show() displays the plot.



Next Article
Article Tags :
Practice Tags :

Similar Reads