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}
3
  • 2
    Tip: Instead of linking to some file for download, you could add \pgfplotstableread[row sep=\\]{c1 c2 c3 \\ 0 0 a \\ 1 0 b \\ 2 0 c\\}\datatable to the preamble, and use \datatable instead of data.csv in the \addplot. Makes the example self contained as well. (You can also use actual linebreaks instead of \\, but that doesn't work well in comments.) Oct 31 '19 at 22:54
  • 2
    I almost completely agree with @TorbjørnT. but would actually suggest to add the table via filecontents, the reason being that preloading the table and plotting it from the file are not completely equivalent. I will add this to my answer.
    – user194703
    Oct 31 '19 at 23:01
  • Thank you. I edited my question to be self-contained with filecontents. Oct 31 '19 at 23:12
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

1
  • Thank you very much. I didn't know that aspect of the scatter/classes names. Oct 31 '19 at 22:42

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.