\begin{tikzpicture}[font=\footnotesize] \begin{scope} \def\dw{0.48} \node[anchor=east] at (-0.05,0.22) {$v$}; \foreach \v in {1,...,4} {\node at ({(\v-0.5)*\dw},0.22) {$\v$};} \foreach \r/\name/\row in {1/{\pi_1}/{1,2,3,4}, 2/{\pi_2}/{2,3,4,1}, 3/{\pi_3}/{3,4,1,2}} { \node[anchor=east] at (-0.05,{-(\r-0.5)*\dw}) {$\name$}; \foreach \x [count=\v] in \row {\node at ({(\v-0.5)*\dw},{-(\r-0.5)*\dw}) {$\x$};} } \foreach \v/\r in {1/1, 2/2, 3/2, 4/3} {\draw[very thick, fill=yellow!40, fill opacity=0.6] ({(\v-1)*\dw+0.03},{-(\r-1)*\dw-0.03}) rectangle +({\dw-0.06},{-\dw+0.06});} \foreach \r/\row in {1/{1,2,3,4}, 2/{2,3,4,1}, 3/{3,4,1,2}} {\foreach \x [count=\v] in \row {\node at ({(\v-0.5)*\dw},{-(\r-0.5)*\dw}) {$\x$};}} \draw[step=\dw, thin] (0,0) grid (4*\dw,-3*\dw); \draw[thick] (0,0) rectangle (4*\dw,-3*\dw); \node[anchor=east] at (-0.05,{-3.95*\dw}) {$\pi_0$}; \foreach \x [count=\v] in {1,3,4,2} { \draw[very thick, fill=yellow!40] ({(\v-1)*\dw+0.03},{-3.45*\dw-0.03}) rectangle +({\dw-0.06},{-\dw+0.06}); \node at ({(\v-0.5)*\dw},{-3.95*\dw}) {$\x$}; } \end{scope} \end{tikzpicture}