I want to plot a vertical line that spans the entire y axis located for example in the x=.25 position of the x axis , not the data axis .
According to this answer (which is apparently not entirely accurate) the axhline,axvline
functions would draw a horizontal/vertical line in the axes coordinates , as shown in:
The method axhline and axvline are used to draw lines at the axes coordinate
But this does not seem to work. The axhline docs say :
y position in data coordinates of the horizontal line.
and sure enough, the code given in the answer above displays:
Compare with the old plot shown in the mentioned answer (code below):
Did this change recently or am I missing something very obvious? If it did change, how would I draw a line in the axes coordinate now?
I'm using Python 3.7.3 and matplotlib
3.1.0.
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 5, 100)
y = np.sin(x)
fig, ax = plt.subplots()
ax.plot(x, y)
ax.axhline(y=0.5, xmin=0.0, xmax=1.0, color='r')
ax.hlines(y=0.6, xmin=0.0, xmax=1.0, color='b')
plt.show()
Answering the question
If I wanted to plot a vertical line that spanned the entire y axis located in the x=.25 position of the x axis, not the data axis. How would I do that?
In that case both of the x coordinates of that line are 0.25
and the y coordinates are 0
for the lower end and 1
for the upper end. The transform of the line is set to the axes coordinate system ax.transAxes
.
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.set(xlim=(0.5,1.5), ylim=(-50,50))
ax.plot([0.25,0.25],[0,1], transform=ax.transAxes)
plt.show()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.