28

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

Considering the following MWE file:

\documentclass[tikz]{standalone}

\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usepackage{amsmath}

\begin{document}

    \begin{tikzpicture}
        \begin{axis}[%
            width=16cm,height=8cm,
            view={-45}{65},
            scale only axis,
            xmin=200,xmax=500,
            xlabel={data1},
            ymin=0,ymax=100,
            ylabel={data2},
            zmin=0,zmax=1,
            zlabel={data3},
        ]
        \addplot3 [
            thick,smooth,
            color= darkgray,
            solid]
            table[row sep=crcr] {
                250 0 0\\
                250 25 0.2\\
                250 50 0.3\\
                250 75 0.2\\
                250 100 0\\
            };

        \addplot3 [
            thick,smooth,
            color= darkgray,
            solid]
            table[row sep=crcr] {
                450 0 0.5\\
                450 25 0.1\\
                450 50 0\\
                450 75 0.1\\
                450 100 0.5\\
            };
        \end{axis}
    \end{tikzpicture}%
\end{document}

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.

3
  • 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
    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
    Sep 3, 2014 at 22:52
  • @Fritz Thanks for the comment and thanks for the answer !
    – Nicolas
    Sep 4, 2014 at 19:52

1 Answer 1

39

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

\documentclass[tikz]{standalone}
\usepackage{pgfplots}

% Style to select only points from #1 to #2 (inclusive)
\pgfplotsset{select coords between index/.style 2 args={
    x filter/.code={
        \ifnum\coordindex<#1\def\pgfmathresult{}\fi
        \ifnum\coordindex>#2\def\pgfmathresult{}\fi
    }
}}

\usepackage{filecontents}
\begin{filecontents}{data.txt}
 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
\end{filecontents}

\begin{document}
    \begin{tikzpicture}
        \begin{axis}
        \addplot [select coords between index={1}{5}] table {data.txt};
        \end{axis}
    \end{tikzpicture}
\end{document}
5
  • 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
    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
    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
    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. 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
    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 .