4

I started to use this fantastic package pgfplots, and I would need a little help. In this example taken from the manual (page 106)

\begin{tikzpicture} \begin{axis}[scatter/classes={
a={mark=square*,blue},%
b={mark=triangle*,red},%
c={mark=o,draw=black}}]
\addplot[scatter,only marks,
    scatter src=explicit symbolic]
    coordinates {
        (0.1,0.15)  [a]
        (0.45,0.27) [c]
        (0.02,0.17) [a]
        (0.06,0.1)  [a]
        (0.9,0.5)   [b]
        (0.5,0.3)   [c]
        (0.85,0.52) [b]
        (0.12,0.05) [a]
        (0.73,0.45) [b]
        (0.53,0.25) [c]
        (0.76,0.5)  [b]
        (0.55,0.32) [c]};
\end{axis} 
\end{tikzpicture}

three scatter classes are created, a, b, and c, each consisting of a few points. Each has separate mark formatting options and legend entry, which is a great feature. But I haven't found any way to turn certain scatter classes "off".

Is there any easy trick to plot only class "a" or "b"? In principle I'd like to use this meta-data as a selection or filter on the data. It would be extremely useful.

Not naming the scatter class would not help, all the points are still there but unformatted, and I could not find another solution in a reasonable time. If it's not possible with the classes, can such filtering be done in any other way?

7

For applications like these, I would go with \addplot ... table instead of \addplot ... coordinates. That makes it easier to access coordinate attributes, and it also makes it much easier to use data from external applications in PGFPlots.

If you provide the data as a table, you can use the discard if={<column name>}{<value>} and discard if not styles from Is it possible to change the color of a single bar when the bar plot is based on symbolic values? for filtering your data:

\documentclass[10pt]{amsart}

\usepackage{pgfplots}
\pgfplotsset{compat=1.11}


\begin{document}

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


\begin{tikzpicture}
\begin{axis}[
    scatter/classes={
        a={mark=square*,blue},%
        b={mark=triangle*,red},%
        c={mark=o,draw=black}
    }
]
\addplot[scatter,only marks,
    scatter src=explicit symbolic,
    discard if={class}{a}
]
    table [meta index=2]{
        x       y       class
        0.1     0.15    a
        0.45    0.27    c
        0.02    0.17    a
        0.06    0.1     a
        0.9     0.5     b
        0.5     0.3     c
        0.85    0.52    b
        0.12    0.05    a
        0.73    0.45    b
        0.53    0.25    c
        0.76    0.5     b
        0.55    0.32    c
       };
\end{axis} 
\end{tikzpicture}

\end{document}
  • This works great, thanks! I'm using a large table actually (a .csv file), and I tested it. The legend entries of course have to be removed manually, but otherwise it's great. I wish your code was included in the package :) – seloran Nov 28 '14 at 22:07

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.