How to Combine `skip coords` and `each nth point` in PGFPlots
At a glance
- 1. Choose the skipped range
Use skip coords between index to omit a contiguous block of coordinate indices.
- 2. Set the sampling interval
Use each nth point to retain regularly spaced coordinates from a table plot.
- 3. Apply both filters
Put both options on the same plot, then compile and check the retained markers.
- 4. Check the indexing
Coordinate indices begin at zero; verify the result if you depend on which points remain.
To omit a block of coordinates and also reduce the number of points PGFPlots draws, combine skip coords between index with each nth point. The first option excludes coordinates in an index range; the second keeps only regularly spaced points from a table plot.
Combine the options
Put each nth point among the plot options and skip coords between index among the options to table. For example, the plot below omits indices 4 through 7 and requests every third point:
\documentclass[tikz,border=3pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}
\begin{tikzpicture}
\begin{axis}
\addplot+[
only marks,
each nth point={3}
] table[
x index=0,
y index=1,
skip coords between index={4}{7}
] {
0 0
1 1
2 4
3 9
4 16
5 25
6 36
7 49
8 64
9 81
10 100
11 121
};
\end{axis}
\end{tikzpicture}
\end{document}
This is a complete example using an inline, two-column table. x index=0 and y index=1 tell PGFPlots to read the first column as x and the second as y. only marks makes the retained coordinates easier to inspect; remove it or change the plot style if you want connecting lines.
How the indices work
The coordinate index is the position in the input table, starting at zero. In the example, the first row (0 0) has index 0, the next row has index 1, and so on. Thus skip coords between index={4}{7} specifies the inclusive index range 4–7: those table coordinates are omitted.
each nth point={3} asks PGFPlots to retain every third point for the table plot. It is useful when a dense data set would produce too many markers or take longer to draw. It does not change the input file; it affects what is plotted.
These options do different jobs, so both can be given on the same \addplot. If the precise combination of a skipped range and periodic sampling matters, compile a small test and inspect the resulting markers: the set of retained points depends on applying both selection rules to the coordinate stream. For a workflow that must sample after removing rows, preprocess the data into a second table and apply each nth point to that table. This makes the intended order explicit.
Common mistakes
- Using the wrong option name: the range option is
skip coords between index, not simplyskip coords. - Forgetting the zero-based index: index 0 refers to the first coordinate, not the second. A range
{4}{7}excludes the fifth through eighth rows of an ordinary table with no header. - Putting options in the wrong place: use
skip coords between indexin the table options aftertable[. Seteach nth pointas a plot option after\addplot+[. - Expecting the data to be rewritten: these options affect plotting, not the source table. If you need a saved or exported reduced data set, filter it separately.
- Expecting a smooth curve from sparse points: dropping coordinates can make a line look less detailed. For large data sets, consider plotting a reduced line without markers, or use separate plots for the line and selected markers.
If you only need one operation, use just its option: skip coords between index={a}{b} for a contiguous omission, or each nth point={n} for regular thinning.
References
Spotted an error? Send a correction. Read how we write and check articles in our editorial policy.