10

Is there any way to adapt Show last N rows from input datafile only in \pgfplotstabletypeset to a \addplot table command ?

This doesn't work:

\documentclass{article}

\usepackage{pgfplots}
\usepackage{filecontents}

\begin{document}

\begin{filecontents}{data.dat}
1 5
2 3
3 6
4 4
5 0
6 1
\end{filecontents}

\pgfkeys{
    /pgfplots/table/print last/.style={
        row predicate/.code={
            % Calculate where to start printing, use `truncatemacro` to get an integer without .0
            \pgfmathtruncatemacro\firstprintedrownumber{\pgfplotstablerows-#1}
            \ifnum##1<\firstprintedrownumber\relax
                \pgfplotstableuserowfalse
            \fi
        }
    },
    /pgfplots/table/print last/.default=1
}

\begin{tikzpicture}
  \begin{axis}[x=1cm]
    \addplot table {data.dat};
  \end{axis}
\end{tikzpicture}

\begin{tikzpicture}
  \begin{axis}[x=1cm]
    \addplot table [print last=4] {data.dat};
  \end{axis}
\end{tikzpicture}

\end{document}

Both plots are the same:

enter image description here

6
  • It is always best to compose a MWE that illustrates the problem including the \documentclass and the appropriate packages so that those trying to help don't have to recreate it. Also, in this case you should also show a line without the print last=4 to ensure that your graph is properly formatted (except for the print last part). Nov 18, 2011 at 17:56
  • Sorry, MWE added. Nov 18, 2011 at 18:43
  • Thanks. But that does not seem to produce the graphs for me. I get the axis, just not the blue lines. Also, minimal should not be used for a MWE. I'd suggest using the article class. It seems that if I change the file name from data.dat to data.data works so not sure what is going on. Nov 18, 2011 at 18:50
  • Changed to article class. It runs for me but if [print last=4] should work, the second plot ought to draw only the last 4 points of the table. Nov 18, 2011 at 21:14
  • Excellent, now you have a full MWE. I don't know why I needed to change the name earlier. It appears that if I use this MWE in a new directory, things work fine. But in my usual directory where I place these tests things don't work with the data.dat file name, even though I checked the file and it appears to be fine... Anyway, my problem is unrelated to yours, so thanks for putting together a MWE. Nov 18, 2011 at 21:29

2 Answers 2

8

Here's a style plot last=<n> that can be added to the \addplot table [<options>]. It uses the create on use syntax to create a new column which checks whether the current row is less than n rows from the final row, assigns the value of that check to meta, and then uses a filter to only keep the values for which meta is 1.

\begin{tikzpicture}
  \begin{axis}[x=1cm]
    \addplot table {data.dat};
  \end{axis}
\end{tikzpicture}

\begin{tikzpicture}
  \begin{axis}[x=1cm]
      \addplot table [plot last=4] {data.dat};
  \end{axis}
\end{tikzpicture}

will yield

The main advantage over this approach is that you don't need to explicitly call \pgfplotstablegetrowsof before your plot, and you don't need to specify the data file twice. The main drawback is that you can't use the meta entry anymore for plotting error bars or changing the colour in a scatterplot.

Here's the full code:

\documentclass{article}

\usepackage{pgfplots}
\usepackage{pgfplotstable}
\usepackage{filecontents}

\begin{document}

\begin{filecontents}{data.dat}
a b
1 5
2.5 3
3 6
4 4
5 0
6 1
\end{filecontents}

\pgfkeys{
    /pgfplots/table/print last/.style={
        row predicate/.code={
            % Calculate where to start printing, use `truncatemacro` to get an integer without .0
            \pgfmathtruncatemacro\firstprintedrownumber{\pgfplotstablerows-#1}
            \ifnum##1<\firstprintedrownumber\relax
                \pgfplotstableuserowfalse
            \fi
        }
    },
    /pgfplots/table/print last/.default=1,
    /pgfplots/table/plot last/.style={
        create on use/rows/.style={
            create col/expr=\pgfplotstablerow>(\pgfplotstablerows-#1-1)
        },
        meta=rows,
        /pgfplots/x filter/.code=\pgfmathparse{x/meta}
    }
}

\begin{tikzpicture}
  \begin{axis}[x=1cm]
    \addplot table {data.dat};
  \end{axis}
\end{tikzpicture}

\begin{tikzpicture}
  \begin{axis}[x=1cm]
      \addplot table [plot last=4] {data.dat};
  \end{axis}
\end{tikzpicture}


\end{document}
10

Thanks Christian, you pointed me to a solution that works. This is the edited and corrected code:

\documentclass{article}

\usepackage{pgfplots}
\usepackage{filecontents}

\begin{document}

\begin{filecontents}{data.dat}
1 5
2 3
3 6
4 4
5 0
6 1
\end{filecontents}

\begin{tikzpicture}
  \begin{axis}[x=1cm,y=1cm]
    \addplot table {data.dat};
  \end{axis}
\end{tikzpicture}

\begin{tikzpicture}
    \pgfplotstablegetrowsof{data.dat}
    \pgfmathsetmacro\yfin{\pgfmathresult - 4}
    \pgfmathsetmacro\yini{0}

    \begin{axis}[x=1cm,y=1cm,skip coords between index={\yini}{\yfin}]
        \addplot table {data.dat};
    \end{axis}
\end{tikzpicture}

\end{document}

enter image description here

3
  • Isn't this producing the <n> coordinates? I thought you wanted the last <n> data values plotted? Also, it would be better to post a complete example with packages and \documentclass. Nov 19, 2011 at 15:57
  • Fernando, I'm glad my comment helped out - you approach was precisely what I had in mind. But Peter is right: it seems as if you have (accidentally?) included rows 0 : (N-2) ... did you mean skip ... = {0}{\yini} or something like that? Nov 19, 2011 at 17:05
  • It's true. Now it's fixed. Nov 19, 2011 at 17:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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