如何使混淆矩阵中的文本垂直化?

如何使混淆矩阵中的文本垂直化?

我想修改此处显示的混淆矩阵:

在此处输入图片描述

我需要使单词垂直,这是代码:

   \newcommand\MyBox[1]{%
  \fbox{\parbox[c][2cm][c]{2cm}{\centering #1}}%
  
}
\newcommand\MyVBox[1]{%
  \parbox[c][1cm][c]{1cm}{\centering\bfseries #1}%
}  
\newcommand\MyHBox[2][\dimexpr1.7cm+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{figure}
\begin{center}
{

\offinterlineskip

\raisebox{-5cm}[0pt][0pt]{
\parbox[c][0pt][c]{0cm}{\hspace{-3.5cm}\rot{\textbf{Predicted}}\\[20pt]}}\par

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

\hspace*{1cm}\MyHBox{RIGHT}\MyHBox{WRONG}\par

\MyTBox {RIGHT}{TP}{FP}

\MyTBox{WRONG}{FN}{TN}

}
\end{center}
\end{figure}

答案1

欢迎来到 TeX.SE!!!

tabularray这是一个使用包和几个\rotatebox宏(来自包)的可能解决方案graphicx

\documentclass{article}
\usepackage{lipsum}     % dummy text
\usepackage{graphicx}   % for \rotatebox
\usepackage{tabularray} % pretty tables

\begin{document}
\lipsum[1]
\begin{table}[ht]\centering
\caption{Confusion matrix}
\begin{tblr}
{% format
   hline{3-5}={3-5}{},vline{3-5}={3-5}{},
   row{1-2}={font=\bfseries},column{1-2}={font=\bfseries},
   row{3-4}={ht=1.6cm},column{3-4}={wd=1.6cm},
   cell{1}{3}={c=2}{},cell{3}{1}={r=2}{},cells={valign=t,halign=c}
}% content
                          &                                 & Actual\\
                          &                                 & RIGHT & WRONG\\
\rotatebox{90}{Predicted} & \rotatebox[origin=c]{90}{RIGHT} & TP    & FP\\
                          & \rotatebox[origin=c]{90}{WRONG} & FN    & TN
\end{tblr}
\end{table}

\lipsum[2]
\end{document}

在此处输入图片描述

答案2

编写混淆矩阵的另一种方法是使用 withtabularray包:

\documentclass{article}
\usepackage{lipsum}     % dummy text hiller

\usepackage[skip=1ex]{caption}
\usepackage{rotating}   % for \rotate rothead
\usepackage{makecell}   % for rothead
\renewcommand\theadfont{}
\makeatletter   % remove bug in makecell
                % https://tex.stackexchange.com/questions/8149/
\def\@rothead[#1]#2{\thead{\\[-.65\normalbaselineskip]
  \turn{\cellrotangle}\thead[#1]{#2}\endturn}}
\makeatother
\usepackage{tabularray} % pretty tables

\begin{document}
\lipsum[66]
    \begin{table}[ht]
    \centering
    \caption{Confusion matrix}
\begin{tblr}{hline{3-5} = {3-5}{}, vline{3-5} = {3-5}{},
             colspec    = {*4{Q[c, m]}},
             column{1}  = {font=\bfseries, cmd=\settowidth\rotheadsize{Predicted}\rothead},
             column{2}  = {font=\bfseries, cmd=\settowidth\rotheadsize{WRONG}\rothead[c]},
             row{1-2}   = {font=\bfseries},
            }
% content
            &           &   \SetCell[c=2]{c}    Actual    
                                    &           \\
            &           &   RIGHT   & WRONG     \\
\SetCell[r=2]{c}    Predicted   
            &   RIGHT  &    TP      & FP        \\
            &   WRONG  &    FN      & TN
\end{tblr}
    \end{table}
\lipsum[66]
\end{document}

在此处输入图片描述

答案3

这是一个{NiceTabular}使用 的解决方案nicematrix

\documentclass{article}
\usepackage{nicematrix}

\begin{document}

\begin{NiceTabular}{>{\bfseries}c@{}>{\bfseries}m[c]{5mm}m[c]{16mm}m[c]{16mm}}
\RowStyle{\bfseries}
& & \Block{1-2}{Actual} \\
\RowStyle{\bfseries}
& & RIGHT & WRONG \\
\rule[-5mm]{0pt}{15mm}\Block{2-1}{\rotate Predicted}
& \rotatebox{90}{RIGHT} & \Block[hvlines]{2-2}{}
          TP & FP \\
\rule[-5mm]{0pt}{15mm}
& \rotatebox{90}{WRONG} & FN & TN \\
\end{NiceTabular}

\end{document}

上述代码的输出

相关内容