Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I would like to use pgfplots command nodes near coords to get a list of named node ((no1), (no2), (no3) etc.) that I can use afterwards to draw some paths that I fail to construct with pgfplots only.

I suppose it would need to use scatter/@post marker code but I did not understand very well that part of the manual (p. 82). Actually, if nodes near coords is a redefinition of scatter/@pre marker code so I suppose I don't really need nodes near coords and could use a altered scatter. I did not find however the code defining nodes near cords in the manual and got lost into the huge code files of pgfplots.

Here is an example taken from the pgfplots manual that could constitute a minimum example:

\begin{tikzpicture}
\begin{axis}[nodes near coords]
\addplot+[only marks] coordinates {
(0.5,0.2) (0.2,0.1) (0.7,0.6)
(0.35,0.4) (0.65,0.1)};
\end{axis}
\end{tikzpicture}

Unfortunately I just do not know if that is really part of the answer.

share|improve this question
add comment

1 Answer

up vote 11 down vote accepted

There's no inbuilt functionality for this, but you can add it yourself relatively easily, for example using the following code:

\newcounter{coordnode}
\pgfplotsset{
    /tikz/stepcoordnodecounter/.code={\stepcounter{coordnode}},
    name nodes near coords/.style={
        every node near coord/.append style={
            stepcoordnodecounter,
            name=#1-\thecoordnode,
        },
        execute at end plot=\setcounter{coordnode}{0},
        execute at end plot visualization=\global\expandafter\edef\csname#1total\endcsname{\thecoordnode}\setcounter{coordnode}{0}
    },
    name nodes near coords/.default=coordnode
}

If you then add name nodes near coords to your \addplot options, the nodes will be named from <node name>-1 to <node name>-<max>, with <node name> either specified using the optional argument or defaulting to coordnode. The total number of nodes is stored in the macro \<node name>total.

Here's an example of how this could be used:

\documentclass{article}

\usepackage{pgfplots}
\usepackage{pgfplotstable}

\newcounter{coordnode}
\pgfplotsset{
    /tikz/stepcoordnodecounter/.code={\stepcounter{coordnode}},
    name nodes near coords/.style={
        every node near coord/.append style={
            stepcoordnodecounter,
            name=#1-\thecoordnode,
        },
        execute at end plot=\setcounter{coordnode}{0},
        execute at end plot visualization=\global\expandafter\edef\csname#1total\endcsname{\thecoordnode}\setcounter{coordnode}{0}
    },
    name nodes near coords/.default=coordnode
}

\begin{document}

\begin{tikzpicture}

\begin{axis}[
    nodes near coords,
    ]
\addplot+[only marks,   name nodes near coords=myname] coordinates {
(0.5,0.2) (0.2,0.1) (0.7,0.6)
(0.35,0.4) (0.65,0.1)};
\addplot+[only marks,   name nodes near coords=secondname] coordinates {
(0.3,0.3) (0.2,0.2) (0.4,0.6)
(0.7,0.4) (0.6,0.1)};
\end{axis}
\draw (myname-1) -- (myname-\mynametotal);
\draw (secondname-4) to [out=180, in=0] (secondname-3);
\end{tikzpicture}

\end{document}
share|improve this answer
    
very nice answer! –  tecepe Oct 8 '12 at 12:17
add comment

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.