How to create pgfplotstable confusion matrix ?
Situation
I want to create a confusion matrix (for example like this), reading data from a file and displaying it flexibly. Moreover, I found these two:
which have all features that only need to be combined.
However, I was unable to do so (for MWE, where I tried to mingle the code together, see below).
I would like to keep the pgfplotstable command for its options (colormaps, easy setup of min and max).
The MWE-Result:
Problem
The Problem is, that I don't know how to combine the two aforementioned MWEs.
I don't really know how the colormap is applied in the first, the scaling is done or how the text is hidden in the second.
The scale-method described there (\resizebox as well) does not allow for specific sizes, i.e. sx=2, sy=100 (mostly squares).
I am aware of this post, but would prefer to use colormaps.
Question
How to create colormap-colored
pgfplotstableconfusion matrix with specifically set cell sizes (row heights together with column width) ?
MWE
% https://tex.stackexchange.com/a/83865/
\documentclass{standalone}
\usepackage[svgnames]{xcolor}
\usepackage{colortbl}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\usepackage{tikz}
\usepgfplotslibrary{colormaps}
\pgfplotstableset{
/color cells/min/.initial=0,
/color cells/max/.initial=1000,
/color cells/textcolor/.initial=,
%
% Usage: 'color cells={min=<value which is mapped to lowest color>,
% max = <value which is mapped to largest>}
color cells/.code={%
\pgfqkeys{/color cells}{#1}%
\pgfkeysalso{%
postproc cell content/.code={%
% inserted by me: https://tex.stackexchange.com/a/123728
\pgfkeysalso{@cell content=\rule{0cm}{2.4ex}}
%
\begingroup
%
% acquire the value before any number printer changed
% it:
\pgfkeysgetvalue{/pgfplots/table/@preprocessed cell content}\value
\ifx\value\empty
\endgroup
\else
\pgfmathfloatparsenumber{\value}%
\pgfmathfloattofixed{\pgfmathresult}%
\let\value=\pgfmathresult
%
% map that value:
\pgfplotscolormapaccess
[\pgfkeysvalueof{/color cells/min}:\pgfkeysvalueof{/color cells/max}]
{\value}
{\pgfkeysvalueof{/pgfplots/colormap name}}%
% now, \pgfmathresult contains {<R>,<G>,<B>}
%
% acquire the value AFTER any preprocessor or
% typesetter (like number printer) worked on it:
\pgfkeysgetvalue{/pgfplots/table/@cell content}\typesetvalue
\pgfkeysgetvalue{/color cells/textcolor}\textcolorvalue
%
% tex-expansion control
% see https://tex.stackexchange.com/questions/12668/where-do-i-start-latex-programming/27589#27589
\toks0=\expandafter{\typesetvalue}%
\xdef\temp{%
\noexpand\pgfkeysalso{%
@cell content={%
\noexpand\cellcolor[rgb]{\pgfmathresult}%
\noexpand\definecolor{mapped color}{rgb}{\pgfmathresult}%
\ifx\textcolorvalue\empty
\else
\noexpand\color{\textcolorvalue}%
\fi
\the\toks0 %
}%
}%
}%
%
\endgroup
%
\temp
\fi
}%
}%
}
}
% https://tex.stackexchange.com/a/148141
\pgfplotscreatecolormap{test}{
[1pt] % colormap steps: 1pt
color(-20pt)=(red);
color(-10pt)=(red!10!white);
color(0pt)=(white);
color(10pt)=(green!10!white);
color(20pt)=(green);
}
\begin{document}
\pgfplotstableread[col sep=comma]{
50, -100, -70, -10
-20, 0, 100, 20
5, 30, 40, 800
3, 30, 50, 70
}\mymatrix
\pgfplotstabletypeset[
col sep=comma,
/pgfplots/colormap name=test,
color cells={
min=-100,
max=100
},
]\mymatrix
\end{document}





\pgfkeysalso{@cell content=\rule{0cm}{2.4ex}}-- in hindsight this probably just covers the text). Let's assume we have 3 digits, e.g. at most1.00or100 %as in the links provided in my previous comment, or that the user has to fiddle with the numbers that become larger. – BadAtLaTeX Apr 08 '20 at 20:33matrix plot, see the pgfplots manual v1.16 pp. 171. This certainly can create the plots of the links of your first comments. Are you aware of this option? – Apr 08 '20 at 20:38