4

This seems very related to How to display every 4th row in a large data file using pgfplotstable?. I just try to plot the filtered rows but am missing something here. Here is the MWE (I copied the example from previous link).

\pgfplotstableread{
 obs      number
  1         2
  2         5
  3         3
  4         2
  5         4
  6         1
  7         2
  8         5
  9         3
  10        2
  11        4
  12        1
}\loadedtable

\pgfplotstabletypeset[
  row predicate/.code={%
    \pgfplotstablegetelem{#1}{obs}\of{\loadedtable}
    \pgfmathparse{int(Mod(\pgfplotsretval,4)}
    \ifnum \pgfmathresult = 0 
      \else \pgfplotstableuserowfalse
    \fi}
]{\loadedtable}

\begin{tikzpicture}
    \begin{axis}
   \addplot[only marks, 
    x filter/.code={\pgfplotstablegetelem{\coordindex}{obs}\of{\loadedtable}
                    \pgfmathparse{int(Mod(\pgfplotsretval, 4))}
                    \ifnum \pgfmathresult = 0 
                    \else
                    \def \pgfmathresult{}
                    \fi},
    ] table[x=obs,y=number] {\loadedtable};
   \end{axis}
\end{tikzpicture}

The tabletypeset works fine but the plot does not work. Is there an easy fix?

On a side note, is there anyway to save the filtered table as a new macro so that I can use it in a plot directly?

Any help is appreciated.

3

For plotting only each n-th coordinate, PGFPlots already provides the mechanism mark repeat=<integer> and mark offset=<integer>:

\documentclass[border=5mm]{standalone}

\usepackage{pgfplots, pgfplotstable}
\begin{document}


\pgfplotstableread{
 obs      number
  1         2
  2         5
  3         3
  4         2
  5         4
  6         1
  7         2
  8         5
  9         3
  10        2
  11        4
  12        1
}\loadedtable

\pgfplotstabletypeset[
  row predicate/.code={%
    \pgfplotstablegetelem{#1}{obs}\of{\loadedtable}%
    \pgfmathparse{int(Mod(\pgfplotsretval,4)}%
    \ifnum \pgfmathresult = 0 %
      \else \pgfplotstableuserowfalse%
    \fi}
]{\loadedtable}

\begin{tikzpicture}
    \begin{axis}
   \addplot[only marks,
   mark repeat=4, mark phase=4
    ] table[x=obs,y=number] {\loadedtable};
   \end{axis}
\end{tikzpicture}

\end{document}
  • Thanks! This is really neat. Any suggestions to my side note? Can we save the filtered table as a new macro in pgfplotstable? – Zhen Sun Aug 5 '14 at 18:37
  • @ZhenSun: No, sorry. Probably best to open a new question for this. – Jake Aug 5 '14 at 20:00

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.