3

I am trying to create a colored confusion matrix using macro for color gradients. Like the following one:

enter image description here

The final confusion matrix will be bigger, so I want to remove the zero values to improve the readability of the table. I cannot simply omit the zero from the cell, because of the macro that calculates the color gradient. Is there a way to omit the zeros maintaining the macro? Here the code I am using:

\documentclass{article}

\usepackage[utf8]{inputenc}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% COLORED CM
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\usepackage[table]{xcolor}
\usepackage{hhline}
\usepackage{pgf}
\usepackage{tikz}
\usepackage{collcell}

 %The min, mid and max values
\newcommand*{\MinNumber}{0.0}%
\newcommand*{\MidNumber}{0.2} %
\newcommand*{\MaxNumber}{1.0}%

%Apply the gradient macro
\newcommand{\ApplyGradient}[1]{%
        \ifdim #1 pt > \MidNumber pt
            \pgfmathsetmacro{\PercentColor}{max(min(100.0*(#1 - \MidNumber)/(\MaxNumber-\MidNumber),100.0),0.00)} %
            \hspace{-0.33em}\colorbox{green!\PercentColor!red}{#1}
        \else
            \pgfmathsetmacro{\PercentColor}{max(min(100.0*(\MidNumber - #1)/(\MidNumber-\MinNumber),100.0),0.00)} %
            \hspace{-0.33em}\colorbox{white!\PercentColor!red}{#1}
        \fi
}
\newcolumntype{G}{>{\collectcell\ApplyGradient}c<{\endcollectcell}}
\renewcommand{\arraystretch}{0}
\setlength{\fboxsep}{3mm} % box size
\setlength{\tabcolsep}{0pt}

%% for rotation in table
\usepackage{adjustbox}
\usepackage{array}

\newcolumntype{R}[2]{%
    >{\adjustbox{angle=#1,lap=\width-(#2)}\bgroup}%
    l%
    <{\egroup}%
}
\newcommand*\rotz{\multicolumn{1}{R{0}{-1em}}}% no optional argument here, please! 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% END COLORED CM
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{document}


\begin{table}[t]
 \caption{The confusion matrix}
 \label{tab:CM}
    \centering
    \resizebox{1\textwidth}{!}{ % to nicely resize the table in the text width

   \begin{tabular}{r*{6}{G}}
  \rotz{} & 
  \rotz{A} & 
  \rotz{B} & 
  \rotz{C} &
  \rotz{D} & 
  \rotz{E} \smallskip \\ 
   A        & 0.54  & 0     & 0.08  & 0.17  & 0     \\ 

   B        & 0     & 0.96  & 0     & 0     & 0     \\ 

   C        & 0.04  & 0.04  & 0.38  & 0.13  & 0     \\ 

   D        & 0.08  & 0     & 0.04  & 0.75  & 0     \\ 

   E        & 0     & 0.04  & 0     & 0     & 0.96  \\ 

  \end{tabular}%\par\bigskip
  }
\end{table}


\end{document}

Thanks in advance!

3

1 Answer 1

3
\documentclass{article}
\usepackage[table]{xcolor}
\usepackage{pgf}
\usepackage{tikz}
\usepackage{collcell}
\usepackage{caption}

%The min, mid and max values
\newcommand*{\MinNumber}{0.0}%
\newcommand*{\MidNumber}{0.2} %
\newcommand*{\MaxNumber}{1.0}%
\def\OmitZero#1.#2.#3!{%
  \ifx\relax#2\relax     % #2 is empty -> no decimal
    #1%
  \else                  % #2 not empty -> decimal
    \ifnum#1=0           % we have 0.
      .#2%
    \else                % we have no 0.xx
      #1.#2%
    \fi
  \fi}

%Apply the gradient macro
\newcommand{\ApplyGradient}[1]{%
    \ifdim #1 pt > \MidNumber pt
    \pgfmathsetmacro{\PercentColor}{max(min(100.0*(#1 - 
    \MidNumber)/(\MaxNumber-\MidNumber),100.0),0.00)} %
    \hspace{-0.33em}\colorbox{green!\PercentColor!red}{\OmitZero#1..!}
    \else
    \pgfmathsetmacro{\PercentColor}{max(min(100.0*(\MidNumber - 
    #1)/(\MidNumber-\MinNumber),100.0),0.00)} %
    \hspace{-0.33em}\colorbox{white!\PercentColor!red}{\OmitZero#1..!}
    \fi
}
\newcolumntype{G}{>{\collectcell\ApplyGradient}c<{\endcollectcell}}
\renewcommand{\arraystretch}{0}
\setlength{\fboxsep}{3mm} % box size
\setlength{\tabcolsep}{0pt}

%% for rotation in table
\usepackage{adjustbox}
\usepackage{array}

\newcolumntype{R}[2]{%
    >{\adjustbox{angle=#1,lap=\width-(#2)}\bgroup}%
    l%
    <{\egroup}%
}
\newcommand*\rotz{\multicolumn{1}{R{0}{-1em}}}% no optional argument here, please! 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% END COLORED CM
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{document}


    \begin{table}[t]
        \caption{The confusion matrix}
        \label{tab:CM}
        \centering
        \resizebox{1\textwidth}{!}{ % to nicely resize the table in the text width

            \begin{tabular}{r*{6}{G}}
                \rotz{} & 
                \rotz{A} & 
                \rotz{B} & 
                \rotz{C} &
                \rotz{D} & 
                \rotz{E} \smallskip \\ 
                A        & 0.54  & 0     & 0.08  & 0.17  & 0     \\ 

                B        & 0     & 0.96  & 0     & 0     & 0     \\ 

                C        & 0.04  & 0.04  & 0.38  & 0.13  & 0     \\ 

                D        & 0.08  & 0     & 0.04  & 0.75  & 0     \\ 

                E        & 0     & 0.04  & 0     & 0     & 0.96  \\ 

            \end{tabular}%\par\bigskip
        }
    \end{table}

\end{document}

enter image description here

1
  • Thanks, this is what I want. I used also the advice of @DavidCarlisle Mar 31, 2017 at 9:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .