Home > Backend Development > Python Tutorial > How can I draw horizontal lines in Matplotlib plots?

How can I draw horizontal lines in Matplotlib plots?

DDD
Release: 2024-11-18 21:26:02
Original
867 people have browsed it

How can I draw horizontal lines in Matplotlib plots?

Plotting Horizontal Lines in Matplotlib

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:

  • y: The y-coordinate of the line.
  • color (optional): The color of the line.
  • linestyle (optional): The style of the line, such as solid, dashed, or dotted.

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()
Copy after login

In this code:

  • plt.axhline(y=0.5) creates a horizontal line at y = 0.5.
  • color='r' sets the line color to red.
  • linestyle='-' indicates a solid line.

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!

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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template