在 PGFPlots 中使用“每个第 n 个点”进行索引偏移

在 PGFPlots 中使用“每个第 n 个点”进行索引偏移

选项each nth point = \n允许PGFPlots对列表中的数据点进行采样,仅保留索引为 2n+1 的点(其中 n=0,1,...)。我希望能够提取索引为 2n+m 的点(类似于选项mark phase = mmark repeat = n)。

以下是我的失败尝试,不知何故,命令skip coords between index={0}{\m}似乎被忽略了:

\begin{filecontents}{pts.dat}
        x    y
        1    0
        0.5 0.866
        -0.5    0.866
        -1  0
        -0.5    -0.866
        0.5 -0.866
        1   0
    \end{filecontents}
    
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[no marks]

\addplot[ultra thick, dashed, lightgray] table {pts.dat};

\foreach \n in {0,3}
    \foreach \m in {1,\n}
        {
          \addplot[
                skip coords between index={0}{\m},
                each nth point=\n,
                filter discard warning=false,
                unbounded coords=discard
                ] table {pts.dat};
        }
\end{axis}
\end{tikzpicture}
\end{document}

答案1

我不太确定您希望完成什么情节,但目前是的,skip coords between index={<begin>}{<end>}并且each nth point={<n>}可以独立工作。

下面的例子提供了一个新的、更通用的选项each nth point starting from={<n>}{<start>}each nth point={<n>}现在相当于each nth point starting from={<n>}{0}

有关\edef内部使用的技巧\foreach,请参阅这个答案

\begin{filecontents}{pts.dat}
 x    y
 1    0
 0.5  0.866
-0.5  0.866
-1    0
-0.5 -0.866
 0.5 -0.866
 1    0
\end{filecontents}
    
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}

\makeatletter
\pgfplotsset{
  /pgfplots/each nth point starting from/.style 2 args={
    /pgfplots/each nth point starting from*={x}{#1}{#2}
  },
    % #1: the filter name: x, y, z, or something exotic like hist/data
    % #2: the N of 'each nth'
  % #3: the starting coordindex
  % Example:
  %  {x}{3}{0} will leave indices 0, 3, 6, ...
  %  {x}{3}{1} will leave indices 1, 4, 7, ...
    /pgfplots/each nth point starting from*/.style n args={3}{
        /pgfplots/#1 filter/.append code={%
            \ifnum\coordindex=0
        \begingroup
          % do not pollute \pgfmathresult
          \pgfkeys{/pgf/fpu=false}%
          \pgfmathsetmacro\c@pgfplots@eachnthpoint@xfilter{int(Mod(-(#3),#2))}%
          \pgfmath@smuggleone\c@pgfplots@eachnthpoint@xfilter
        \endgroup
                \edef\c@pgfplots@eachnthpoint@xfilter@cmp{#2}%
            \else
                \pgfplotsutil@advancestringcounter\c@pgfplots@eachnthpoint@xfilter
            \fi
            \ifx\c@pgfplots@eachnthpoint@xfilter@cmp\c@pgfplots@eachnthpoint@xfilter
                \def\c@pgfplots@eachnthpoint@xfilter{0}%
            \else
                \let\pgfmathresult\pgfutil@empty
            \fi
        }
    },
}
\makeatother

\begin{document}
\begin{tikzpicture}
  \begin{axis}[no marks]
    \addplot[ultra thick, dashed, lightgray] table {pts.dat};
  
    \foreach \m in {1,...,3} {
      \edef\x{
        \noexpand\addplot[
          each nth point starting from={3}{\m},
          filter discard warning=false,
          unbounded coords=discard
        ] table {pts.dat};
      }
      \x
    }
    
    \foreach \m in {1,2} {
      \edef\x{
        \noexpand\addplot[blue,
          each nth point starting from={2}{\m},
          filter discard warning=false,
          unbounded coords=discard
        ] table {pts.dat} -- cycle;
      }
      \x
    }
  \end{axis}
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容