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.

  • 1
    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
    Commented Sep 3, 2014 at 21:30
  • Related: Remove rows in a \addplot table command (for plotting only the last n rows of a table).
    – Jake
    Commented Sep 3, 2014 at 22:52
  • @Fritz Thanks for the comment and thanks for the answer !
    – Nicolas
    Commented Sep 4, 2014 at 19:52

1 Answer 1


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};
  • I am using this on a table with 250+ rows and it seems somewhat inefficient. Main thing is, it works! Is there a more efficient way?
    – SpmP
    Commented Feb 8, 2015 at 9:30
  • Is it much slower than plotting the whole table without filtering? I'd guess it should be about the same speed because PGFplots still needs to parse all coordinates. Maybe it could be made faster by first filtering the the values into a smaller temporary table and then plot that with PGFplots. However, you should probably post a separate question about that, because here it will be difficult to find.
    – Fritz
    Commented Feb 9, 2015 at 8:21
  • It did seem very slow as the throwing out coordinates blah was not quick in succession. The table is very big tho. Splitting into smaller sub-tables may be the way to go. Will ask a question on it.
    – SpmP
    Commented Feb 12, 2015 at 0:05
  • Thank you! This worked for me and solved my problem. I have got 100 entries and it is fast. Commented Dec 19, 2020 at 15:39
  • I think using domain restrictions like restrict x to domain=value1:value2 (could be x, y or z) should work in most cases and would be efficient.
    – Bibi
    Commented Dec 7, 2021 at 16:51

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .