省略零值的彩色混淆矩阵

省略零值的彩色混淆矩阵

我正在尝试使用颜色渐变宏创建一个彩色混淆矩阵。如下所示:

在此处输入图片描述

最终的混淆矩阵会更大,所以我想删除零值以提高表格的可读性。由于计算颜色渐变的宏,我无法简单地从单元格中省略零。有没有办法省略维护宏的零?这是我使用的代码:

\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}

提前致谢!

答案1

\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}

在此处输入图片描述

相关内容