Plot a Horizontal line in Matplotlib
Last Updated :
02 Apr, 2025
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
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
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
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
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
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.
Similar Reads
Line chart in Matplotlib - Python
Matplotlib is a data visualization library in Python. The pyplot, a sublibrary of Matplotlib, is a collection of functions that helps in creating a variety of charts. Line charts are used to represent the relation between two data X and Y on a different axis. In this article, we will learn about lin
6 min read
How to plot a dashed line in matplotlib?
Matplotlib is used to create visualizations and plotting dashed lines is used to enhance the style and readability of graphs. A dashed line can represent trends, relationships or boundaries in data. Below we will explore how to plot and customize dashed lines using Matplotlib. To plot dashed line:Sy
2 min read
How to use matplotlib plot inline?
Matplotlib is a Python library that helps in drawing graphs. It is used in data visualization and graph plotting. Matplotlib Plot Inline is a package that supports Matplotlib to display plots directly inline and save them to notebooks. In this article, we'll cover the following:Â What is Matplotlib I
3 min read
Plot a Vertical line in Matplotlib
Plotting vertical lines is a technique in data visualization which is used to highlight specific data points, thresholds or regions on a graph. Whether we're marking key values, dividing data into segment, vertical lines can make our charts more informative. In this article, we'll see different meth
3 min read
How to Change Line Color in Matplotlib?
Matlab's plotting functions are included in Python by the Inclusion of the library Matplotlib. The library allows the plotting of the data of various dimensions without ambiguity in a plot. The library is widely used in Data Science and Data visualization. In this article, we will discuss how to cha
3 min read
Line plot styles in Matplotlib
Line plots are important data visualization elements that can be used to identify relationships within the data. Using matplotlib.pyplot.plot() function we can plot line plots. Styling tools in this helps us customize line plots according to our requirements which helps in better representations. Li
3 min read
matplotlib.pyplot.axhline() in Python
Matplotlib is a library in Python and it is numerical - mathematical extension for NumPy library. Pyplot is a state-based interface to a Matplotlib module which provides a MATLAB-like interface. matplotlib.pyplot.axhline() Function The axhline() function in pyplot module of matplotlib library is use
2 min read
Plot Multiple lines in Matplotlib
In this article, we will learn how to plot multiple lines using matplotlib in Python. Let's discuss some concepts:Matplotlib: Matplotlib is an amazing visualization library in Python for 2D plots of arrays. Matplotlib is a multi-platform data visualization library built on NumPy arrays and designed
6 min read
Matplotlib.axes.Axes.add_line() in Python
Matplotlib is a library in Python and it is numerical - mathematical extension for NumPy library. The Axes Class contains most of the figure elements: Axis, Tick, Line2D, Text, Polygon, etc., and sets the coordinate system. And the instances of Axes supports callbacks through a callbacks attribute.
2 min read
How to Plot Logarithmic Axes in Matplotlib?
Logarithmic axes help visualize data that spans several orders of magnitude by scaling the axes logarithmically instead of linearly. In Matplotlib, you can easily set logarithmic scales for the x-axis, y-axis, or both using simple methods. Letâs explore straightforward ways to apply logarithmic scal
2 min read