Plot a horizontal line on a given plot
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
A horizontal line on a chart is usually a reference value such as a threshold, average, target, or control limit. In Matplotlib, the right tool depends on whether the line should span the entire axes or only a specific x-range, with axhline covering the first case and hlines covering the second.
Use axhline for a Full-Width Reference Line
If the line should run across the whole plotting area at a fixed y-value, use axhline.
This is the usual answer for a baseline or target that applies to the whole chart. It is clearer than drawing a manual two-point line with plot because the intent is obvious from the API.
Use hlines for a Partial Span
If the horizontal line should cover only part of the x-axis, use hlines instead.
That is useful when the reference applies only to a subsection of the plot, such as a maintenance window, a highlighted interval, or a band of interest.
Add Meaning With Labels and Annotations
A horizontal line is more useful when the viewer knows what it represents. Add a legend label or annotate the line directly.
This matters because an unlabeled line often forces the reader to guess whether it is an average, an SLA threshold, or just another data series.
Plot a Computed Reference Line
Often the y-value is derived from the data instead of being hard-coded. A mean or median line is a common example.
This is a simple but effective way to show whether points cluster above or below a computed baseline.
Use the Axes Object in Multi-Plot Figures
When your figure contains subplots, attach the line to the correct axes object rather than relying on the implicit current axes.
This avoids drawing the line on the wrong subplot, which is a common mistake in larger notebooks and scripts.
Common Pitfalls
The most common pitfall is using a regular plot call to fake a horizontal reference line when axhline or hlines would express the intent more clearly.
Another issue is forgetting that axhline spans the full axes width, not a limited x-range. If the line should stop early, hlines is the right tool.
Developers also often forget to label the line, which leaves the reader to infer its meaning from context. If the line matters enough to add, it usually matters enough to annotate.
Finally, in figures with several axes, make sure you are calling the method on the intended axes object rather than whichever subplot happens to be current.
Summary
- Use
axhlinefor a horizontal line that spans the whole axes. - Use
hlineswhen the line should cover only part of the x-range. - Label or annotate the line so its meaning is obvious.
- Compute the y-value dynamically when the reference line represents a statistic.
- Prefer the explicit axes API when working with subplots.
Related reading

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.