# Line with arrowhead in PGFPLOT

How can I add a line ending with an arrowhead in pgfplot?

\begin{tikzpicture}
\begin{axis}[grid=major,axis x line=middle,
axis y line=middle]

{(0,0) (0.5,0)};

\end{axis}
\end{tikzpicture}


I want the above line to end with an arrow but I can't figure out what to put in the brackets. Probably that's a dumb question but neither google nor the manual helped me:(

-

Try the after end axis key. It allows you to add arbitrary TikZ code and still access the plot's coordinate system. It's documented in section 4.23 ("Miscellaneous Options") of the pgfplots 1.4 manual.

\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[grid=major,axis x line=middle,
axis y line=middle,
after end axis/.code={
\draw[red,->] (axis cs:0,0) -- (axis cs:0.5,0);
}]
\end{axis}
\end{tikzpicture}
\end{document}

-
(+1) Very precise and elegant solution. –  chl Dec 31 '10 at 9:46

Or, in your code, put "->" in the square brackets like this:

\addplot[->] coordinates
{(0,0) (0.5,0)};


If you want the arrow at the other end, use "<-".

-

Would that help?

\documentclass[10pt]{article}
\usepackage{pgf,tikz}
\usetikzlibrary{arrows}
\pagestyle{empty}
\begin{document}
\begin{tikzpicture}
\draw[->,color=black] (-1,0) -- (1,0);
\end{tikzpicture}
\end{document}

-
Ops, I'm sorry, the code above could be quite misleading...that is just a snippet, if I a add the \draw command within the tikz picture the arrow is created in the lower left side of the picture and coordinates are not referred to the axis but to the picture area. I could adjust those coordinates to match the center of cartesian coordinates system but it's not a smart solution! –  Pie86 Dec 31 '10 at 0:56