1

I am drawing a heatmap/scatter plot with the mark colour determined based on a score. I have mapped scores to Red, Green and Blue values, and trying to colour the marks based on the RGB values. The data are given in a csv file. I found the original code here and adopted to my case.

My problem is, I get below error when I try to read the data from my csv file. But if I copy the content in the csv into my .tex file it works fine.

How do I fix this error? or is there a better way to draw this plot?

Any help is much appreciated

! Missing number, treated as zero. t l.40 \end{groupplot} ?

This is the sample code.

\documentclass[tikz,border=3.14mm]{standalone} 
\usepackage{pgfplots} \pgfplotsset{compat=1.16} 
\usepgfplotslibrary{groupplots} 

\begin{filecontents*}{myfile.csv}
 SAE;Affected categories;red;green;blue
 0.01;2;204;0;255
 0.01;3;204;0;255
 0.02;25;24;8;255
 0.03;17;113;45;255
 0.04;2;204;0;255
\end{filecontents*}

\begin{document}
\pgfplotstableread[col sep = semicolon]{myfile.csv}\myfile

\begin{tikzpicture}%
 \begin{groupplot}[]
  \nextgroupplot[%
   ylabel={Affected categories}
  ]%
  \addplot[
   scatter,%
   scatter/@pre marker code/.code={%
    \edef\temp{\noexpand\definecolor{mapped color}{RGB}{\pgfplotspointmeta}}%
    \temp
    \scope[draw=mapped color,fill=mapped color]%
   },%
   scatter/@post marker code/.code={%
    \endscope
   },%
   only marks,     
   mark=*,
   point meta={TeX code symbolic={%
    \edef\pgfplotspointmeta{\thisrow{red},\thisrow{green},\thisrow{blue}}%
   }},
  ] 
  table [x = {SAE}, y = {Affected categories}, col sep=semicolon]{\myfile};     
\end{groupplot}
\end{tikzpicture}

\end{document}
  • When I wrap your code fragment into \documentclass[tikz,border=3.14mm]{standalone} \usepackage{pgfplots} \pgfplotsset{compat=1.16} \usepgfplotslibrary{groupplots} \begin{document} and \end{document}, I do not get an error message when compiling this on an updated TeXLive 2019 installation. So please provide us with a full document that starts with \documentclass and ends with \end{document} and tell us how you compile it such that we can help you. – user121799 Jul 14 at 16:58
  • The error only happens when loading from an external csv file. If I copy the data into the .tex file, as in my previous example, it compiles without any issues. I updated the code make the case clearer. – Niroshan Jul 14 at 23:56
  • Could you please add a data file using filecontents (see e.g. tex.stackexchange.com/a/83929/121799 for how that works) that shows the problem. I am afraid that otherwise it will be next-to-impossible to find the reason. – user121799 Jul 14 at 23:59
  • Thanks for effort trying to help @marmot. I updated the example. It looks like trying to read the file with \pgfplotstableread is causing the problem – Niroshan Jul 15 at 0:16
  • 1
    What I want to say is that \addplot[<your options>] table [x = {SAE}, y = {Affected categories}, col sep=semicolon]{myfile.csv}; works beautifully. Unless you have an urgent reason to insist on ] table [x = {SAE}, y = {Affected categories}, col sep=semicolon]{\myfile};, which, as you point out, has problems, I would recommend using the former. – user121799 Jul 15 at 5:20
1

The behaviors of

\addplot[<options>] table[<options>] {<file>};

and

\addplot[<options>] table[<options>] {\macro};

are not precisely the same. This has been pointed out here. The "easier to deal with" option is the first one. So you can avoid all the problems by just doing

\documentclass[tikz,border=3.14mm]{standalone} 
\usepackage{pgfplots} 
\pgfplotsset{compat=1.16} 
\usepgfplotslibrary{groupplots} 

\begin{filecontents*}{myfile.csv}
 SAE;Affected categories;red;green;blue
 0.01;2;204;0;255
 0.01;3;204;0;255
 0.02;25;24;8;255
 0.03;17;113;45;255
 0.04;2;204;0;255
\end{filecontents*}

\begin{document}
\pgfplotstableread[col sep=semicolon]{myfile.csv}\myfile

\begin{tikzpicture}%
 \begin{groupplot}[]
  \nextgroupplot[%
   ylabel={Affected categories}
  ]%
  \addplot[
   scatter,%
   scatter/@pre marker code/.code={%
    \edef\temp{\noexpand\definecolor{mapped color}{RGB}{\pgfplotspointmeta}}%
    \temp
    \scope[draw=mapped color,fill=mapped color]%
   },%
   scatter/@post marker code/.code={%
    \endscope
   },%
   only marks,     
   mark=*,
   point meta={TeX code symbolic={%
    \edef\pgfplotspointmeta{\thisrow{red},\thisrow{green},\thisrow{blue}}%
   }},
  ] 
  table [x = {SAE}, y = {Affected categories}, col sep=semicolon]{myfile.csv};     
\end{groupplot}
\end{tikzpicture}
\end{document}

enter image description here

If you really need to use a macro, you can consult this answer and some linked answers. If you need to use the macro because you manipulated the data (e.g. sorted it or you dropped some rows), it could still be easier to write the resulting table to a file and then use it as above, see e.g. here.

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.