1

I found a clarification about how to draw confusion matrix in latex in:

In this link

You can use the following code.

\documentclass[english]{article}
\usepackage[T1]{fontenc}
\usepackage[latin9]{inputenc}
\usepackage{babel}
\usepackage[table]{xcolor}
\usepackage{collcell}
\usepackage{hhline}
\usepackage{pgf}
\usepackage{multirow}

\def\colorModel{hsb} %You can use rgb or hsb

\newcommand\ColCell[1]{
  \pgfmathparse{#1<50?1:0}  %Threshold for changing the font color into the cells
    \ifnum\pgfmathresult=0\relax\color{white}\fi
  \pgfmathsetmacro\compA{0}      %Component R or H
  \pgfmathsetmacro\compB{#1/100} %Component G or S
  \pgfmathsetmacro\compC{1}      %Component B or B
  \edef\x{\noexpand\centering\noexpand\cellcolor[\colorModel]{\compA,\compB,\compC}}\x #1
  } 
\newcolumntype{E}{>{\collectcell\ColCell}m{0.4cm}<{\endcollectcell}}  %Cell width
\newcommand*\rot{\rotatebox{90}}

\begin{document}
\newcommand\items{3}   %Number of classes
\arrayrulecolor{white} %Table line colors
\noindent\begin{tabular}{cc*{\items}{|E}|}
\multicolumn{1}{c}{} &\multicolumn{1}{c}{} &\multicolumn{\items}{c}{Predicted} \\ \hhline{~*\items{|-}|}
\multicolumn{1}{c}{} & 
\multicolumn{1}{c}{} & 
\multicolumn{1}{c}{\rot{Class A}} & 
\multicolumn{1}{c}{\rot{Class B}} & 
\multicolumn{1}{c}{\rot{Class C}} \\ \hhline{~*\items{|-}|}
\multirow{\items}{*}{\rotatebox{90}{Actual}} 
&Class A  & 100   & 0  & 10   \\ \hhline{~*\items{|-}|}
&Class B  & 10   & 80  & 10   \\ \hhline{~*\items{|-}|}
&Class C  & 30   & 0   & 70   \\ \hhline{~*\items{|-}|}
\end{tabular}

\end{document}

You can control the color ranges using the HSB or RGB models and the formulas in compA, compB and compC.

For "Gray" range use:

\pgfmathsetmacro\compA{0}      %Component R or H
\pgfmathsetmacro\compB{0} %Component G or S
\pgfmathsetmacro\compC{#1/100}      %Component B or B

I would like to use the gray color but with reverse ranging of shown in the example. I mean use black box with 100 and as soon as the value decreases the colors decreases until it gets white with zero value.

The range that I want to reverse

BTW, I have tried to ask about this there, but I have no 50 reputation to make a comment.

  • 1
    (1) Welcome, (2) what does this have to do with biblatex (the tag used) – daleif Jul 17 '17 at 11:39
  • I dont know, I am so new in latex so I thought its related to it!! – Mamoda Jul 17 '17 at 11:43
  • Do you mean something like \pgfmathsetmacro\compB{1 - #1/100}? Of course doing this would require the complementary font-color change, as well: \pgfmathparse{#1>50?1:0} – Steven B. Segletes Jul 17 '17 at 11:44
  • Try \pgfmathsetmacro\compC{1-#1/100} . – Bobyandbob Jul 17 '17 at 11:44
  • Sorry for asking again, Is it possible to reduce the size of the table if I use many classes such 14? – Mamoda Jul 17 '17 at 12:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.