TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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]

\addplot[domain=0:1, no markers] {(x^2)*(3-2*x)};

\addplot[] coordinates
           {(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:(

share|improve this question
up vote 9 down vote accepted

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);
             }]
\addplot[domain=0:1, no markers] {(x^2)*(3-2*x)};
\end{axis}
\end{tikzpicture}
\end{document}
share|improve this answer
    
(+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 "<-".

share|improve this answer
1  
It is astounding that this is not the accepted answer. – Mike Pierce Oct 23 '15 at 2:05

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}
share|improve this answer
    
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

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.