1

So I have found this wonderful confusion matrix on this thread. I was wondering how could I add color to the background of the different boxes.

\documentclass{article}
\usepackage{array}
\usepackage{graphicx}
\usepackage{multirow}

\newcommand\MyBox[2]{
  \fbox{\lower0.75cm
   \vbox to 1.7cm{\vfil
    \hbox to 1.7cm{\hfil\parbox{1.4cm}{#1\\#2}\hfil}
   \vfil}%
 }%
}

\begin{document}

\noindent
\renewcommand\arraystretch{1.5}
\setlength\tabcolsep{0pt}
\begin{tabular}{c >{\bfseries}r @{\hspace{0.7em}}c @{\hspace{0.4em}}c 
@{\hspace{0.7em}}l}
\multirow{10}{*}{\rotatebox{90}{\parbox{1.1cm}{\bfseries\centering actual\\ 
value}}} & 
& \multicolumn{2}{c}{\bfseries Prediction outcome} & \\
& & \bfseries p & \bfseries n & \bfseries total \\
& p$'$ & \MyBox{True}{Positive} & \MyBox{False}{Negative} & P$'$ \\[2.4em]
& n$'$ & \MyBox{False}{Positive} & \MyBox{True}{Negative} & N$'$ \\
& total & P & N &
\end{tabular} 

\end{document}
2

For instance, you can replace the fbox with the fcolorbox macro from the package xcolor for adding colors. The newcommand MyBox is now defined as:

 \MyBox{<frame color>}{<background color>}{<text1>}{<text2>}

Output:

enter image description here

Code:

\documentclass{article}
\usepackage{array}
\usepackage{graphicx}
\usepackage{multirow}
\usepackage{xcolor}

\newcommand\MyBox[4]{
\fcolorbox{#1}{#2}{\lower0.75cm
\vbox to 1.7cm{\vfil
\hbox to 1.7cm{\hfil\parbox{1.4cm}{#3\\#4}\hfil}
\vfil}%
}%
}

\begin{document}

\noindent
\renewcommand\arraystretch{1.5}
\setlength\tabcolsep{0pt}
\begin{tabular}{c >{\bfseries}r @{\hspace{0.7em}}c @{\hspace{0.4em}}c 
@{\hspace{0.7em}}l}
\multirow{10}{*}{\rotatebox{90}{\parbox{1.1cm}{\bfseries\centering actual\\ 
value}}} & 
& \multicolumn{2}{c}{\bfseries Prediction outcome} & \\
& & \bfseries p & \bfseries n & \bfseries total \\
& p$'$ & \MyBox{red}{yellow}{True}{Positive} & \MyBox{orange}{white}{False}{Negative} & P$'$ \\[2.4em]
& n$'$ & \MyBox{green}{blue}{False}{Positive} & \MyBox{black}{red}{True}{Negative} & N$'$ \\
& total & P & N &
\end{tabular} 

\end{document}
| improve this answer | |

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.