You’re looking for axhline (a horizontal axis line). For example, the following will give you a horizontal line at y = 0.5:
import matplotlib.pyplot as plt
plt.axhline(y=0.5, color=’r’, linestyle=’-‘)
plt.show()
If you want to draw a horizontal line in the axes, you might also try ax.hlines() method. You need to specify y position and xmin and xmax in the data coordinate (i.e, your actual data range in the x-axis). A sample code snippet is:
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(1, 21, 200)
y = np.exp(-x)
fig, ax = plt.subplots()
ax.plot(x, y)
ax.hlines(y=0.2, xmin=4, xmax=20, linewidth=2, color=’r’)
plt.show()
The snippet above will plot a horizontal line in the axes at y=0.2. The horizontal line starts at x=4 and ends at x=20. The generated image is: