单元格中的颜色表

单元格中的颜色表

在此处输入图片描述

我已经知道如何制作表格了,我想知道如何在每个块内放置颜色。我需要一些建议才能开始。

答案1

这是一个您可以参考的示例。我已将宽度为零的 L 列定义为最后一列。在第 3 行至第 7 行中,我放置了一个宽度为零的规则,其高度与第 3 列至第 6 列的列宽相同,以确保单元格为正方形。我还为不同的颜色定义了快捷方式,以减少表格中的混乱。

请随时提问,我会更新答案。:

在此处输入图片描述

\documentclass[a4paper, 11pt]{article}
\usepackage{lmodern, calc, hhline}
\usepackage[dvipsnames,table]{xcolor} 
\usepackage{rotating}

\setlength\extrarowheight{2pt}                          % Move cell content down
\renewcommand{\arraystretch}{0.5}

\newcolumntype{C}{>{\centering\arraybackslash}m{1.5cm}}
\newcolumntype{L}{@{}m{0pt}@{}}   % To ensure equal row height 

 % Shortcuts
\newcommand*{\gray}{\cellcolor{gray!25}}
\newcommand*{\dgray}{\cellcolor{gray!50}}
\newcommand*{\blue}{\cellcolor{blue!25}}
\newcommand*{\red}{\cellcolor{red!25}}
\newcommand*{\yellow}{\cellcolor{yellow!25}}
\newcommand*{\tgreen}[1]{\textcolor{green!60!black}{#1}}
\newcommand*{\tred}[1]{\textcolor{red}{#1}}

\arrayrulecolor{gray!80}                        % Gray table rules
\setlength{\arrayrulewidth}{0.8pt}

\begin{document}
\footnotesize\sffamily
\begin{tabular}{@{}c@{\hspace{\tabcolsep}}c|*{4}{C|}L}                                                    
                    &\multicolumn{1}{c}{}&\multicolumn{4}{c}{\strut\bfseries Confusion Matrix}&\\
                    \hhline{~~|-|-|-|-|}%
                    \raisebox{-2.75cm}[0pt][0pt]{\rotatebox[origin=c]{90}{\bfseries Output Class}}
                    &   1
                    &  \blue\textbf{30}\linebreak  25.3\%
                    &   \red\textbf{4}\linebreak5.1\%
                    &   \red\textbf{2}\linebreak  2.5\%
                    &   \gray\tgreen{88.6\%}\linebreak\tred{11.4\%}
                    &   \rule{0pt}{1.5cm+1\tabcolsep}\\
                    \hhline{~~|-|-|-|-|}%

                    &   2
                    &  \red\textbf{2}\linebreak  2.5\%
                    &   \blue\textbf{22}\linebreak  27.8\%
                    &   \red\textbf{0}\linebreak  0.0\%
                    &   \gray\tgreen{91.7\%}\linebreak\tred{8.3\%}
                    &    \rule{0pt}{1.5cm+1\tabcolsep}\\
                    \hhline{~~|-|-|-|-|}%
                    &    3
                    &   \red\textbf{0}\linebreak  0.0\%
                    &   \red\textbf{1}\linebreak  1.3\%
                    &   \blue\textbf{28}\linebreak  35.4\%
                    &   \gray\tgreen{96.6\%}\linebreak\tred{3.4\%}
                    &   \rule{0pt}{1.5cm+1\tabcolsep}\\
                    \hhline{~~|-|-|-|-|}%
                    &
                    &   \yellow\tgreen{90.9\%}\linebreak\tred{9.1\%}
                    &   \yellow\tgreen{81.5\%}\linebreak\tred{16.5\%}
                    &   \yellow\tgreen{93.3\%}\linebreak\tred{11.4\%}
                    &   \dgray\tgreen{88.6\%}\linebreak\tred{11.4\%}
                    &   \rule{0pt}{1.5cm+1\tabcolsep}\\
                    \hhline{~~|-|-|-|-|}%
                    &\multicolumn{1}{c}{}&\multicolumn{1}{c}{\strut 1}&\multicolumn{1}{c}{2}&\multicolumn{1}{c}{3}&\multicolumn{1}{c}{}\\
                    &\multicolumn{1}{c}{}&\multicolumn{4}{c}{\strut\bfseries Target Class}&\\
        \end{tabular}
\end{document}

答案2

开始的最佳方式是这个网站: https://www.tablesgenerator.com/

来自发电机的 MWE

\documentclass[10pt,a4paper]{article}
\usepackage[latin1]{inputenc}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{graphicx}
\usepackage[table,xcdraw]{xcolor}
\begin{document}
\begin{table}[]
    \begin{tabular}{|l|l|l|l|}
        \hline
        \multicolumn{1}{|c|}{\cellcolor[HTML]{9AFF99}\begin{tabular}[c]{@{}c@{}}20\\ 25,3\%\end{tabular}} & \multicolumn{1}{c|}{\cellcolor[HTML]{FFCCC9}\begin{tabular}[c]{@{}c@{}}4\\ 5,1\%\end{tabular}} &  &  \\ \hline
        &                                                                                                &  &  \\ \hline
        &                                                                                                &  &  \\ \hline
        &                                                                                                &  &  \\ \hline
    \end{tabular}
\end{table}
\end{document}

MWE 进行了一些增强

\documentclass[10pt,a4paper]{article}
\usepackage[latin1]{inputenc}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{graphicx}
\usepackage[table,xcdraw]{xcolor}
\usepackage{makecell}
\begin{document}
    \begin{table}[]
    \begin{tabular}{|c|c|c|c|}
        \hline
        \cellcolor[HTML]{9AFF99}\makecell{20\\ 25,3\% } & \cellcolor[HTML]{FFCCC9}\makecell{4\\ 5,1\% } & \cellcolor[HTML]{FFCCC9}\makecell{4\\ 5,1\% } &  \\ \hline
        &                                                                                                &  &  \\ \hline
        &                                                                                                &  &  \\ \hline
        &                                                                                                &  &  \\ \hline
    \end{tabular}
\end{table}
\end{document}

答案3

下面可以作为使用 的起点tikz matrix

在此处输入图片描述

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{fit,matrix}


\begin{document}

\begin{tikzpicture}[cell/.style={rectangle,draw=black}, nodes in empty cells]
  \matrix(table)[
  matrix of nodes,
  row sep =-\pgflinewidth,
  column sep = -\pgflinewidth,
  nodes={anchor=center,minimum height=1.5cm,minimum width=1.5cm, align=center, text width=1cm, draw=black},
  column 1/.style = {nodes={cell, minimum width=0.25cm, fill=white, draw=none, align=right}},
  row 1/.style = {nodes={cell, minimum height=0.5cm, fill=none, draw=none, align=center}},
  row 6/.style = {nodes={cell, minimum height=0.5cm, fill=none, draw=none, align=center}},
  row 7/.style = {nodes={cell, minimum height=0.5cm, fill=none, draw=none, align=center}},
  ] 
  {& & & & \\
   1 
      & |[fill=green]|{20\\ 25.3\%} 
          & |[fill=red]|{4\\ 5.1\% }
              & |[fill=red]|{2\\ 2.5\%} 
                  & |[fill=blue]|{76.9\%\\ 23.1\%} \\
    2 
      & |[fill=red]|{20\\ 25.3\%} 
          & |[fill=green]|{4\\ 5.1\% }
              & |[fill=red]|{2\\ 2.5\%} 
                  & |[fill=blue]|{76.9\%\\ 23.1\%} \\
    3 
     & |[fill=red]|{20\\ 25.3\%} 
         & |[fill=red]|{4\\ 5.1\% }
             & |[fill=green]|{2\\ 2.5\%} 
                 & |[fill=yellow]|{76.9\%\\ 23.1\%} \\

     & |[fill=yellow]|{20\\ 25.3\%} 
         & |[fill=yellow]|{4\\ 5.1\% }
             & |[fill=yellow]|{2\\ 2.5\%} 
                 & |[fill=blue]|{76.9\%\\ 23.1\%} \\

     & 1
         & 2
             & 3\\
     & & & & \\};
  \node[fit=(table-1-2)(table-1-5)]{\textbf{Confusion Matrix}};
  \node[fit=(table-2-1)(table-5-1),rotate=90,anchor=center,transform shape, text width=5cm]
 {\textbf{Output Class}};
  \node[fit=(table-7-2)(table-7-5)]{\textbf{Target Class}};
\end{tikzpicture}

\end{document}

相关内容