使用 TikZ 绘制多色网格

使用 TikZ 绘制多色网格

我怎样才能在问题答案中绘制类似下面的网格?

使用 TikZ 填充网格

但是我可以为不同的单元格选择不同的填充颜色吗?

我需要能够为网格中的单独节点着色(填充)。我尝试使用这个,但出于某些原因,填充上有一些奇怪的填充

\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}

答案1

\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}

在此处输入图片描述

更新:

命令\draw[step=0.5cm,color=black] (-1,-1) grid (1,1);绘制网格。在定义网格尺寸step之前和之后修复元素大小和坐标grid。如果您想要一个三行八列的网格,每个网格的方格大小为 0.5 厘米,左下角位于(0,0),请使用

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

如同

\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}

在此处输入图片描述

答案2

您可以使用矩阵:

\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}

在此处输入图片描述

答案3

这个怎么样:

\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}

在此处输入图片描述

答案4

您可以\foreach结合使用/.try键处理程序将样式应用于某些单元格:

\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}

在此处输入图片描述

相关内容