我有一个混淆矩阵,范围从 0.0 到 1.0。其中超过 0.70 的值是不好的。因此,我将它们涂成红色。我遵循所介绍的示例 这里为我的混淆表着色。现在我唯一的问题是,当它完全变成红色(接近 1.0 值)时,黑色的文本颜色不太可读。
我的代码如下。我可以对这段代码进行哪些更改,以便当颜色达到某个点时,它会改变文本颜色?由于我刚接触 latex,所以我对其命令不是很熟悉。
%The min, mid and max values
\newcommand*{\MinNumber}{0.0}%
\newcommand*{\MidNumber}{0.5} %
\newcommand*{\MaxNumber}{1.0}%
%Apply the gradient macro
\newcommand{\ApplyGradient}[1]{%
%\ifnum\pgfmathresult=0\relax\color{white}\fi
\ifdim #1 pt > \MidNumber pt
\pgfmathsetmacro{\PercentColor}{max(min(100.0*(#1 - \MidNumber)/(\MaxNumber-\MidNumber),100.0),0.00)} %
\hspace{-0.33em}\colorbox{red!\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}}}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% END COLORED CM
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\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*{4}{G}}
\rotz{} &
\rotz{A} &
\rotz{B} &
\rotz{C} & \smallskip \\
A & 0.54 & 0 & 0.08 \\
B & 0 & 0.96 & 0 \\
C & 0.04 & 0.04 & 0.38 \\
\end{tabular}%\par\bigskip
}
\end{table}
如图所示。如果我缩小表格尺寸,解码为红色的极值的黑色字体颜色就不太明显了。我想将这些极值的颜色更改为白色,以提高可见性。
答案1
您的代码中有几个虚假空格,导致尝试使用 取消它们\hspace{-0.33em}
。最好将它们删除。
可以简化计算,其xfp
在浮点数方面优于 PGF,并提供可扩展运营。
\documentclass{article}
\usepackage{xcolor}
\usepackage{collcell}
% xfp is better than PGF in floating point numbers
\usepackage{xfp}
%The min, mid and max values
\newcommand*{\MinNumber}{0.0}
\newcommand*{\MidNumber}{0.5}
\newcommand*{\MaxNumber}{1.0}
\ExplSyntaxOn
\NewExpandableDocumentCommand{\fpcompareTF}{mmm}
{
\fp_compare:nTF { #1 } { #2 } { #3 }
}
\ExplSyntaxOff
\newcommand{\calcpercent}[2]{%
\fpeval{%
max(min(100*(#1-\MidNumber)/(#2-\MidNumber),100),0)%
}%
}
%Apply the gradient macro
\newcommand{\ApplyGradient}[1]{%
\fpcompareTF{#1 > \MidNumber}
{%
\fpcompareTF{#1 > 0.90}{\color{white}}{}%
\colorbox{red!\calcpercent{#1}{\MaxNumber}!red}{#1}%
}%
{%
\colorbox{white!\calcpercent{#1}{\MinNumber}!red}{#1}%
}%
}
\newcolumntype{G}{>{\collectcell\ApplyGradient}c<{\endcollectcell}}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% END COLORED CM
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}
\begin{table}[t]
\centering
\setlength{\tabcolsep}{0pt}
\renewcommand{\arraystretch}{0}
\caption{The confusion matrix}
\label{tab:CM}
\medskip
\begin{tabular}{r@{\,}*{3}{G}}
& \multicolumn{1}{@{}c@{}}{A}
& \multicolumn{1}{c}{B}
& \multicolumn{1}{c}{C} \\[\smallskipamount]
A & 0.54 & 0 & 0.08 \\
B & 0 & 0.96 & 0 \\
C & 0.04 & 0.04 & 0.38 \\
\end{tabular}
\end{table}
\end{document}
也许您必须调整计算百分比的公式。