在 pgfplotset 样式中使用预定义列表

在 pgfplotset 样式中使用预定义列表

我想使用预定义的元组列表将定义的标记放置在图中的某些数据点上\def\datalist{datapoint/mark}。我找到了一个合适的解决方案标签也有同样的问题它依赖于nodes near coords。不幸的是,只有当datalist明确包含在 中时,它才有效。如果在 中直接使用addplot+[nodes near some coords={datapoint/mark}]宏,则代码无法编译。我猜解决方案与\datalistaddplot+[nodes near some coords=\datalist]在 TikZ/PGFplots 中使用宏定义列表但这些解决方法对我都不起作用。

一位 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}

答案1

您需要/.expanded密钥处理程序,即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}

相关内容