I already know how to make a table, I would like to know how I can put the colors inside each block. I need advice to get started.
Asked
Active
Viewed 2,091 times
0
-
You can use the cellcolor from the colortbl package. – leandriis Jan 09 '20 at 14:57
-
As an alternative, you could also use a tikz matrix. – leandriis Jan 09 '20 at 15:04
-
Be aware of the problem with disappearing rules if you consider using LaTeX tabulars, horizontal and vertical lines and colours. – Sveinung Jan 09 '20 at 15:44
-
1see https://tex.stackexchange.com/questions/251300/changing-color-in-foreach – Zarko Jan 09 '20 at 16:00
3 Answers
1
Here is a example you can work on. I have defined the L column with zero width as the last column. In row 3 to 7, I put a zero width rule width the same height as the column width for column 3 to 6, to ensure square cells. I also defined shortcuts for different colours, to reduce the clutter in the table.
Please feel free to ask questions, and I will update the answer.:
\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}
Sveinung
- 20,355
0
The best way to get started my be this site: https://www.tablesgenerator.com/
MWE from the generator
\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 with some enhancements
\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}
Sango
- 887
-
\multicolumn{1}{|c|}{...}? Wouldn't it be easier to directly usectype instead ofltype columns? – leandriis Jan 09 '20 at 15:41 -
0
The following might serve as a place to start from using a 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}
leandriis
- 62,593


