3

I would like to color every dot red where the corresponding second column of the data is set to 1.

This is my minimal working example:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{pgfplotstable}

\begin{document}

\pgfplotstableread[header=false, col sep=comma]{
    Label 1,1,0.0443
    Label 2,0,0.0371
    Label 3,1,0.0191
    Label 4,0,0.0638
    Label 5,0,0.0149
}\data

\begin{figure}
    \begin{tikzpicture}
        \centering
        \begin{axis}[
            axis x line=bottom,
            axis y line=left,
            xmin=0,
            width=10cm,
            ymajorgrids,
            bar width=2ex, y=3ex,
            enlarge y limits={abs=0.75},
            ytick=data,
            scaled ticks=false,
            yticklabels from table={\data}{0},
            xticklabel style={/pgf/number format/fixed},
        ]
            \addplot[only marks] table [
                y expr=-\coordindex,
                x index=2
            ]{\data};
        \end{axis}
    \end{tikzpicture}
\end{figure}

\end{document}

enter image description here

The first and the third dot should be red as defined in the data.

I have not yet found any approach to defining colors (or shape!) based on the data. Is it possible with TikZ/PGFPlots?

Xiphias
  • 1,233

2 Answers2

4

You can use scatter/classes for this.

enter image description here

\documentclass{standalone}
\usepackage[utf8]{inputenc}
\usepackage{pgfplots}
\usepackage{pgfplotstable}

\begin{document}

\pgfplotstableread[header=false, col sep=comma]{
    Label 1,1,0.0443
    Label 2,0,0.0371
    Label 3,1,0.0191
    Label 4,0,0.0638
    Label 5,0,0.0149
}\data

    \begin{tikzpicture}
        \centering
        \begin{axis}[
            axis x line=bottom,
            axis y line=left,
            xmin=0,
            width=10cm,
            ymajorgrids,
            bar width=2ex, y=3ex,
            enlarge y limits={abs=0.75},
            ytick=data,
            scaled ticks=false,
            yticklabels from table={\data}{0},
            xticklabel style={/pgf/number format/fixed},
            scatter src=explicit,
            scatter/classes={
                0={mark=*,black},
                1={mark=*,red}
            }
        ]
            \addplot[scatter,only marks] table [
                y expr=-\coordindex,
                x index=2,
                meta index=1 % specify meta column
            ]{\data};
        \end{axis}
    \end{tikzpicture}

\end{document}
Torbjørn T.
  • 206,688
0

Add red in \addplot[only marks,red] option

   \documentclass{article}
   \usepackage[utf8]{inputenc}
   \usepackage{tikz}
   \usepackage{pgfplots}
   \usepackage{pgfplotstable}

   \begin{document}

    \pgfplotstableread[header=false, col sep=comma]{
     Label 1,1,0.0443
     Label 2,0,0.0371
     Label 3,1,0.0191
     Label 4,0,0.0638
     Label 5,0,0.0149
    }\data

   \begin{figure}
    \begin{tikzpicture}
      \centering
      \begin{axis}[
        axis x line=bottom,
        axis y line=left,
        xmin=0,
        width=10cm,
        ymajorgrids,
        bar width=2ex, y=3ex,
        enlarge y limits={abs=0.75},
        ytick=data,
        scaled ticks=false,
        yticklabels from table={\data}{0},
        xticklabel style={/pgf/number format/fixed},
           ]
          \addplot[only marks,red] table [
            y expr=-\coordindex,
            x index=2
          ]{\data};
         \end{axis}
        \end{tikzpicture}
    \end{figure}

    \end{document}

enter image description here

Biki Teron
  • 3,275