Confusion matrix with LaTeX

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

\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}
Advertisements

One thought on “Confusion matrix with LaTeX

  1. 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}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s