Examples of how to plot horizontal lines with matplotlib:
Table of contents
Plot horizontal lines
To plot horizontal lines, a solution is to use axhline, example
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')
Dashed lines
plt.axhline(y=0.5,color='gray',linestyle='--')
References
Links | Site |
---|---|
matplotlib.pyplot.axhline | matplotlib.org |
Python matplotlib.pyplot.axhline() Examples | programcreek.com |