Learning is a mess - 3 months ago 13

Python Question

I'm trying to plot some data with Matplotlib (Python library) and to add an horizontal line, that would not cover the full axis range but start around the middle and finish on the right axis.

I am using:

`plt.axhline(y=1.75,xmin=0.5)`

where y specifies the height of the line in data units, but xmin (as well as xmax) need to be defined in axis units (=0 for the beginning of axis and =1 at the end). Though I only know the point I want my line to start in data units, is there a method/function to convert from one to the other?

Answer

Just draw a line with `plt`

```
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0.2,10,100)
fig, ax = plt.subplots()
ax.plot(x, 1/x)
ax.plot(x, np.log(x))
ax.set_aspect('equal')
ax.grid(True, which='both')
y = 1.25
xmin = 2
xmax = ax.get_xlim()[1]
ax.plot([xmin, xmax], [y, y], color='k')
```

which gives me:

Source (Stackoverflow)

Comments