5

I'm trying to create a picture like this: where I have a grid and I depict the boxes shifting by colors changing. I was hoping I could achieve this effect easily in Tikz with a foreach (I am very inexperienced with Tikz). It doesn't seem to like me putting the variable \j or \i into the color section – in particular, when I try and multiply it by something (for example, and as in the presented code, 10).

\begin{tikzpicture}[scale=.5]
\foreach \i in {1,2,3,4,5,6,7,8}
{
\foreach \j in {1,2,3,4,5,6,7,8}
{
\definecolor{myblue}{cmyk}{10*j,0,0,0}
\draw[fill= myblue] (\i-1,\j-1)--(\i,\j-1)--(\i,\j)--(\i-1,\j)--(\i-1,\j-1);
}
}
\end{tikzpicture}

Before attempting this solution I was using \draw[fill=blue!10*\j!white] (\i-1,\j-1)--(\i,\j-1)--(\i,\j)--(\i-1,\j)--(\i-1,\j-1);

Any help would be much appreciated.

N.L
  • 115
  • Just need to find a little piece of mathematics that will allow the color change – a linear gradient. Color mixins will likely be useful: blue!\i!white or something like that. – Sean Allred Jun 20 '15 at 21:34

1 Answers1

16

Please always post complete code so that people do not have to guess which packages and class are required to reproduce the problem or answer the question.

The following gives one way of incrementing the depth of colouring according to \j:

\documentclass[tikz,border=5pt]{standalone}
\begin{document}
  \definecolor{myblue}{cmyk}{10,0,0,0}
  \begin{tikzpicture}
    \foreach \i in {1,2,3,4,5,6,7,8}
    {
      \foreach \j in {1,2,3,4,5,6,7,8}
      {
        \pgfmathsetmacro\k{\j*10}
        \draw[fill=myblue!\k] (\i-1,\j-1)--(\i,\j-1)--(\i,\j)--(\i-1,\j)--(\i-1,\j-1);
      }
    }
  \end{tikzpicture}
\end{document}

increments of blue

Probably you want to vary the shade according to \i for the left hand figure:

    \pgfmathsetmacro\k{\i*10}

horizontal incrementing blue

You can get some quite nice effects:

    \pgfmathsetmacro\k{mod(\i+\j-1,8)*10}

more complex increments

cfr
  • 198,882
  • 1
    Worked excellently, thank you! In the future I'll be sure to post the full code related to any questions I have in the future. – N.L Jun 20 '15 at 22:24
  • @NicholasLacasse Sounds like a plan ;). Glad it helped. – cfr Jun 20 '15 at 22:54
  • Would like to know why this got down-voted almost two years later. Somebody would have preferred red squares or something? – cfr Mar 18 '17 at 18:18
  • 2
    Maybe because it did not work for the downvoter, for example because the proposed method was used in an axis environment (in which case \invokepgfplotsforeach should be used, rather than \foreach. Just guessing! – anderstood Aug 20 '17 at 16:58
  • 1
    @anderstood In general, arbitrary code from a tikzpicture will not work in an axis environment, so that would be a bad reason to down vote. However, that is not to say that you aren't correct about the reason. It just bugs me when people down vote without saying why. If the answer's deficient, I'd like to know so I can consider improving it. Just saying 'boo!' isn't helpful :(. – cfr Aug 20 '17 at 20:33
  • @cfr Hopefully, an upvote 4 1/2 years after the answer compensates the downvote :-) – Sveinung Jan 09 '20 at 23:10