# Tikz,PGFplots: drawing in node's cs placed on plot

I'm trying to place some arrows which should be placed alongside the graphs. I place nodes on the graph using

node[pos=0.2,above] (point1) {}

Then I want to draw in that coordinate system

\draw [->] (node cs:name=point1,anchor=north) -- (0,1);

but this doesn't work at all.

Here's the complete code:

\begin{tikzpicture}
\tikzexternaldisable
\begin{axis}[xlabel=$\lambda$,ylabel=$M_R$
domain=0:50,
width=12cm,
height=8cm,
ytick=\empty
]
node[pos=0.2,circle,fill=black,scale=0.3] (point1) {}
node[pos=0.6,circle,fill=black,scale=0.3] (point1) {};
\addplot[smooth,black] table[x=lam,y expr=\thisrow{ct}*4] {bilder/mr/ct.out} node[pos=1, above] {$W$} node[pos=1,xshift=12pt,yshift=-10pt] (v1) {} ;
\draw (node cs:name=a1,anchor=north) [->]  (0,0) -- (0,1);

\end{axis}
\end{tikzpicture}

The ct.out looks like this:

lam ct  cp
0.4 0.012360489 0.004944196
0.8 0.020495909 0.016396727
1   0.027456016 0.027456016
1.2 0.039743915 0.047692698
1.5 0.062400877 0.093601316
1.7 0.08363738  0.142183546
1.9 0.10823897  0.205654043
2   0.11533637  0.23067274
2.1 0.11622777  0.244078317
2.3 0.11317585  0.260304455
2.5 0.10662368  0.2665592
3   0.087667927 0.263003781
3.5 0.069310704 0.242587464
4   0.0525382   0.2101528
5   0.0262106   0.131053
6   0.003988099 0.023928596

Arrows should look like this

-

This is not a perfect solution but you can start with something like:

\documentclass{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
%\tikzexternaldisable
\begin{axis}[xlabel=$\lambda$,ylabel=$M_R$
domain=0:50,
width=12cm,
height=8cm,
ytick=\empty
]
coordinate[pos=0.1] (point00)
node[pos=0.2,circle,fill=black,scale=0.3] (point1) {}
coordinate[pos=0.2] (point01)
coordinate[pos=0.25] (point02)
node[pos=0.6,circle,fill=black,scale=0.3,outer sep=1mm] (point2) {};
\addplot[smooth,black] table[x=lam,y expr=\thisrow{ct}*4] {ct.out} node[pos=1, above] {$W$} node[pos=1,xshift=12pt,yshift=-10pt] (v1) {} ;
\draw[<-,blue,thick,shorten >=2mm] ([yshift=2mm]point00) -- ([yshift=2mm]point01);
\draw[->,blue,thick] ([yshift=2mm]point01) -- ([yshift=2mm]point02);
\draw[->,red,thick] (point2.north east) --++(-25:1cm);
\draw[->,red,thick] (point2.north) --++(150:2cm);
\end{axis}
\end{tikzpicture}
\end{document}

You can draw arrows between two points (blue ones) or using relative coordinates (red ones). Syntax is ++(increment x, increment y) or ++(angle:length). Setting an outer sep for point1 and point2 (as Altermundus suggested) avoid need for yshift.

The result is

-
 Hello, I think your solution is quite good. Especially the shorten option brought me to the idea, that for a perfect result you would need two coordinates positioned on the plot and then draw an arrow on the path between them and additionaly define the length. Is it possible to define the absolute length of an arrow on a path? Or do I have to open a new topic? – severin Mar 6 '12 at 17:15 @severin: If you prefer to fix arrows length you can use sintax ++(angle:length). Look to the edited answer. – Ignasi Mar 7 '12 at 9:33 Ok, seems like either you specify the absolute length and estimate the angle pointing on the path OR you use two points on the path and have to play around with the length. – severin Mar 9 '12 at 12:31

1) add ... outer sep=6pt] (point1) {}

2) \draw[blue,thick,->] ([yshift=1pt]point1.90 ) -- ++(10,20); manually I place the arrow ([yshift=1pt]point1.90 ) you can use .90 or .north [yshift=1pt]is used to align the arrows.

\documentclass{article}
\usepackage{pgfplots}
\begin{document}

