Coloring a node after its creation

In your question you said that you want to fill random nodes. If you want a realy random choice you can do the following random darkstyle style :

\documentclass[tikz, border=7pt]{standalone}
\usetikzlibrary{calc,trees,positioning,arrows,chains,shapes.geometric,%
decorations.pathreplacing,decorations.pathmorphing,shapes,%
matrix,shapes.symbols}
\tikzset{
  % ----- the static part of the style
  darkstyle/.style={
    circle,minimum size=20,draw
  },
  % ----- the random part of the style
  darkstyle/.append code = {
    \pgfmathsetmacro\randcolor{{"red!50","blue!35"}[int(2*random())]}
    \pgfkeysalso{fill=\randcolor}
  }
}
\begin{document}
\begin{tikzpicture}

  \foreach \x in {0,...,2}
    \foreach \y in {0,...,2}
       {\pgfmathtruncatemacro{\label}{\x - 5 *  \y +21}
       \node [darkstyle]  (\x\y) at (1.5*\x,1.5*\y) {};}

  \foreach \x in {0,...,2}
    \foreach \y [count=\yi] in {0,...,1}
      \draw (\x\y)--(\x\yi)  ;

\draw [decorate,decoration={brace,amplitude=7pt,raise=2pt,aspect=0.5}] (20.south) -- (00.south);

  \foreach \x in {0,...,2}
    \foreach \y in {0,...,3}
       {\pgfmathtruncatemacro{\label}{\x - 5 *  \y +21}
       \node [darkstyle]  (\x\y) at (10+1.5*\x,1.5*\y) {};}

  \foreach \x in {0,...,2}
    \foreach \y [count=\yi] in {0,...,2}
      \draw (\x\y)--(\x\yi)  ;

\draw [decorate,decoration={brace,amplitude=7pt,raise=2pt,aspect=0.5}] (20.south) -- (00.south);

  \foreach \x in {0,...,5}
    \foreach \y in {0,...,4}
       {\pgfmathtruncatemacro{\label}{\x - 5 *  \y +21}
       \node [darkstyle]  (\x\y) at (3+1.5*\x,-10+1.5*\y) {};}

  \foreach \x in {0,...,5}
    \foreach \y [count=\yi] in {0,...,3}
      \draw (\x\y)--(\x\yi)  ;

\end{tikzpicture}
\end{document}

enter image description here

EDIT : And if you don't realy want a random choice, you can prescribe some nodes to be with different color like this :

\documentclass[tikz, border=7pt]{standalone}
\usetikzlibrary{calc,trees,positioning,arrows,chains,shapes.geometric,%
decorations.pathreplacing,decorations.pathmorphing,shapes,%
matrix,shapes.symbols}
\tikzset{
  % ----- the static part of the style
  darkstyle/.style={
    circle,minimum size=20,draw, fill = blue!35
  },
  % ----- the prescribed part pf the style
  mycolor/.style = {fill=red!35},
  mycolorA12/.style = {mycolor},
  mycolorA20/.style = {mycolor},
  mycolorB11/.style = {mycolor},
  mycolorC54/.style = {mycolor},
}
\begin{document}
\begin{tikzpicture}

  \foreach \x in {0,...,2}
    \foreach \y in {0,...,2}
       {\pgfmathtruncatemacro{\label}{\x - 5 *  \y +21}
       \node [darkstyle,mycolorA\x\y/.try]  (\x\y) at (1.5*\x,1.5*\y) {};}

  \foreach \x in {0,...,2}
    \foreach \y [count=\yi] in {0,...,1}
      \draw (\x\y)--(\x\yi)  ;

\draw [decorate,decoration={brace,amplitude=7pt,raise=2pt,aspect=0.5}] (20.south) -- (00.south);

  \foreach \x in {0,...,2}
    \foreach \y in {0,...,3}
       {\pgfmathtruncatemacro{\label}{\x - 5 *  \y +21}
       \node [darkstyle,mycolorB\x\y/.try]  (\x\y) at (10+1.5*\x,1.5*\y) {};}

  \foreach \x in {0,...,2}
    \foreach \y [count=\yi] in {0,...,2}
      \draw (\x\y)--(\x\yi)  ;

\draw [decorate,decoration={brace,amplitude=7pt,raise=2pt,aspect=0.5}] (20.south) -- (00.south);

  \foreach \x in {0,...,5}
    \foreach \y in {0,...,4}
       {\pgfmathtruncatemacro{\label}{\x - 5 *  \y +21}
       \node [darkstyle,mycolorC\x\y/.try]  (\x\y) at (3+1.5*\x,-10+1.5*\y) {};}

  \foreach \x in {0,...,5}
    \foreach \y [count=\yi] in {0,...,3}
      \draw (\x\y)--(\x\yi)  ;

\end{tikzpicture}
\end{document}

enter image description here


enter image description here

\documentclass{minimal}
\usepackage{tikz}
\usetikzlibrary{calc,trees,positioning,arrows,chains,shapes.geometric,%
decorations.pathreplacing,decorations.pathmorphing,shapes,%
matrix,shapes.symbols}

\begin{document}
\begin{tikzpicture}[
darkstyle/.style={circle,draw,fill=gray!20,minimum size=20},
bluestyle/.style={circle,draw,fill=blue!20,minimum size=20},
]


  \foreach \x in {0,...,2}
    \foreach \y in {0,...,2} 
       {\pgfmathtruncatemacro{\label}{\x - 5 *  \y +21}
\ifnum\x=\ifnum\y=2 0\else-1\fi
       \node [bluestyle]  (\x\y) at (1.5*\x,1.5*\y) {};
\else
       \node [darkstyle]  (\x\y) at (1.5*\x,1.5*\y) {};
\fi
       } 

  \foreach \x in {0,...,2}
    \foreach \y [count=\yi] in {0,...,1}  
      \draw (\x\y)--(\x\yi)  ;


\draw [decorate,decoration={brace,amplitude=7pt,raise=2pt,aspect=0.5}] (20.south) -- (00.south); 

  \foreach \x in {0,...,2}
    \foreach \y in {0,...,3} 
       {\pgfmathtruncatemacro{\label}{\x - 5 *  \y +21}
       \node [darkstyle]  (\x\y) at (10+1.5*\x,1.5*\y) {};} 

  \foreach \x in {0,...,2}
    \foreach \y [count=\yi] in {0,...,2}  
      \draw (\x\y)--(\x\yi)  ;

\draw [decorate,decoration={brace,amplitude=7pt,raise=2pt,aspect=0.5}] (20.south) -- (00.south); 



  \foreach \x in {0,...,5}
    \foreach \y in {0,...,4} 
       {\pgfmathtruncatemacro{\label}{\x - 5 *  \y +21}
\ifnum\x=\ifnum\y=0 5\else-1\fi
       \node [bluestyle]  (\x\y) at (3+1.5*\x,-10+1.5*\y) {};
\else
       \node [darkstyle]  (\x\y) at (3+1.5*\x,-10+1.5*\y) {};
\fi

} 

  \foreach \x in {0,...,5}
    \foreach \y [count=\yi] in {0,...,3}  
      \draw (\x\y)--(\x\yi)  ;



\end{tikzpicture}
\end{document}

Tags:

Nodes

Tikz Pgf