2

I'm trying to generate a scatter plot using the pgfplots package. However, I'm having troubles with the scatter/classes and scatter srcoptions. For some reason, LaTeX keeps sending me the following error:

Package pgfplots Warning: scatter/classes: can't find class for

At the end, LaTeX generates the chart but it does not assign any style to the coordinates: enter image description here

Here's a minimal example of my code:

\documentclass[border = 1cm]{standalone}

\usepackage{tikz} % https://www.ctan.org/pkg/pgf
\usepackage{pgfplots} % https://www.ctan.org/pkg/pgfplots
   % -> 
   \pgfplotsset{compat = newest} 
   \usetikzlibrary{plotmarks}

\begin{filecontents}{data.dat}
c1  c2  c3
1   1   a
2   4   b
7   2   c
\end{filecontents}

\begin{document}

\begin{tikzpicture}
   \begin{axis}[
      scatter/classes = {
         a = {mark = *, draw = green},
         b = {mark = *, draw = blue},
         c = {mark = *, draw = red}
       }]
      \addplot[                 
         scatter,
         only marks,
         scatter src = explicit symbolic] 
      table[
         x = c1, 
         y = c2,
         meta = c3,
         col sep = space
      ]{data.dat};
   \end{axis}
\end{tikzpicture}

\end{document}
Alder Miguel
  • 392
  • 1
  • 11

1 Answers1

2

You only need to replace the spaces between a and =, and similarly for b and c. Otherwise the names of the classes will contain these spaces.

\documentclass[border = 1cm]{standalone}
\usepackage{filecontents}
\begin{filecontents*}{data.csv}
c1;c2;c3
1;1;a
2;4;b
7;2;c
\end{filecontents*}


\usepackage{tikz} % https://www.ctan.org/pkg/pgf
\usepackage{pgfplots} % https://www.ctan.org/pkg/pgfplots
   % -> 
   \pgfplotsset{compat = newest} 
   \usetikzlibrary{plotmarks}

\begin{document}

\begin{tikzpicture}
   \begin{axis}[
      scatter/classes = {
         a={mark = *, draw = green},
         b={mark = *, draw = blue},
         c={mark = *, draw = red}
       }]
      \addplot[                 
         scatter,
         only marks,
         scatter src=explicit symbolic] 
      table[
         x = c1, 
         y = c2,
         meta = c3,
         col sep = semicolon
      ]{data.csv};
   \end{axis}
\end{tikzpicture}
\end{document}

enter image description here

Now the marks have a (hardly visible) halo of the respective color, if you want them also filled in these colors, use

\documentclass[border = 1cm]{standalone}
\usepackage{filecontents}
\begin{filecontents*}{data.csv}
c1;c2;c3
1;1;a
2;4;b
7;2;c
\end{filecontents*}


\usepackage{tikz} % https://www.ctan.org/pkg/pgf
\usepackage{pgfplots} % https://www.ctan.org/pkg/pgfplots
   % -> 
   \pgfplotsset{compat = newest} 
   \usetikzlibrary{plotmarks}

\begin{document}

\begin{tikzpicture}
   \begin{axis}[
      scatter/classes = {
         a={mark = *, green},
         b={mark = *, blue},
         c={mark = *, red}
       }]
      \addplot[                 
         scatter,
         only marks,
         scatter src=explicit symbolic] 
      table[
         x = c1, 
         y = c2,
         meta = c3,
         col sep = semicolon
      ]{data.csv};
   \end{axis}
\end{tikzpicture}
\end{document}

enter image description here