# Confusion matrix with LaTeX

The following code generates a confusion matrix whose diagonal elements have colored background.

\documentclass{article}
\usepackage{tabularx}
\usepackage{colortbl}
\usepackage{hhline}

\begin{document}

\begin{tabularx}{.7\textwidth}{>{\bfseries}c|c c c |}
& A & B & \multicolumn{1}{c}{C} \\
\hhline{----}
A & 4 \cellcolor[gray]{.8}& 1 & 0  \\
B & 0 & 5 \cellcolor[gray]{.8}& 0  \\
C & 0 & 2 & 3 \cellcolor[gray]{.8} \\
\hhline{~---}
\end{tabularx}

\end{document}

__ATA.cmd.push(function() {
__ATA.initSlot('atatags-26942',  {
collapseEmpty: 'before',
sectionId: '26942',
width: 300,
height: 250
});
});

__ATA.cmd.push(function() {
__ATA.initSlot('atatags-114160',  {
collapseEmpty: 'before',
sectionId: '114160',
width: 300,
height: 250
});
});

Related


## One thought on “Confusion matrix with LaTeX”

1. EagleFranck says:

Thank you very much for this it very easy to understand. however to produced the same confusion matrix its required few modifications as below.
\begin{tabularx}{.7\textwidth}{&|gt;{\bfseries}|c|c c c |}
& A & B & \multicolumn{1}{c}{C} \\
\hhline{—-}
A & 4 \cellcolor[gray]{.8}& 1 & 0 \\
B & 0 & 5 \cellcolor[gray]{.8}& 0 \\
C & 0 & 2 & 3 \cellcolor[gray]{.8} \\
\hhline{~—}
\end{tabularx}