I'm wondering how could I draw a matrix like that:

enter image description here

I know how to draw a simple matrix:



\usetikzlibrary{matrix, calc}



\matrix (input) [matrix of nodes,
                nodes={rectangle, draw=white, minimum size=.8cm}] at (0,0)
|[fill=black]| & |[fill=black!20]|  & |[fill=black!50]|         \\
|[fill=black!50]| & |[fill=black!50]|  & |[fill=black!20]|      \\
|[fill=black!20]| & |[fill=black!20]|  & |[fill=black]|         \\
\node [draw,below=8pt] at (input.south) {Sample};



enter image description here

But not how to change it to achieve what I want.

Thank you.

  • Normally folks here like that you show what you have already tried - nice. -but you need to add a few lines to make your code compile-able: \documentclass{... \begin{document}... and do not write Thank you. Dec 8 '14 at 19:09

One answer using the rotation symetry of the figure :

\begin{tikzpicture}[thick, scale=.35]
  \draw[densely dotted, gray, shift={(-4.5,-4.5)}] (0,0) grid +(9,9);
  \draw (-4.5,-4.5) rectangle (4.5,4.5);
  \node {x};
  \foreach[count=\i] \a in {0,1,2,3} {
      \draw (4.5,3.5) -| ++(-1,-1) -| ++(-1,-1) -| ++(-1,-1) -- ++(-6,0);
      \path let \n1={int(2*\i-1)}, \n2={int(2*\i)} in (3,1) node{\n1} (2,3) node{\n2};

enter image description here

  • thank you for your answer. Could you explain me that line \draw (4.5,3.5) -| ++(-1,-1) -| ++(-1,-1) -| ++(-1,-1) -- ++(-6,0);? I'm trying to adapt it to reduce the matrix to a 5 x 5 size. I tried \draw (2.5,1.5) -| ++(-1,-1) -- ++(-4,0); but I think I misunderstood it.
    – pceccon
    Dec 9 '14 at 12:47
  • 1
    @pceccon Your understanding is good ! But you have also to replace all 4.5 by 2.5 and 9 by 5. The only thing that is not clear for me (because I don't know what you want exactly to obtain) is how to modify the number coordinates. Probably \path let \n1={int(2*\i-1)}, \n2={int(2*\i)} in (2,1) node{\n1} (1,2) node{\n2}; will be ok.
    – Kpym
    Dec 9 '14 at 17:24
  • Hi, @Kpym. After a considerable time, I was asked to paint each of those regions with a specific color. Is it possible to do this adapting your code? Thank you very much.
    – pceccon
    Apr 1 '15 at 20:26
  • 1
    @pceccon You can replace the first line (the grid) by something like \begin{scope}[shift={(-4.5,-4.5)}] \fill[red] (1,2) rectangle +(1,1); \fill[yellow] (4,5) rectangle +(1,1); \draw[densely dotted, gray] (0,0) grid +(9,9); \end{scope}. In general it is better to ask this kind of questions in a question, not in a comment.
    – Kpym
    Apr 2 '15 at 7:28

Just playing around (without tikz), building up stacked layers of \Sv (solid vertical), \Dv (dashed vertical), \Sh (solid horizontal), \Dh (dashed horizontal). Optional argument on vertical lines provides text following line

Quirks: \Sd (solid dot) needed in upper right corner of graph, due to the way I construct things. and \intersect may need to be defined to {.} if the \dashfill leaves the intersections blank.


\def\intersect{}% might need it as {.}
\def\dashfill{\cleaders\hbox to 1.43pt{.}\hfill}
\newcommand\dashline[1]{\textcolor{black!50}{\hbox to #1{\dashfill\hfil}}}







enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.