Home > Backend Development > Python Tutorial > How can I add a horizontal line to an existing plot in Matplotlib?

How can I add a horizontal line to an existing plot in Matplotlib?

Mary-Kate Olsen
Release: 2024-11-18 11:58:02
Original
287 people have browsed it

How can I add a horizontal line to an existing plot in Matplotlib?

Adding Horizontal Lines to Existing Plots

To include a horizontal line on a previously generated plot, the axhline function can be utilized, specifically designed to create horizontal axis lines.

Example Usage:

To illustrate its functionality, consider plotting a horizontal line at y = 0.5:

import matplotlib.pyplot as plt

# Create plot
# ...

# Add horizontal line
plt.axhline(y=0.5, color='r', linestyle='-')

# Display plot
plt.show()
Copy after login

Output:

The resulting plot will include a horizontal red line located at y = 0.5.

Additional Notes:

  • The y parameter represents the y-coordinate of the line.
  • The color parameter specifies the line color, and linestyle defines the line style (e.g., dashed, solid).

The above is the detailed content of How can I add a horizontal line to an existing plot in Matplotlib?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template