3

I was trying to improve the solution atDrawing lines in a plot and give them a label? by using Jake's answer atHow to access \xmin, \xmax, \ymin, \ymax from within PGFplots axis environment.So, Jake's answer produces the brown dotted line (the x-axis) with

(current axis.left of origin) -- (current axis.right of origin)

I thought a simple use oftikz'scalc library would allow me to adjust the vertical position of the horizontal line. If I want a horizontal line aty=12, for example, I simply use

($(current axis.left of origin)+(axis cs: 0,12)$) -- ($(current axis.right of origin)+(axis cs: 0,12)$)

where I have applied a+ (axis cs: 0,12) and same for the other coordinate.

Well, that is great in theory, but doesn't work. Instead, it produces the dashed line in red (which is only half the desired line):

enter image description here

Notes:

  • Instead of doing coordinate calculations, I also tried applyingshift={(axis cs: 0,12)} option but that had no effect on the output.

Code:

\documentclass{article}\usepackage{pgfplots}\usetikzlibrary{calc}\begin{document}\begin{tikzpicture}\begin{axis}    \addplot {x*x};        \draw [ultra thick, dotted, draw=brown]         (current axis.left of origin) --         (current axis.right of origin);            \draw [ultra thick, dashed, draw=red]         ($(current axis.left of origin) +(axis cs: 0,12)$) --         ($(current axis.right of origin)+(axis cs: 0,12)$);\end{axis}\end{tikzpicture}\end{document}
askedMar 21, 2014 at 15:56
Peter Grill's user avatar
2
  • 4
    I think you have to useaxis direction cs in this caseCommentedMar 21, 2014 at 16:10
  • @Jake: Yep that works. Please post that as an answer. Some info re:axis cs andaxis direction cs would be useful as well.CommentedMar 21, 2014 at 19:05

1 Answer1

2

I really don't know why but this works for me

\documentclass{article}\usepackage{pgfplots}\usetikzlibrary{calc}\pgfplotsset{compat=1.10}\begin{document}\begin{tikzpicture}\begin{axis}    \addplot {x*x};    \draw [ultra thick, dotted, draw=brown]         (current axis.left of origin) --         (current axis.right of origin);    \draw [ultra thick, draw=red]         ($(current axis.left of origin)-(axis cs:0,-12)$) --         ($(current axis.right of origin)+(axis cs:0,12)$);\end{axis}\end{tikzpicture}\end{document}

enter image description here

answeredMar 21, 2014 at 16:13
Red's user avatar
1
  • 1
    Indeed. Very bizarre that a+(0,12) produces incorrect results, yet-(0,-12) produces the desired result. Perhaps a bug then?CommentedMar 21, 2014 at 16:15

You mustlog in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.