1

I would like to place defined markers on certain datapoints in a plot using a predefined list of tuples \def\datalist{datapoint/mark}. I found a suitable solution of the same problem with labels which relies upon the nodes near coords. Unfortunately, it works only if the datalist is explicitly contained in the addplot+[nodes near some coords={datapoint/mark}]. The code does not compile if the macro \datalist is used directly in addplot+[nodes near some coords=\datalist]. I guess a solution is related to Using Macro Defined Lists in TikZ/PGFplots but none of the workarounds worked for me.

A MWE:

\documentclass[tikz,border=10pt]{standalone}
\usepackage{pgfplots}
\usetikzlibrary{shapes}

\def\datalist{0/rectangle, 2/circle, 4/diamond}

\pgfplotsset{ compat=1.16, %%% node near coord/.style args={#1/#2}{% Style for activating the label for a single coordinate nodes near coords*={ \ifnum\coordindex=#1 \fi }, scatter/@pre marker code/.append code={ \ifnum\coordindex=#1 \pgfplotsset{every node near coord/.append style=fill, draw,#2}\fi } }, nodes near some coords/.style={ % Style for activating the label for a list of coordinates scatter/@pre marker code/.code={},% Reset the default scatter style, so we don't get coloured markers scatter/@post marker code/.code={},% node near coord/.list={#1}% Run "node near coord" once for every element in the list } } \begin{filecontents}{datafile.csv} 1.00 1.00 2.00 2.00 3.00 3.00 4.00 4.00 5.00 5.00 \end{filecontents}

\begin{document} \begin{tikzpicture} \begin{axis}[] \addplot +[ %nodes near some coords=\datalist, % doesn't compile, returns 'File ended while scanning use of \pgfkeys@code. \par mwe.tex' nodes near some coords={0/rectangle, % works 2/circle, 4/diamond}, ] table []{datafile.csv}; \end{axis} \end{tikzpicture} \end{document}

fafroo
  • 35

1 Answers1

3

You need the /.expanded key handler, i.e. nodes near some coords/.expanded=\datalist.

\documentclass[tikz,border=10pt]{standalone}
\usepackage{pgfplots}
%\pgfplotsset{compat=1.17} %<-consider adding
\usetikzlibrary{shapes}

\def\datalist{0/rectangle, 2/circle, 4/diamond}

\pgfplotsset{ compat=1.16, %%% node near coord/.style args={#1/#2}{% Style for activating the label for a single coordinate nodes near coords*={ \ifnum\coordindex=#1 \fi }, scatter/@pre marker code/.append code={ \ifnum\coordindex=#1 \pgfplotsset{every node near coord/.append style=fill, draw,#2}\fi } }, nodes near some coords/.style={ % Style for activating the label for a list of coordinates scatter/@pre marker code/.code={},% Reset the default scatter style, so we don't get coloured markers scatter/@post marker code/.code={},% node near coord/.list={#1}% Run "node near coord" once for every element in the list } } \begin{filecontents}[overwrite]{datafile.csv} 1.00 1.00 2.00 2.00 3.00 3.00 4.00 4.00 5.00 5.00 \end{filecontents}

\begin{document} \begin{tikzpicture} \begin{axis}[] \addplot +[ nodes near some coords/.expanded=\datalist, % works ] table []{datafile.csv}; \end{axis} \end{tikzpicture} \end{document}

  • 1
    The second thing solved the issue and it runs like a charm, thank you :)! The first thing is a typo that slipped in during cut-pasting. I suggest editing the =-part in the question and the answer. Would you agree @lazy-squirrel? – fafroo Dec 09 '20 at 08:39
  • @fafroo Yes, of course. –  Dec 09 '20 at 15:30