When working with plots, adding horizontal lines can enhance visualizations and highlight specific values. In this regard, Matplotlib's powerful features provide a convenient way to incorporate horizontal lines into your plots.
Adding Horizontal Lines Using axhline()
The axhline() function is designed specifically for plotting horizontal lines. It takes two primary arguments:
Example:
To demonstrate the use of axhline(), let's plot a horizontal line at y = 0.5.
import matplotlib.pyplot as plt plt.axhline(y=0.5, color='r', linestyle='-') plt.show()
In this code:
When you run the code, you'll see a plot with a horizontal red line at y = 0.5.
The above is the detailed content of How can I draw horizontal lines in Matplotlib plots?. For more information, please follow other related articles on the PHP Chinese website!