# visualizing matrix factorization

I want to create diagram like below.

Update: Mostly done.

\documentclass{standalone}
\usepackage{amsmath}
\usepackage{tikz}
\usepackage{colortbl,pgfplotstable}
1     0     1
0     3     0
0     1     1
3     0     0
0     0     3
}\myB

1     0     1
1     1     0
0     0     1
}\myA

1     0     0     3     0
0     3     1     0     0
1     0     1     0     3
}\myD

1     0     1     1     1
1     1     2     1     0
1     1     1     1     1
1     0     1     1     2
2     0     1     0     1
}\myC

\pgfkeys{/mytable/.is family}
\pgfkeyssetvalue{/mytable/0}{gray}
\pgfkeyssetvalue{/mytable/1}{black}
\pgfkeyssetvalue{/mytable/3}{red}
\pgfkeyssetvalue{/mytable/2}{blue}

\def\imagetop#1{\vtop{\null\hbox{#1}}}

\begin{document}

\begin{tabular}{c@{\hskip 0.05cm}c@{\hskip 0.05cm}c@{\hskip 0.3cm}c}
$B$ & $A$ & $B^{T}$ & $\hat{C}$ \\
\imagetop{
\begin{tikzpicture}
\node {
\pgfplotstabletypeset[
postproc cell content/.style={/pgfplots/table/@cell content={%
\cellcolor{\pgfkeysvalueof{/mytable/#1}}}},
};
\end{tikzpicture}
} &
\imagetop{
\begin{tikzpicture}
\node {
\pgfplotstabletypeset[
postproc cell content/.style={/pgfplots/table/@cell content={%
\cellcolor{\pgfkeysvalueof{/mytable/#1}}}},
};
\end{tikzpicture}
} &
\imagetop{
\begin{tikzpicture}
\node {
\pgfplotstabletypeset[
postproc cell content/.style={/pgfplots/table/@cell content={%
\cellcolor{\pgfkeysvalueof{/mytable/#1}}}},
};
\end{tikzpicture}
} &
\imagetop{
\begin{tikzpicture}
\node {
\pgfplotstabletypeset[
postproc cell content/.style={/pgfplots/table/@cell content={%
\cellcolor{\pgfkeysvalueof{/mytable/#1}}}},
};
\end{tikzpicture}
} \\
$p(w_{1} | z_{1})$ & $p(z_{1}, z_{2} | m)$ & $p(w_{2} | z_{2})$ & $p(w_{1}, w_{2} | m)$
\end{tabular}

\end{document}


Final version looks like this

Here is one way that you can generalize for your own needs

\documentclass{standalone}
\usepackage{colortbl,pgfplotstable}
1     0     1
0     3     0
0     1     1
3     0     0
0     0     3
}\myB

\pgfkeys{/mytable/.is family}
\pgfkeyssetvalue{/mytable/0}{gray}
\pgfkeyssetvalue{/mytable/1}{black}
\pgfkeyssetvalue{/mytable/3}{red}
\pgfkeyssetvalue{/mytable/2}{blue}

\begin{document}
\pgfplotstabletypeset[
postproc cell content/.style={/pgfplots/table/@cell content={%
\cellcolor{\pgfkeysvalueof{/mytable/#1}}}},

• can I nest these \pgfplotstabletypeset in atabular to get labels above and below. – SPV Jun 12 '16 at 21:12
• @SivaPrasadVarma Not without some hacking but you can put them inside \nodes in a tikzpicture and label the nodes. – percusse Jun 12 '16 at 21:13