Home > Backend Development > Python Tutorial > How to Add a Horizontal Line to an Existing Plot?

How to Add a Horizontal Line to an Existing Plot?

Susan Sarandon
Release: 2024-11-21 02:05:12
Original
776 people have browsed it

How to Add a Horizontal Line to an Existing Plot?

Generate a Horizontal Line on an Existing Plot

Question: Enhance an existing plot by incorporating a horizontal line.

Solution:

To include a horizontal line on your plot, utilize axhline, which represents a horizontal line on the x-axis. This versatile function enables you to customize the line's position, color, and style.

For instance, to plot a horizontal line at y = 0.5, your code would look like this:

import matplotlib.pyplot as plt

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

# Display the plot
plt.show()
Copy after login

Here's an illustration of the resulting figure:

[Image of a sample figure with a horizontal line at y = 0.5]

By leveraging the capabilities of axhline, you can effortlessly enhance your plots with horizontal lines, adding clarity and depth to your visualizations.

The above is the detailed content of How to Add a Horizontal Line to an Existing Plot?. 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