2

According to the documentation page: http://matplotlib.sourceforge.net/api/pyplot_api.html the way to use the axvline is like

axvline(x=0, ymin=0, ymax=1)

However, this does not work in my computer. Nothing is drawn. Rather, simply

axvline(x=0)

without setting the ymin and ymax works.

I am not sure whether this is a bug. Or maybe I missed something subtle?

matplotlib.__version__
'0.99.1.1'

uname -a
Linux pc20172 2.6.32-41-generic #94-Ubuntu SMP Fri Jul 6 18:00:34 UTC 2012 x86_64 GNU/Linux

Edit: a minimum code to reproduce the problem.

from pylab import *
ion()
plot([1,2])
axvline(x=0.5, ymin=1, ymax=2) # No vertical line is drawn.

clf() # Clear the figure to redo the plot.
plot([1,2])
axvline(x=0.5) # Now the desired vertical line is drawn.
Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
FJDU
  • 1,425
  • 2
  • 15
  • 21
  • Are you sure nothing is drawn? If this is the only line on the plot, try `axvline(x=0, ymin=0, ymax=1, linewidth=100)` to make it bigger. – DSM Aug 08 '12 at 13:51
  • @DSM I don't think there is any thing wrong with the linewidth. I added a few lines of code that you may test. – FJDU Aug 08 '12 at 14:52

2 Answers2

4

From the documentation in help(axvline):

Draw a vertical line at x from ymin to ymax. With the default values of ymin = 0 and ymax = 1, this line will always span the vertical extent of the axes, regardless of the ylim settings, even if you change them, eg. with the :meth:set_ylim command. That is, the vertical extent is in axes coords: 0=bottom, 0.5=middle, 1.0=top but the x location is in data coordinates.

So

axvline(x=0.5, ymin=1, ymax=2) # No vertical line is drawn.

is drawing a line just off the plot area. If you make the linewidth bigger, you can see this:

pic showing just-out-of-bounds effect

DSM
  • 342,061
  • 65
  • 592
  • 494
  • Thanks for your answer! I also just found this out seconds ago. It is a bit misleading that x and y are using different coordinate systems. BTW, your trick of using a very large linewidth is very useful! I didn't know that increasing the width can also increase the length of a line. – FJDU Aug 08 '12 at 15:04
  • @user789977: it doesn't increase the length of the line itself, but there's actually a little cap on the end that it's easy not to notice unless it's really big. See my answer [to this question](http://stackoverflow.com/questions/10297220/matplotlib-linewidth-is-added-to-the-length-of-a-line/10297860#10297860). I should admit though that this only happened to work here by fluke: I originally thought that it was just that the line was hugging a plot edge in one of the cases and it was hard to see. – DSM Aug 08 '12 at 15:09
3

OK. Now I realize that the ymin and ymax are not in data coordinate, but rather in the "normalized" coordinate. So, 0 means bottom of the plot, while 1 means the top.

FJDU
  • 1,425
  • 2
  • 15
  • 21