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):

Notes:
- Instead of doing coordinate calculations, I also tried applying
shift={(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}- 4I think you have to use
axis direction csin this caseJake– Jake2014-03-21 16:10:42 +00:00CommentedMar 21, 2014 at 16:10 - @Jake: Yep that works. Please post that as an answer. Some info re:
axis csandaxis direction cswould be useful as well.Peter Grill– Peter Grill2014-03-21 19:05:43 +00:00CommentedMar 21, 2014 at 19:05
1 Answer1
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}
- 1Indeed. Very bizarre that a
+(0,12)produces incorrect results, yet-(0,-12)produces the desired result. Perhaps a bug then?Peter Grill– Peter Grill2014-03-21 16:15:47 +00:00CommentedMar 21, 2014 at 16:15
You mustlog in to answer this question.
Explore related questions
See similar questions with these tags.
