[Tex/LaTex] How empty cells in tikz table (matrix)

arraystablestikz-matrixtikz-pgf

I am trying to recreate below table in tikz (I need because I could use tikz in my jupyter notebook to render embedded output but not yet latex)

enter image description here

When I try via tikz I only get this far..

enter image description here

My MWE:

\begin{tikzpicture}

\tikzset{square matrix/.style={
    matrix of nodes,
    column sep=-\pgflinewidth, row sep=-\pgflinewidth,
    nodes={draw,
      minimum height=#1,
      anchor=center,
      text width=#1,
      align=center,
      inner sep=0pt
    },
  },
  square matrix/.default=1.2cm
}

\matrix[square matrix]
{
1 & &  & & & & \\ 
2 & x & & & & & \\ 
3 & x & x & & & & \\ 
4 & x & x & x & & & \\ 
5 & x & x & x & x & & \\ 
6 & x & x & x & x & x & \\ 
};

\end{tikzpicture}

Best Answer

Sebastiano provided the solution for missing cells and how to fill each one, but it's also possible to define styles for complet rows and columns. And with some extra works draw the top left cell.

\documentclass[tikz,border=2mm]{standalone} 
\usetikzlibrary{matrix, positioning}

\begin{document}
\begin{tikzpicture}

\tikzset{%
square matrix/.style={
    matrix of nodes,
    column sep=-\pgflinewidth, 
    row sep=-\pgflinewidth,
    nodes in empty cells,
    nodes={draw,
      minimum size=#1,
      anchor=center,
      align=center,
      inner sep=0pt
    },
    column 1/.style={nodes={fill=cyan!30}},
    row 1/.style={nodes={fill=cyan!30}},
  },
  square matrix/.default=1.2cm
}

\matrix[square matrix] (A)
{
  & 1 & 2 & 3 & 4 & 5 & 6\\ 
1 & & & & & & \\ 
2 & x & & & & & \\ 
3 & x & x & & & &   \\ 
4 & x & x & x & & & \\ 
5 & x & x & x & x & & \\ 
6 & x & x & x & x & x & \\ 
};

\draw (A-1-1.north west)--(A-1-1.south east);
\node[below left=2mm and 2mm of A-1-1.north east] {$i$};
\node[above right=2mm and 2mm of A-1-1.south west] {$j$};
\end{tikzpicture}
\end{document}

enter image description here

Update:

To obtain some special color distribution like in your link

enter image description here

you can mix some automating filling for regular rows and columns and some manual filling on background:

\documentclass[tikz,border=2mm]{standalone} 
\usetikzlibrary{matrix, positioning, backgrounds}

\tikzset{%
square matrix/.style={
    matrix of math nodes,
    column sep=-\pgflinewidth, 
    row sep=-\pgflinewidth,
    nodes in empty cells,
    nodes={draw,
      minimum width=#1,
      minimum height=.5*#1,
      anchor=center,
%      align=center,
      inner sep=0pt
    },
    column 1/.style={nodes={fill=gray!30}},
    row 1/.style={nodes={fill=gray!30}},
  },
  square matrix/.default=1.2cm
}

\begin{document}
\begin{tikzpicture}
\matrix (A) [square matrix]
{
j\backslash i  & x_1 & x_2 & x_3 & x_4 & x_5 & x_6\\    
y_1 & R_{11} & R_{12} & R_{13} & R_{14} & R_{15} & R_{16} \\ 
y_2 & R_{21} & R_{22} & R_{23} & R_{24} & R_{25} & R_{26} \\ 
y_3 & R_{31} & R_{32} & R_{33} & R_{34} & R_{35} & R_{36} \\ 
y_4 & R_{41} & R_{42} & R_{43} & R_{44} & R_{45} & R_{46} \\ 
y_5 & R_{51} & R_{52} & R_{53} & R_{54} & R_{55} & R_{56} \\ 
y_6 & R_{61} & R_{62} & R_{63} & R_{64} & R_{65} & R_{66} \\ 
};
\begin{scope}[on background layer]
\fill[cyan!15] (A-3-2.north west)-|(A-4-3.north west)-|(A-5-4.north west)-|(A-6-5.north west)-|(A-7-6.north west)-|(A-7-6.south east)-|cycle;
\fill[red!15] (A-2-3.north west)|-(A-3-4.north west)
|-(A-4-5.north west)|-(A-5-6.north west)|-(A-6-7.north west)|-(A-6-7.south east)
|-cycle;
\foreach \i in {2,...,7}
    \fill[gray!30] (A-\i-\i.north west) rectangle (A-\i-\i.south east);
\end{scope}
\end{tikzpicture}
\end{document}

enter image description here

2nd Update:

You could construct an automatic solution:

\documentclass[tikz,border=2mm]{standalone} 
\usetikzlibrary{matrix, positioning, backgrounds}

\tikzset{%
myfill/.code={%
    \ifnum\pgfmatrixcurrentcolumn<\pgfmatrixcurrentrow
        \pgfkeysalso{fill=cyan!30}
    \else
        \ifnum\pgfmatrixcurrentcolumn>\pgfmatrixcurrentrow
            \pgfkeysalso{fill=red!30}
        \else
            \pgfkeysalso{fill=gray!20}
        \fi
    \fi
    },
square matrix/.style={
    matrix of math nodes,
    column sep=-\pgflinewidth, 
    row sep=-\pgflinewidth,
    nodes in empty cells,
    nodes={draw,
        myfill,
      minimum width=#1,
      minimum height=.5*#1,
      anchor=center,
      inner sep=0pt,
      node contents={
        R_{\the\numexpr\pgfmatrixcurrentcolumn-1\relax%
        \the\numexpr\pgfmatrixcurrentrow-1\relax%
      }}
    },
    column 1/.style={
        nodes={fill=gray!30,
        node contents={y_\the\numexpr\pgfmatrixcurrentrow-1\relax}
        }},
    row 1/.style={
        nodes={fill=gray!30,
        node contents={x_\the\numexpr\pgfmatrixcurrentcolumn-1\relax}
        }},
     row 1 column 1/.style={
        nodes={fill=gray!30,
        node contents={j\backslash i}
        }},
  },
  square matrix/.default=1.2cm
}

\begin{document}
\begin{tikzpicture}
\matrix (A) [square matrix]
{
&&&&&&\\
&&&&&&\\
&&&&&&\\
&&&&&&\\
&&&&&&\\
&&&&&&\\
&&&&&&\\
};
\end{tikzpicture}
\end{document}

enter image description here