\begin{tikzpicture}
\begin{axis}[xlabel=$\lambda$,ylabel=$M_R$
domain=0:50,
width=12cm,
height=8cm,
ytick=\empty
]
node[pos=0.2,circle,fill=black,scale=0.3,outer sep=6pt] (point1) {}
node[pos=0.6,circle,fill=black,scale=0.3] (point2) {};
\addplot[smooth,black] table[x=lam,y expr=\thisrow{ct}*4] {ct.out} node[pos=1, above] (a1){$W$} node[pos=1,xshift=12pt,yshift=-10pt] (v1) {} ;
\draw[blue,thick,->] ([yshift=1pt]point1.90 )  -- ++(10,20);
\draw[blue,thick,->] (point1.180)  -- ++(-10,-20);
\end{axis}
\end{tikzpicture}
\end{document}
-

Here is a start at an alternate approach that has much room for improvement, although I think it would work well for cases where the data is more dense.

Here I define a macro \DrawArrows which uses restrict x to domain in an attempt to follow the curve and draw the arrow along the curve.

## Notes:

• One downside of this approach is that the data must be available for the x-coordinate of where the point is.
• I believe this case would work well if there were more data available.
• Initially I had only two parameters for the coordinates: x, delta x, but with very little data to work with this was not producing very good results. So, it was expanded to explicitly provide three points: x-left, x, x-right so that they could be tweaked separately.

## Further Enhancements:

• Perhaps a better solution might be possible using the tikz decorations library.
• This question here about how to raise a generic curve along another path that might be useful in adapting this solution to properly handle the case of the red arrows in the image above.

## Code:

\documentclass{article}
\usepackage{pgfplots}
\usepackage{filecontents}

\begin{filecontents*}{ct.out}
lam ct  cp
0.4 0.012360489 0.004944196
0.8 0.020495909 0.016396727
1   0.027456016 0.027456016
1.2 0.039743915 0.047692698
1.5 0.062400877 0.093601316
1.7 0.08363738  0.142183546
1.9 0.10823897  0.205654043
2   0.11533637  0.23067274
2.1 0.11622777  0.244078317
2.3 0.11317585  0.260304455
2.5 0.10662368  0.2665592
3   0.087667927 0.263003781
3.5 0.069310704 0.242587464
4   0.0525382   0.2101528
5   0.0262106   0.131053
6   0.003988099 0.023928596
\end{filecontents*}

\newcommand*{\DrawArrows}[6]{%
% #1 = addplot options (controls arrow and line)
% #2 = table options
% #3 = data file
% #4 = x coordinate of left point
% #5 = x coordinate of point
% #6 = x coordinate of right point
table[restrict x to domain=#5:#5, #2] {#3}
node[circle,fill=black,scale=0.3,outer sep=6pt] {};

table[restrict x to domain=#5:#6, #2] {#3};

table[restrict x to domain=#4:#5, #2] {#3};
}%

\begin{document}

\begin{tikzpicture}
\begin{axis}[xlabel=$\lambda$,ylabel=$M_R$
domain=0:50,
width=12cm,
height=8cm,
ytick=\empty
]
% node[pos=0.2,circle,fill=none,scale=0.3,outer sep=6pt] (point1) {}
% node[pos=0.6,circle,fill=black,scale=0.3] (point2) {}
;
\addplot[smooth,black] table[x=lam,y expr=\thisrow{ct}*4] {ct.out} node[pos=1, above] (a1){$W$} node[pos=1,xshift=12pt,yshift=-10pt] (v1) {} ;

%---------------
\DrawArrows
{brown, ultra thick, xshift=-1.5pt, yshift=1.5pt}% addplot options
{x=lam,y expr=\thisrow{ct}*2}%   table options
{ct.out}%                        data file
{1.2}{1.5}{1.8}% x coordinates: left point, middle, right point

\DrawArrows
{blue, ultra thick, xshift=1.0pt, yshift=2.0pt}% addplot options
{x=lam,y expr=\thisrow{ct}*2}%   table options
{ct.out}%                        data file
{3.0}{3.5}{4.0}% x coordinates: left point, middle, right point

\DrawArrows
{red, ultra thick, xshift=-2pt, yshift=1pt}% addplot options
{x=lam,y expr=\thisrow{ct}*4}%   table options
{ct.out}%                        data file
{1.7}{1.9}{3.0}% x coordinates: left point, middle, right point

\end{axis}
\end{tikzpicture}
\end{document}
-