17

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.

19

There's no inbuilt functionality for this, but you can add it yourself relatively easily by making use of the \coordindex macro that holds the index of the current coordinate:

\pgfplotsset{
    name nodes near coords/.style={
        every node near coord/.append style={
            name=#1-\coordindex,
            alias=#1-last,
        },
    },
    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 last node for each plot will be named <node name>-last.

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

\documentclass{article}

\usepackage{pgfplots}
\usepackage{pgfplotstable}

\pgfplotsset{
    name nodes near coords/.style={
        every node near coord/.append style={
            name=#1-\coordindex,
            alias=#1-last,
        },
    },
    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-0) -- (myname-last);
\draw (secondname-3) to [out=180, in=0] (secondname-2);
\end{tikzpicture}

\end{document}
  • very nice answer! – tcpaiva Oct 8 '12 at 12:17
  • 4
    Note that pgfplots comes with some sort of "step node counter" on its own: you can substitute name=#1-\thecoordnode by name=#1-\coordindex. – Christian Feuersänger Aug 16 '14 at 6:04
  • 1
    @ChristianFeuersänger: Ah yes, of course! Thanks for that! I've edited my answer. – Jake Aug 16 '14 at 17:20
  • Can one modify this to get the exact positions, so just the coordinates, instead of nodes? – gr4nt3d Jan 29 at 16:09
2

Just quickly, don't have time for a full MWE right now; but one can also use the mark=text plot style with text mark as node=true:

\addplot+[
  only marks,
  mark=text, 
  text mark={}, % empty for now; try also \coordindex
  text mark as node=true,
  text mark style={%
    name=MYNODE\coordindex,
    color=orange,
    shape=circle,
    draw,
    inner sep=0pt,
    minimum size=0pt,
    align=center,
    text width=5pt,
    text depth=0pt
  },
]
  table[
    x index=1, %x expr=\coordindex,
    y expr=0.0,
  ] \mytable ;
\end{axis}

\draw[thick,green] (0,0) -- (MYNODE1.center);

This will give you 5pt circles named MYNODE0, MYNODE1 etc that you can use afterwards. Note that if you do use some text for the node, then the size or the drawn node will change (but of course, you may not have to draw anything, in which case there should be no problem).

  • This worked quite nicely, as long as you don't forget to actually set mark=text ;). Thank you! (It also gives exact positions - which is nice) – gr4nt3d Jan 29 at 16:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.