5

I am making a confusion matrix for a document, and I was wondering if LaTeX has the capabilities to handle this. It is essentially a table, but with background shading corresponding to the value in the table (the shading could be hand-coded).

How can this be done in LaTeX? Is it possible? An example ishttps://i.stack.imgur.com/mLxlO.png

shoes
  • 53
  • 2
    Possible duplicate: http://tex.stackexchange.com/questions/42444/parametrize-shading-in-table-through-tikz – mythealias Nov 01 '12 at 16:31
  • @mythealias That looks like what I want, but when I include pgfplotstable into my document, I get a "command already defined" error, due to a conference paper's style file. – shoes Nov 01 '12 at 17:09

3 Answers3

5

Building on Qrrbrbirlbel's answer, the collcell package can be used to automatically apply the color to each cell without the need to explicitly use a macro; a little example (notice particularly the second matrix):

\documentclass{article}
\usepackage[table]{xcolor}
\usepackage{collcell}
\usepackage{hhline}
\usepackage{pgf}

\newcommand\gray{gray}

\newcommand\ColCell[1]{%
  \pgfmathparse{#1<.8?1:0}%
    \ifnum\pgfmathresult=0\relax\color{white}\fi
  \pgfmathparse{1-#1}%
  \expandafter\cellcolor\expandafter[%
    \expandafter\gray\expandafter]\expandafter{\pgfmathresult}#1}

\newcolumntype{E}{>{\collectcell\ColCell}c<{\endcollectcell}}

\begin{document}

\noindent\begin{tabular}{c*{6}{|E}|}
 \multicolumn{1}{c}{} & \multicolumn{1}{c}{hw} & \multicolumn{1}{c}{bx} 
  & \multicolumn{1}{c}{wk} & \multicolumn{1}{c}{jg} & \multicolumn{1}{c}{cl} 
  & \multicolumn{1}{c}{rn} \\ \hhline{~*6{|-}|}
 ving & 1 & 0 & 0 & 0 & 0 & 0 \\ \hhline{~*6{|-}|}
 xing & 0 & 0.92 & 0.08 & 0 & 0 & 0 \\ \hhline{~*6{|-}|} 
 king & 0 & 0.03 & 0.97 & 0 & 0 & 0 \\ \hhline{~*6{|-}|}
 ging & 0 & 0 & 0 & 1 & 0 & 0 \\ \hhline{~*6{|-}|}
 ping & 0 & 0 & 0 & 0 & 1 & 0 \\ \hhline{~*6{|-}|}
 ning & 0 & 0 & 0 & 0 & 0 & 1 \\ \hhline{~*6{|-}|}
\end{tabular}\par\bigskip

\noindent\begin{tabular}{ *{6}{E}}
 0.2 & 0.3 & 0.2 & 0.86 & 0 & 1 \\
 0.87 & 0.65 & 0.43 & 0.6 & 1 & 0.3 \\
 0.17 & 0.25 & 0.83 & 0.23 & 0.7 & 0.5 \\
 0.72 & 0.45 & 1 & 0.5 & 0.43 & 0.85 \\
 0.1 & 0.15 & 0 & 0.65 & 0.83 & 0.25 \\
 0 & 1 & 0.4 & 0.51 & 0.63 & 0.75 \\
\end{tabular}

\end{document}

enter image description here

Moriambar
  • 11,466
Gonzalo Medina
  • 505,128
4

xcolor is used for the colors.
hhline is used for horizontal lines that don't get covered by \cellcolor.
pgf is used for the calculations.

The command \gray is only defined to reduce the amount of \expandafters.
The length \tabwidth is used so that all columns have the same width.

Code

\RequirePackage[table]{xcolor}
\documentclass{article}
\usepackage{array,hhline}
\usepackage{pgf}

\newcommand*{\gray}{gray}
\newcommand*{\woB}[1]{\multicolumn{1}{c}{#1}}

\newcommand*{\colorme}[1]{%
    \pgfmathparse{#1<.5?1:0}%
    \ifnum\pgfmathresult=0\relax\color{white}\fi
    \pgfmathparse{1-#1}
    \expandafter\cellcolor\expandafter[\expandafter\gray\expandafter]\expandafter{\pgfmathresult}%
    #1%
}
\newlength{\tabwidth}
\settowidth{\tabwidth}{0.00}
\begin{document}
\begin{tabular}{l*6{|>{\centering\arraybackslash}m{\tabwidth}}|}
    \woB{} & \woB{hw}      & \woB{bx}       & \woB{wk}       & \woB{jg}    & \woB{cl}    & \woB{rn}    \\ \hhline{~*6{|-}|}
     ving  & \colorme{1}   & 0              & 0              & 0           & 0           & 0           \\ \hhline{~*6{|-}|}
     xing  & 0             & \colorme{0.92} & \colorme{0.08} & 0           & 0           & 0           \\ \hhline{~*6{|-}|}
     king  & 0             & \colorme{0.03} & \colorme{0.97} & 0           & 0           & 0           \\ \hhline{~*6{|-}|}
     ging  & 0             & 0              & 0              & \colorme{1} & 0           & 0           \\ \hhline{~*6{|-}|}
     ping  & 0             & 0              & 0              & 0           & \colorme{1} & 0           \\ \hhline{~*6{|-}|}
     ning  & 0             & 0              & 0              & 0           & 0           & \colorme{1} \\ \hhline{~*6{|-}|}
\end{tabular}
\end{document}

Output

enter image description here

Qrrbrbirlbel
  • 119,821
1

Here is Qrrbrbirlbel's solution without the mighty pgf.

\documentclass{article}
\usepackage[table]{xcolor}
\usepackage{array,hhline}
\makeatletter
\newcommand*{\ccol}[1]{%
  \ifdim#1pt<.5pt\relax\else\color{white}\fi
  \edef\x{\noexpand\cellcolor[gray]{\strip@pt\dimexpr1pt-#1pt}}\x
  #1%
}
\newlength{\cellwidth}
\settowidth{\cellwidth}{0.00}
\def\jline{\\\hhline{~*6{|-}|}}
\makeatother

\begin{document}
\begin{tabular}{l*6{|>{\centering\arraybackslash}m{\cellwidth}}|}
\noalign{\gdef\w#1{\multicolumn{1}{c}{#1}}}
\w{} & \w{hw} & \w{bx} & \w{wk} & \w{jg} & \w{cl} & \w{rn}\jline
ving & \ccol{1} & 0 & 0 & 0 & 0 & 0\jline
xing & 0 & \ccol{0.92} & \ccol{0.08} & 0 & 0 & 0 \jline
king & 0 & \ccol{0.03} & \ccol{0.97} & 0 & 0 & 0\jline
ging & 0 & 0 & 0 & \ccol{1} & 0 & 0\jline
ping & 0 & 0 & 0 & 0 & \ccol{1} & 0\jline
ning & 0 & 0 & 0 & 0 & 0 & \ccol{1}\jline
\noalign{\global\let\w\undefined}
\end{tabular}
\end{document}
Ahmed Musa
  • 11,742