How to plot horizontal lines with matplotlib ?

Published: June 21, 2019

DMCA.com Protection Status

Examples of how to plot horizontal lines with matplotlib:

Plot horizontal lines

To plot horizontal lines, a solution is to use axhline, example

How to plot horizontal lines with matplotlib ?
How to plot horizontal lines with matplotlib ?

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 2*np.pi, 1000)
y1 = np.sin(x)

f = plt.figure()

ax = f.add_subplot(111)

plt.plot(x, y1)

plt.axhline(y=0.5)
plt.axhline(y=-0.5)

plt.title('How to plot a vertical line with matplotlib ?', fontsize=8)

plt.xlim(0, 2.0*np.pi)
plt.ylim(-1.5, 1.5)

plt.savefig('matplotlib_horizontal_line_03.png', bbox_inches='tight')
plt.show()

Change the line color

plt.axhline(y=0.5,color='gray')

How to plot horizontal lines with matplotlib ?
How to plot horizontal lines with matplotlib ?

Dashed lines

plt.axhline(y=0.5,color='gray',linestyle='--')

How to plot horizontal lines with matplotlib ?
How to plot horizontal lines with matplotlib ?

References

Links Site
matplotlib.pyplot.axhline matplotlib.org
Python matplotlib.pyplot.axhline() Examples programcreek.com
Image

of