2

Hello I would like to construct a Confusion Matrix as described here: confusion matrix using only LaTeX code

enter image description here

Unfortunately I have failed to adjust the code :(

I got as close as this thread, but the labels "actual" & "predicted" are not really centred. enter image description here

Can somebody help?

Niclas
  • 69
  • 3
    Welcome to TeX.SE. It looks like you've simply copied the image from the other answer. Can you at least expand that source to get the 3x3 matrix? We like to see at least some sort of attempt on this site. – Teepeemm Aug 27 '19 at 11:53

3 Answers3

4

After a coffee break I was able to answer it. :D

Still thank you for your help!!

enter image description here

\documentclass[11pt]{report}
\usepackage{graphicx}
\usepackage[printonlyused,nohyperlinks]{acronym}

%%%% Confusion Matrix Body %%%%
\newcommand\MyBox[1]{%
    \fbox{\parbox[c][3cm][c]{3cm}{\centering #1}}%
    % Size of boxes
}
\newcommand\MyVBox[1]{%
    \parbox[c][1cm][c]{1cm}{\centering\bfseries #1}%
}  
\newcommand\MyHBox[2][\dimexpr3cm+2\fboxsep\relax]{%
    \parbox[c][1cm][c]{#1}{\centering\bfseries #2}%
}  
\newcommand\MyTBox[4]{%
    \MyVBox{#1}
    \MyBox{#2}\hspace*{-\fboxrule}%
    \MyBox{#3}\par\vspace{-\fboxrule}%
}  
%%%%
\newcommand*\rot{\rotatebox{90}}

\begin{document}

\begin{acronym}
    \acro{TP}{True Positive}
    \acro{FP}{False Positive}
    \acro{TN}{True Negative}
    \acro{FN}{False Negative}
\end{acronym}

\begin{figure}
    \begin{center}
    {

        \offinterlineskip

        \raisebox{-6cm}[0pt][0pt]{
            \parbox[c][5pt][c]{1cm}{\hspace{-4.1cm}\rot{\textbf{Actual}}\\[20pt]}}\par

        \hspace*{1cm}\MyHBox[\dimexpr3.4cm+6\fboxsep\relax]{Predicted}\par

        \hspace*{1cm}\MyHBox{Cancelled}\MyHBox{Not Cancelled}\par

        \MyTBox{\rot{Cancelled}}{\ac{TP}}{\ac{FN}}

        \MyTBox{\rot{Not Cancelled}}{\ac{FP}}{\ac{TN}}

    }
\end{center}

\end{figure}
\end{document}

Because I am interested is there a better way to make Actual aligned in the middle with the same distance as Predicted. (My solution was try and error)

Niclas
  • 69
3

Hope the below code meets your requirement:

\documentclass{book}
\usepackage{rotating,multirow}
\begin{document}

\begin{tabular}{cc|c|c|c|}
&\multicolumn{1}{c}{}&\multicolumn{3}{c}{\textbf{Source1}}\\
&\multicolumn{1}{c}{}&\multicolumn{1}{c}{\textbf{A}}
&\multicolumn{1}{c}{\textbf{B}}
&\multicolumn{1}{c}{\textbf{C}}\\
\cline{3-5}
\multicolumn{1}{c}{\multirow{3}{*}{\rotatebox{90}{\textbf{Source2}}}}
&\textbf{A} &0.1 &0.3 &0.5\\
\cline{3-5}
&\textbf{B} &0.5 &0.2 &0.1\\
\cline{3-5}
&\textbf{C} &0.4 &0.7 &0.2\\
\cline{3-5}
\end{tabular}

\end{document}

And the output is:

enter image description here

MadyYuvi
  • 13,693
0
\begin{tabular}{cc|c|c|}
&\multicolumn{1}{c}{}&\multicolumn{2}{c}{\textbf{Predicted}}\\
&\multicolumn{1}{c}{}&\multicolumn{1}{c}{}{\textbf{0}}
&\multicolumn{1}{c}{\textbf{1}}\\
\cline{3-4}
\multicolumn{1}{c}{\multirow{2}{*}{\rotatebox{90}{\textbf{True}}}}
&\textbf{0} &0.1 &0.3\\
\cline{3-4}
&\textbf{1} &0.5 &0.2\\
\cline{3-4}
\end{tabular}
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center. – Community Apr 19 '23 at 12:34