Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

When using pgfplots, is there a way to plot only the first n rows of a given table ?

Considering the following MWE file:




            scale only axis,
        \addplot3 [
            color= darkgray,
            table[row sep=crcr] {
                250 0 0\\
                250 25 0.2\\
                250 50 0.3\\
                250 75 0.2\\
                250 100 0\\

        \addplot3 [
            color= darkgray,
            table[row sep=crcr] {
                450 0 0.5\\
                450 25 0.1\\
                450 50 0\\
                450 75 0.1\\
                450 100 0.5\\

How can I plot only the first n=4 rows of each table and ask pgfplots to stop reading the table once this row is reached ?

Of course, for the above example, this seems useless, but when huge data tables are used, this could be extremely usefull, in particular to use the same data and focus on different parts of the table in different figures.

share|improve this question
Just a hint for the next time: try to make your example smaller by reducing it to the essentials that are relevant to the problem you want to solve. For example, all those styles and fancy 3d plots are not really necessary. That makes it easier for people to help you. :-) –  Fritz Sep 3 '14 at 21:30
Related: Remove rows in a \addplot table command (for plotting only the last n rows of a table). –  Jake Sep 3 '14 at 22:52
@Fritz Thanks for the comment and thanks for the answer ! –  Nicolas Sep 4 '14 at 19:52

1 Answer 1

up vote 13 down vote accepted

Here is a style that allows you to select the rows N through M (inclusive, starting at 0) by specifying select coords between index={N}{M}. To select the first N points, you would specify select coords between index={0}{N-1}.

It is based on the explanation of the filter point style in the pgfplots manual. There is also an oppposite style that is already part of pgfplots: skip coords between index={N}{M} discards all points that are inside the range N to M.

The code below results in this cropped parabola:

Cropped parabola


% Style to select only points from #1 to #2 (inclusive)
\pgfplotsset{select coords between index/.style 2 args={
    x filter/.code={

 X    Y
-3    9      %  0 left out
-2    4      %  1 plotted
-1    1      %  2 plotted
-0.5  0.25   %  3 plotted
 0    0      %  4 plotted
 0.5  0.25   %  5 plotted
 1    1      %  6 left out
 2    4      %  7 left out
 3    9      %  8 left out

        \addplot [select coords between index={1}{5}] table {data.txt};
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.