1

The each nth point = \n option of PGFPlots allows to sample data points in a list, keeping only the points indexed 2n+1 (with n=0,1,...). I would like to be able to extract the points with indices 2n+m (similarily to the option mark phase = m and mark repeat = n).

Here is my unsuccessful attempt, where somehow, the command skip coords between index={0}{\m} seems to be ignored:

\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}

Mammouth
  • 323
  • What do you wish to accomplish. Is this your real data? Why so many plots on top of each other? You can not use \foreach inside axis without careful consideration. - use \pgfplotsinvokeforeach, but do not nest it. – hpekristiansen Jul 05 '22 at 16:36

1 Answers1

2

I'm not quite sure what plots you wish to accomplish, but yes currently skip coords between index={<begin>}{<end>} and each nth point={<n>} work independently.

The example below provides a new and more general option each nth point starting from={<n>}{<start>}. each nth point={<n>} is now equivalent to each nth point starting from={<n>}{0}.

For the \edef trick used inside \foreach, see this answer.

\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}

enter image description here

muzimuzhi Z
  • 26,474