两个相同大小的表格tikz

两个相同大小的表格tikz

我正在使用以下代码创建两个表(tikz 包}

    \documentclass[twoside,11pt]{article}

\usepackage{tikz}
\usepackage{url}
\usepackage[T1]{fontenc}
\usetikzlibrary{positioning}
\usepackage{subcaption}

\newcommand{\confmatrix}{
\matrix (conmat) [row sep=.01cm,column sep=.01cm, ampersand replacement = \&] {
\node (tpos) [box, align = center,
    label=left:\( \mathbf{P'} \),
    label=above:\( \mathbf{P} \)
    ] {True  Positive \\ (tp)};
\&
\node (fneg) [box, align = center,
    label=above:\textbf{N},
        ] {False  Negative \\ (fn)};
\\
\node (fpos) [box, align = center,
    label=left:\( \mathbf{N'} \),
        ] {False  Positive \\ (fp)};
\&
\node (tneg) [box, align = center,
        ] {True  Negative \\ (tn)};
\\
};
\node [rotate=90,left=.01cm of conmat, text width=2.5cm,align=center,anchor=center] {\textbf{Actual Label}};
\node [above=.01cm of conmat] {\textbf{Predicted Label}};
}

\newcommand{\costmatrix}{
\matrix (costmat) [row sep=.01cm,column sep=.01cm, ampersand replacement = \&] {
\node (tpcos) [box, align = center] {$0$};
\&
\node (fncos) [box, align = center] {$1+\beta^2-t$};
\\
\node (fpcos) [box, align = center] {$t$};
\&
\node (tncos) [box, align = center] {$0$};
\\
};
}

\begin{document}
\begin{figure}[H]
\captionsetup[subfigure]{font=footnotesize}
\centering
\subcaptionbox{Contingency Table}[.4\textwidth]{
\begin{tikzpicture}[box/.style={draw,rectangle,text width=1.5cm,align=left}]
\confmatrix
\end{tikzpicture} }
\subcaptionbox{Cost Matrix}[.3\textwidth] {
\begin{tikzpicture}[box/.style={draw,rectangle,minimum size =1cm,text width=1.5cm,align=left}]
\costmatrix
\end{tikzpicture} }
\caption{Binary Classification}
\label{fig:conf_cost_mat}
\end{figure}
\end{document}

我的问题是,表格大小不一样。我希望两个表格大小相同。

答案1

您必须将两个矩阵中相等的列和行的最小大小固定为相等,以获得相似的大小。否则, 的第一列costmat将调整为比 的第一列更窄的大小conmat

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning}

\tikzset{box/.style={draw, minimum width=3cm, minimum height=1cm}}

\newcommand{\confmatrix}{
\matrix (conmat) [draw, row sep=.01cm,column sep=.01cm, ampersand replacement = \&] {
\node (tpos) [box, align = center,
    label=left:\( \mathbf{P'} \),
    label=above:\( \mathbf{P} \)
    ] {True  Positive \\ (tp)};
\&
\node (fneg) [box, align = center,
    label=above:\textbf{N} ] {False  Negative \\ (fn)};
\\
\node (fpos) [box, align = center, label=left:\( \mathbf{N'} \) ] {False  Positive \\ (fp)};
\&
\node (tneg) [box, align = center ] {True  Negative \\ (tn)};
\\
};
\node [rotate=90,left=.01cm of conmat, text width=2.5cm,align=center,anchor=center] {\textbf{Actual Label}};
\node [above=.01cm of conmat] {\textbf{Predicted Label}};
}

\newcommand{\costmatrix}{
\matrix (costmat) [draw, row sep=.01cm,column sep=.01cm, ampersand replacement = \&] {
\node (tpcos) [box, align = center] {$0$};
\&
\node (fncos) [box, align = center] {$1+\beta^2-t$};
\\
\node (fpcos) [box, align = center] {$t$};
\&
\node (tncos) [box, align = center] {$0$};
\\
};}

\begin{document}
\begin{tikzpicture}
\confmatrix

\begin{scope}[yshift=-3cm]
\costmatrix
\end{scope}

\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容