11

How can I draw a grid similar to the one below in the question answer?

Fill a grid with TikZ

But I can select different fill colors for the separate cells?

I need to be able to color (fill) in the separate nodes in the grid. I tried using this, but for some reasons there's some weird padding on the fill

\documentclass{article} 
\usepackage{algorithmic}
\usepackage{amsfonts}
\usepackage{amsmath}
\usepackage{tikz}
\title{Title} 
\author{User} 
\begin{document} 
\begin{tikzpicture}
    \draw[step=0.5cm,color=black] (-1,-1) grid (1,1);
    \node[fill=green] at (-0.75,+0.75) {};
    \node at (-0.25,+0.75) {};
    \node at (+0.25,+0.75) {};
    \node at (+0.75,+0.75) {};
    \node at (-0.75,+0.25) {};
\end{tikzpicture}
\end{document}
4
  • 1
    Welcome to TeX.SX! Please make your code compilable (if possible), or at least complete it with \documentclass{...}, the required \usepackage's, \begin{document}, and \end{document}. That may seem tedious to you, but think of the extra work it represents for TeX.SX users willing to give you a hand. Help them help you: remove that one hurdle between you and a solution to your problem.
    – erik
    Commented Oct 21, 2015 at 15:27
  • Sorry about that, I included the necessary preambles and code now
    – q.Then
    Commented Oct 21, 2015 at 15:32
  • Perhaps this helps? Commented Oct 22, 2015 at 6:55
  • possible duplicate tex.stackexchange.com/questions/271577/…
    – CroCo
    Commented Oct 22, 2015 at 7:16

4 Answers 4

14
\documentclass[tikz,border=2mm]{standalone}

\begin{document}

\begin{tikzpicture}[every node/.style={minimum size=.5cm-\pgflinewidth, outer sep=0pt}]
    \draw[step=0.5cm,color=black] (-1,-1) grid (1,1);
    \node[fill=green] at (-0.75,+0.75) {};
    \node[fill=red] at (-0.25,+0.75) {};
    \node[fill=orange] at (+0.25,+0.75) {};
    \node[fill=yellow] at (+0.75,+0.75) {};
    \node[fill=purple!70] at (-0.75,+0.25) {};
\end{tikzpicture}

\end{document}

enter image description here

Update:

Command \draw[step=0.5cm,color=black] (-1,-1) grid (1,1); draws the grid. step fixes element size and coordinates before and after grid define grid dimensions. If you want a grid with three rows and eight columns with 0.5cm squares and bottom left corner at (0,0), use

\draw[step=0.5cm,color=black] (0,0) grid (4,1.5);

as in

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

\begin{document}

\begin{tikzpicture}[every node/.style={minimum size=.5cm-\pgflinewidth, outer sep=0pt}]
    \draw[step=0.5cm,color=black] (0,0) grid (6,1.5);
    \node[fill=green] at (0.25,+1.25) {};
    \node[fill=red] at (0.25,+0.25) {};
    \node[fill=orange] at (+3.25,+0.75) {};
    \node[fill=orange] at (+2.75,+0.75) {};
    \node[fill=yellow] at (+5.75,+1.25) {};
    \node[fill=purple!70] at (5.75,+0.25) {};
\end{tikzpicture}

\end{document}

enter image description here

4
  • Hey, this is exactly what I'm looking for, but how can I change the dimensions of the grid? (i.e. to 3 by n)
    – q.Then
    Commented Oct 21, 2015 at 16:26
  • @PhilipTsang Answer updated.
    – Ignasi
    Commented Oct 22, 2015 at 6:48
  • Is it possible to write text inside the colored rectangles?
    – alper
    Commented May 18, 2023 at 8:04
  • 1
    @alper Yes, just write whatever you want into {} of desired node. But remember that nodes adapt their size to the contents, then it's possible that you break this structure if the text is larger than available space.
    – Ignasi
    Commented May 18, 2023 at 10:25
9

You can use a matrix :

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

\begin{document}
  \begin{tikzpicture}[fill=orange]
    \matrix[matrix of nodes, nodes={draw,minimum size=1cm}, nodes in empty cells,column sep=-\pgflinewidth,row sep=-\pgflinewidth](M){
               &          &   |[fill]|   &   |[fill]|   &   |[fill]|   &   |[fill]|   &          &          \\
               & |[fill]| &              &              &              &              & |[fill]| &          \\
      |[fill]| &          &   |[fill]|   &              &   |[fill]|   &              &          & |[fill]| \\
      |[fill]| &          &   |[fill]|   &              &   |[fill]|   &              &          & |[fill]| \\
      |[fill]| &          &              &              &              & |[fill=red]| &          & |[fill]| \\
      |[fill]| &          & |[fill=red]| & |[fill=red]| & |[fill=red]| &              &          & |[fill]| \\
               & |[fill]| &              &              &              &              & |[fill]| &          \\
               &          & |[fill]|     & |[fill]|     & |[fill]|     & |[fill]|     &          &          \\
    };
  \end{tikzpicture}
\end{document}

enter image description here

5

How about this:

\documentclass[tikz,border=5pt]{standalone}

\usepackage{tikz}

\tikzset{
  pics/square/.default={1},
  pics/square/.style = {
    code = {
    \draw[pic actions] (0,0) rectangle (#1,#1);
    }
  }
}    

\begin{document}

\begin{tikzpicture}
   \foreach \x in {1,2,...,10}  {
     \foreach \y in {1,2,...,5} {
       \pic[fill=blue!50] at (\x,\y) {square};
     }
   }
   \pic[fill=yellow] at (5,3) {square};
   \pic[draw=none,fill=yellow] at (1.25,2.25) {square=.5};
\end{tikzpicture}

\end{document}

enter image description here

4

You can use \foreach combined with /.try key handler to apply style to some cells :

\documentclass[tikz,border=7mm]{standalone}
\begin{document}
  \begin{tikzpicture}[fill=orange,
    cell27/.style={fill}, cell37/.style={fill}, cell47/.style={fill}, cell57/.style={fill},
    cell16/.style={fill}, cell66/.style={fill},
    cell05/.style={fill}, cell25/.style={fill}, cell45/.style={fill}, cell75/.style={fill},
    cell04/.style={fill}, cell24/.style={fill}, cell44/.style={fill}, cell74/.style={fill},
    cell03/.style={fill}, cell53/.style={fill=red}, cell73/.style={fill},
    cell02/.style={fill}, cell22/.style={fill=red}, cell32/.style={fill=red}, cell42/.style={fill=red}, cell72/.style={fill},
    cell11/.style={fill}, cell61/.style={fill},
    cell20/.style={fill}, cell30/.style={fill}, cell40/.style={fill}, cell50/.style={fill},
  ]
    \foreach \i in {0,...,7}
      \foreach \j in {0,...,7}
        \path[cell\i\j/.try] (\i,\j) rectangle +(1,1);
    \draw grid (8,8);
  \end{tikzpicture}
\end{document}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .