# Use tikz macros in pgfplots?

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?

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

• 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. 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.
• 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