# Drawing grid with random color in TikZ

I'm trying to draw a 10x10 grid in TikZ like so:

\documentclass{minimal}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}[scale=1]
\foreach \y in {0.05,0.15,...,0.95}
\foreach \x in {0.05,0.15,...,0.95}
\pgfmathparse{0.9*rnd+0.3}
\definecolor{MyColor}{rgb}{\pgfmathresult,\pgfmathresult,\pgfmathresult}
\node[fill=MyColor,minimum size=0.1] at (\x,\y) {};
\draw[step=.1,help lines] (0,0) grid (1,1);
\draw[->] (0,1) -- (1.2,1);
\draw[->] (0,1) -- (0,-0.2);
\end{tikzpicture}
\end{document}


Which is already quite close to what I had in mind, but the nodes don't line up properly. What am I doing wrong? Here is an image of my output:

.

Also, since I'm new to TikZ, feel free to comment on the code, in terms of readability, coding practices and so on.

-
Nicely done! It will align if you just add the missing options minimum size=1mm,inner sep=0,outer sep=0 instead of minimum size=0.1. Also it is useful to write up grid (1cm,1cm) so you can keep track of the units :) –  percusse Dec 6 '11 at 10:01
@percusse Thanks, that works. However, if I want to change scale=1 to scale=2, I also have to adjust minimum size. Is there a way to make it independent? As for the units in grid, why is that preferable? Like a math plot, it don't think of it in cm, so it would be counterintuitive to supply it, no? –  Psirus Dec 6 '11 at 10:09
You can add the option transform shape to the tikzpicture environment. That will enlarge everything together. Regarding the cm thing, it is just a matter of taste probably. Nevermind that :) –  percusse Dec 6 '11 at 10:12

You should not always use the node feature. Sometimes you need the coordinates of the node, if you don't it can often pay to do it "manually" (less code).

I have here compiled 2 methods, one where yours is corrected, and the other without the use of nodes. I prefer the one without nodes as it is more drawing clean, but everyone can prefer whatever style they want!

### Without node

\begin{tikzpicture}[scale=1]
\foreach \y in {0.0,0.1,...,0.9} {
\foreach \x in {0.0,0.1,...,0.9} {
\pgfmathparse{0.9*rnd+0.3}
\definecolor{MyColor}{rgb}{\pgfmathresult,\pgfmathresult,\pgfmathresult}
\path[fill=MyColor] (\x,\y) rectangle ++(0.1,0.1);
}
}
\draw[step=.1,help lines] (0,0) grid (1,1);
\draw[->] (0,1) -- (1.2,1);
\draw[->] (0,1) -- (0,-0.2);
\end{tikzpicture}


As you can see you just draw a rectangle and fill it with the color of your choice.

### Fixed code

What you need to ensure is correct placement, and correct size. A node usually has some space associated with the filling of the text. This space is to ensure not too tight a drawing compared to the text, compare the following two tikz drawings:

\tikz \node[draw,inner sep=0pt,outer sep=0pt] {H};
\tikz \node[draw] {H};


Therefore i have added that you do not want any outer sep, but kept an inner sep which has the size of your node. Next is your ability to place the node with respect to its size. Here anchor is your friend, it basically tells if the node should be placed at the coordinate anchoring the node at the coordinate within the node. Try for instance and change anchor=center to anchor=east. With anchor=center it takes the center coordinate in the node and place at the coordinate you specify. With anchor=east it takes the east point of the node and places at the coordinate. It is strictly not needed in the below example, but gives a good idea what it does (and it is VERY useful)

\begin{tikzpicture}[scale=1]
\foreach \y in {0.1,0.2,...,.9} {
\foreach \x in {0.1,0.2,...,.9} {
\pgfmathparse{0.9*rnd+0.3}
\definecolor{MyColor}{rgb}{\pgfmathresult,\pgfmathresult,\pgfmathresult}
\node[fill=MyColor,inner sep=0.1cm,outer sep=0pt,anchor=center] at (\x,\y) {};
}
}
\draw[step=.1,help lines] (0,0) grid (1,1);
\draw[->] (0,1) -- (1.2,1);
\draw[->] (0,1) -- (0,-0.2);
\end{tikzpicture}


Hope this brings you in the correct direction.

### Colors in tikz

Also when defining new colors you can skip a define color step:

\pgfmathparse{70*rnd+30}
\edef\tmp{\pgfmathresult}
\node[fill=white!\tmp!black] ...


In this case you skip the \definecolor, and need not worry about that. In this case the number should be between 0 and 100. Further more you need the \edef step to keep that result. There is a lot of math going on in tikz so you need to save the math variable temporarily (which is why \edef is needed.)

-
Thank you very much, that is exactly what I was looking for. I'm using your version without the nodes, I just didn't know better. Note: I had to change 0.9 to 1 in the foreach to fill all the rectangles. –  Psirus Dec 6 '11 at 10:17
@Psirus i am glad, sorry, missed that one :) Please see the edited answer where a hint of the color is added. –  zeroth Dec 6 '11 at 10:24
@zeroth you can removeanchor= center, by default it is always used and outer sep=0pt can be also removed because this option changes only the position of the anchors and not the size of the node. –  Alain Matthes Dec 7 '11 at 19:11
@Altermundus i know, i simply added them for descriptive purposes, i also noted in my answer that anchor=center is not needed. –  zeroth Dec 7 '11 at 20:32