I'm trying to put some arrows on the left and right side of the maximum of a curve. The arrows should point along the curve. My problem is that if I use the [pos=X] notation I don't know where I am in relation to the maximum. And if I use absolute coordinates the node is not sloped.
\documentclass{article}
\usepackage{pgfplots}
\usetikzlibrary{shapes.geometric}
\pgfplotsset{compat=1.7}
\begin{document}
\begin{tikzpicture}
\tikzset{myarrow/.style=
{sloped,isosceles triangle,anchor=apex,fill=black,inner sep=2pt}}
\begin{axis}
\addplot [smooth] {-x^2}
node[pos=0.4,myarrow,rotate=180]{} %ok
node[pos=0.6,myarrow]{}; %ok
\addplot[smooth,green] {-x^2+2*x-4}
node[pos=0.4,myarrow,rotate=180]{}
node[pos=0.6,myarrow]{} %wrong side
node[myarrow,fill=red] at (axis cs:4,-12){}; %wrong rotation!
\end{axis}
\end{tikzpicture}
\end{document}
Edit: Some remark about Jake's answer
Jake's code (naturally) works but
for my case it is a bit too complicated. Jake gets the correct rotation for the arrow by drawing a short path from x-1pt to x (cool idea). He gets the coordinates for this small path with intersections. But as my plot is based on a function I can simply calculate the values (e.g. with
\pgfmathparse
) and then draw the small path after the plot:\path (axis cs:3.99,-11.94012) -- (axis cs:4,-12) node[pos=1,myarrow]{};
The same can also be achieved by redrawing the plot but with a restricted domain:
\addplot[draw=none,domain=3.99:4] {\MyFunction{x}} node[pos=1,myarrow,fill=yellow]{};
I also looked if there is a way to decide where I am relative to the maximum (so that I can change the rotation) and in theory it is possible: If I use the code found here pgfplots: mark max/min value of a function
\pgfplots@metamax
is known after the plot and can be used in calculations.