Idea: `each nth point={<n>}` with non-zero shift
muzimuzhi opened this issue · comments
The downsampling option each nth point={<n>}
always keeps points 0, n, 2n, ..., but users may want to keep points k, n-k, 2n-k, ... (0 <= k < n). Hence a more general option is required and will be helpful.
In https://tex.stackexchange.com/q/649944 I gave an attempt that provides each nth point starting from={<n>}{<k>}
which makes each nth point={<n>}
equivalent to each nth point starting from={<n>}{0}
.
Another approach, letting skip coords between index={<begin>}{<end>}
alter \coordindex
, seems dangerous and will cause backwards compatibility problems.
Example copied from my tex-sx answer. Sorry it's a bit lengthy.
\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}