Matplotlib - Draw horizontal lines

By xngo on July 23, 2020

Matplotlib does provide hlines() function to conveniently draw horizontal lines. Here are some examples.

import matplotlib
matplotlib.use('Agg') # Bypass the need to install Tkinter GUI framework
import matplotlib.pyplot as plt
# Draw a simple red line at y=0.5.
plt.axhline(y=0.5, color='r', linestyle='-')
# Draw a line at y=0.2 between x-axis 0.25 and 0.8.
plt.axhline(y=0.2, xmin=0.25, xmax=0.8, color=green_fluorescent, linestyle='--', linewidth=2)
# Save graph to file.


Horizontal lines - examples



About the author

Xuan Ngo is the founder of He currently lives in Montreal, Canada. He loves to write about programming and open source subjects.