5

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:

enter image description here

Compare with the old plot shown in the mentioned answer (code below):

enter image description here

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()
11
  • it depends on your min max parameters, no? Commented Aug 2, 2019 at 14:16
  • axvline did not intentionally change behaviour. The question does not allow to see what's undesired. Commented Aug 2, 2019 at 14:16
  • I infer from the linked question that since the functions use coordintate system, min=0 and max =1 should span the full range of the axis, but now it's only drawing a line from values x=0 to x=1, while expected is x=0, x=5 Commented Aug 2, 2019 at 14:18
  • 1
    You're right, the image in that answer is wrong. The red line is off - it's neither at y=0.5 in data- nor in axes coordinates. So I think one can assume it to be generated by a code that is not shown in the answer. But if you replace the wrong image with the one you get with that code, the answer is correct, rigth? It might make sense to just edit that answer. Commented Aug 2, 2019 at 14:28
  • 1
    Ok, I answered that question below; maybe you want to edit your question with what you really wanted to know, such that Q&A are coherent?! Commented Aug 2, 2019 at 14:40

1 Answer 1

6

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()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.