0

Context: I want to plot some data from a CSV file with pgfplots. This file contains more information than needed, so I want to filter it. Filters (with discard if not from here or there) work well on one column, but I get some troubles when I want to filter on several columns in the same time.

Problem: In my case, I want to take the intersection of 3 'discard if not' conditions. But it seems that only the last conditions is taken into account.

Here is a following MWE:

\documentclass{standalone}

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

\usepackage{filecontents} \begin{filecontents}{data.csv} # x, y, color, shape, size 1, 3, a, 0, 2 2, 6, b, 0, 2 3, 5, a, 1, 2 4, 4, b, 1, 3 5, 9, a, 0, 3 6, 1, b, 1, 3 \end{filecontents}

\begin{document}

\pgfplotsset{ discard if not A/.style 2 args={ x filter/.code={ \edef\tempa{\thisrow{#1}} \edef\tempaa{#2} \ifx\tempa\tempaa \else \def\pgfmathresult{} \fi } }, discard if not B/.style 2 args={ x filter/.code={ \edef\tempb{\thisrow{#1}} \edef\tempbb{#2} \ifx\tempb\tempbb \else \def\pgfmathresult{} \fi } }, }

\begin{tikzpicture} \begin{axis}[grid]
\addplot+[mark=+, only marks,] table[x expr=\thisrow{x}1, y expr=\thisrow{y}1, discard if not A={color}{a}, discard if not A={shape}{0}, discard if not B={size}{2}, ignore chars={#}, col sep=comma, ] {data.csv}; \end{axis} \end{tikzpicture} \end{document}

In the previous example, instead of plotting only 1 point, I get the 3 points corresponding to size=2.

Even though my question seems similar to this question, it is not the same since it differs from the combination of one 'discard if' and one 'discard if not' vs three 'discard if not'. But of course, I tried to use this previous question to solve my issue without any success.

Question: How to solve this issue, and obtain the intersection of this three discard if not condition with pgfplots?

R. N
  • 1,076

1 Answers1

2

Instead of x filter/.code you want x filter/.append code. Then you only need one version.

\documentclass{standalone}

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

\usepackage{filecontents} \begin{filecontents}[overwrite]{data.csv} # x, y, color, shape, size 1, 3, a, 0, 2 2, 6, b, 0, 2 3, 5, a, 1, 2 4, 4, b, 1, 3 5, 9, a, 0, 3 6, 1, b, 1, 3 \end{filecontents}

\begin{document}

\pgfplotsset{ discard if not A/.style 2 args={ x filter/.append code={ \edef\tempa{\thisrow{#1}} \edef\tempaa{#2} \ifx\tempa\tempaa \else \def\pgfmathresult{} \fi } }}

\begin{tikzpicture} \begin{axis}[grid]
\addplot+[mark=+, only marks,] table[x expr=\thisrow{x}1, y expr=\thisrow{y}1, discard if not A={color}{a}, discard if not A={shape}{0}, discard if not A={size}{2}, ignore chars={#}, col sep=comma, ] {data.csv}; \end{axis} \end{tikzpicture} \end{document}

enter image description here