0

I am new to Tikz and I am trying to draw a very specific graph. It contains several features that with my limited knowledge I am unable to make. It is a grid type of drawing with some text specifications, as the attached picture, enter image description here. This is only the first step of a series of drawings I would like to make, to add animation. Checking on other posts it seems that it can be done with tikz or pgfplot, but I'm not sure which one is better to later add some animation, like grid filling.

The code that I have so far is the following one, trying to draw a grid with tikz as the first step, but it looks quite tedious.

 \documentclass[24point]{article}
    \usepackage{tikz}
\begin{document}

\begin{tikzpicture}[every node/.style={minimum size=.5cm-\pgflinewidth, outer sep=0pt}]
 \draw[thin, step=0.5cm,color=black] (0,3) grid (6,0.5);
\node[] at (+0.25,+1.25) {0};
\node[] at (+0.75,+1.25) {0};
\node[] at (+1.25,+1.25) {0};
\node[] at (+1.75,+1.25) {0};
\node[fill=lightgray] at (+2.25,+1.25) {1};
\node[fill=lightgray] at (+2.75,+1.25) {1};
\node[fill=lightgray] at (+3.25,+1.25) {1};
\node[fill=lightgray] at (+3.75,+1.25) {1};
\node[fill=gray] at (+4.25,+1.25) {1};
\node[] at (+4.75,+1.25) {0};
\node[] at (+5.25,+1.25) {0};
\node[] at (+5.75,+1.25) {0};
\node[] at (+0.25,+1.75) {0};
\node[] at (+0.75,+1.75) {0};
\node[] at (+1.25,+1.75) {0};
\node[] at (+1.75,+1.75) {0};
\node[fill=lightgray] at (+2.25,+1.75) {1};
\node[] at (+2.75,+1.75) {0};
\node[] at (+3.25,+1.75) {0};
\node[] at (+3.75,+1.75) {0};
\node[] at (+4.25,+1.75) {0};
\node[] at (+4.75,+1.75) {0};
\node[] at (+5.25,+1.75) {0};
\node[] at (+5.75,+1.75) {0};
\node[] at (+0.25,+2.75) {1};
\node[] at (+0.75,+2.75) {2};

\node[] at (+1.25,+2.75) {3}; \node[] at (+1.75,+2.75) {4}; \node[] at (+2.25,+2.75) {5}; \node[] at (+2.75,+2.75) {6}; \node[] at (+3.25,+2.75) {7}; \node[] at (+3.75,+2.75) {8}; \node[] at (+4.25,+2.75) {9}; \node[] at (+4.75,+2.75) {10}; \node[] at (+5.25,+2.75) {11}; \node[] at (+5.75,+2.75) {12}; \end{tikzpicture}

\end{document}

I will appreciate any directions to follow from there.

EDIT The following code is my version with matrix tikz. Still, I need to add more space between lines, the double line between rows, and the text vertical text on the side. I took part of the code from The simplest way to fill a matrix of nodes?

    \documentclass[24point]{article}
            \usepackage{tikz}
        \begin{document}

 \tikzstyle{matrici}=[
       matrix of math nodes, 
      nodes in empty cells,
      column sep=-\pgflinewidth, % to avoid double borders in contiguous cells
   nodes={
    draw, 
   align=center, 
  inner sep=0pt, 
    text width=0.5cm, 
    minimum height=0.5cm
}
]

     \begin{tikzpicture} 


    \matrix[matrici, 
     row 1/.style={nodes={draw=none, fill=none}},

row 2/.style={nodes={draw=none, fill=none}}, row 3/.style={nodes={draw=none, fill=none}}, row 2 column 1/.style={nodes={font=\small}}, row 2 column 2/.style={nodes={font=\small}}, row 2 column 3/.style={nodes={font=\small}}, row 2 column 4/.style={nodes={font=\small}}, row 2 column 5/.style={nodes={font=\small}}, row 2 column 6/.style={nodes={font=\small}}, row 2 column 7/.style={nodes={font=\small}}, row 2 column 8/.style={nodes={font=\small}}, row 2 column 9/.style={nodes={font=\small}}, row 2 column 10/.style={nodes={font=\small}}, row 2 column 11/.style={nodes={font=\small}}, row 2 column 12/.style={nodes={font=\small}}, row 3 column 1/.style={nodes={font=\small}}, row 3 column 2/.style={nodes={font=\small}}, row 3 column 3/.style={nodes={font=\small}}, row 3 column 4/.style={nodes={font=\small}}, row 3 column 5/.style={nodes={font=\small}}, row 3 column 6/.style={nodes={font=\small}}, row 3 column 7/.style={nodes={font=\small}}, row 3 column 8/.style={nodes={font=\small}}, row 3 column 9/.style={nodes={font=\small}}, row 3 column 10/.style={nodes={font=\small}}, row 3 column 11/.style={nodes={font=\small}}, row 3 column 12/.style={nodes={font=\small}}, row 2 column 5/.style={nodes={fill=gray!20, font=\small}}, row 3 column 5/.style={nodes={fill=gray!30, font=\small}}, row 3 column 6/.style={nodes={fill=gray!30, font=\small}}, row 3 column 7/.style={nodes={fill=gray!30, font=\small}}, row 3 column 8/.style={nodes={fill=gray!30, font=\small}}, row 3 column 9/.style={nodes={fill=gray!20, font=\small}}, ]
(X)
{1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 & 10 & 11 & 12 \ 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \ 0 & 0 & 0 & 0 & 1 & 1 & 1 & 1 & 1 & 0 & 0 & 0 \ &&&&&&&&&&&\ };

\draw[->, thick] (-0.80,-1.6) -- (1,-1.6);

\draw [densely dashed] (-1,-1.1) -- (-1,-1.7);

\draw [densely dashed] (1.25,-1.1) -- (1.25,-1.7); \end{tikzpicture}

    \end{tikzpicture}   
\end{document}

  • Such diagrams can be generated with the matrix library of TikZ. –  Nov 10 '20 at 06:12
  • @noncommercial Thanks, I will check it out. Do you know how it is possible to add the line number on the top, and the square text boxed on the side? – Ana Batista Nov 10 '20 at 06:21
  • @noncommercial So, I checked it the matrix package it is less tedious than tikz node, but I'm not sure If it is possible to fill in the nodes an animate. – Ana Batista Nov 10 '20 at 07:10
  • I don't think I understood what type of animation you want to create, can you clarify that part? Also, what the matrix is supposed to represent, etc. – Alenanno Nov 10 '20 at 16:26
  • @Alenanno thanks for your interest. The animation I will be (as a second step) filling in and out several continuous blocks (to show the algorithm on a beamer presentation). The matrix or the graph itself represents an allocation algorithm in which entities of different lengths have to be allocated contiguously. Similar to a binary matrix [0,1]. – Ana Batista Nov 10 '20 at 16:44

0 Answers0