Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

Since the axis environment is within a tikzpicture I thought that you could use the usual \draw commands within the axis environment.

I am confused as to why the code below produces the red line outside of the axis?

enter image description here

Furthermore if you comment out just the \addplot call, both the red and blue lines are not drawn.

Questions:

  1. Why is the red line drawn outside of the axis environment?
  2. Is there a way to use a \draw command within the axis environment to draw lines bwteen axis cs coordinates?
  3. Why does commenting out the \addplot result in no lines being drawn?

Code:

\documentclass{article}
\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}
\begin{axis}[clip=false,ultra thick]
    \draw [red] (axis cs: 0.2,0.2) -- (axis cs: 0.6,0.8);
    \addplot [mark=*,blue] coordinates{( 0.2,1.2) (0.6,1.8)};
\end{axis}
\end{tikzpicture}
\end{document}
share|improve this question
4  
Notice that the y axis of the plot starts from 1.2 and the straight line has y coordinates from 0.2 to 0.8, so obviously it will fall outside the plot. –  Gonzalo Medina Jan 9 '12 at 0:52
    
Wow!! How embarrassing... I guess that explains #1 and #2? But if you comment out the \addplot the range of the y-axis changes such that the red line should have been drawn? –  Peter Grill Jan 9 '12 at 1:05

1 Answer 1

up vote 6 down vote accepted
  1. As Gonzalo Medina said in his comment, the y axis in this case starts at 1.2, so a line from 0.2,0.2 to 0.6,0.8 is correctly positioned below the axis. The axis limits can only be automatically determined from \addplot commands, not from TikZ commands like \draw.
  2. The way you used the \draw command is the correct approach to draw lines in an axis, but you have to adjust the axis limits manually if necessary (as it is in your example) by using xmin, xmax, etc.
  3. If no \addplot commands are found in an axis, PGFplots falls back to standard axis limits and just draws the axis, but doesn't execute the \draw command. In this case, you should also adjust all axis limits manually. Alternatively, if you're happy with the standard axis limits (0 to 1 for x and y), you could use the after end axis/.code key to execute the \draw command.
share|improve this answer
    
Yep, specifying xmin=0,xmax=1,ymin=0,ymax=1 does indeed result in the red line being drawn. Thanks... –  Peter Grill Jan 9 '12 at 1:15

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.