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.

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

share|improve this question
    
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). –  Peter Grill Nov 18 '11 at 17:56
    
Sorry, MWE added. –  Fernando Nadal Nov 18 '11 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. –  Peter Grill Nov 18 '11 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. –  Fernando Nadal Nov 18 '11 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. –  Peter Grill Nov 18 '11 at 21:29

2 Answers 2

up vote 7 down vote accepted

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}
share|improve this answer

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

share|improve this answer
    
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. –  Peter Grill Nov 19 '11 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? –  Christian Feuersänger Nov 19 '11 at 17:05
    
It's true. Now it's fixed. –  Fernando Nadal Nov 19 '11 at 17:28

Your Answer

 
discard

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.