0

I would like to draw a horizontal line with matplotlib's plt.axhline() function, but I want the horizontal line to stop at the absolute value of 5 on the x-axis. How do I set xmax in plt.axhline() to stop at 5?

plt.figure()
plt.plot(np.arange(-60, 60, 20), np.arange(0, 1.2, 0.2))
plt.axhline(y = 0.5, xmax = 5, c= 'r')
JonnDough
  • 827
  • 6
  • 25
  • `plt.axhlines` takes `xmin` and `xmax` in axes coordinates. If you want to use data coordinates, you could use [`plt.hlines`](https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.hlines.html#matplotlib.pyplot.hlines) instead – tmdavison Nov 03 '21 at 10:59

1 Answers1

4

You need to use plt.hlines instead, also specify a xmin and change c to color .

import matplotlib.pyplot as plt
import numpy as np
xmin = -65
plt.figure()
plt.plot(np.arange(-60, 60, 20), np.arange(0, 1.2, 0.2))
plt.hlines(y = 0.5, xmin=xmin , xmax = 5, color= 'r')
plt.xlim(left=xmin);
Tobias P. G.
  • 827
  • 8
  • 15