1

For a visualization I have in mind I'd like to create an rectangular arrangement of filled circles using different colors. For example, something like this:

+ + + +
+ + + *
+ + * *
+ * * *
* * * *

Where the + would be blue circles and the * would be red circles.

I created a somewhat functional version of this adapted from some other question:

\documentclass[border=5pt,tikz,multi]{standalone}
\usetikzlibrary{fit,positioning}
\begin{document}
\begin{tikzpicture}[ultra thick]
\coordinate (c0) at (0,0);
\foreach \i [count=\j, evaluate=\j as \k using \j-1, evaluate=\j as \n using { \j>0 ? "blue" : "red" }] in {1,...,3}
\node (c\j) [right=5 pt of c\k |- c0, circle, anchor=north west, minimum size=1mm, draw, fill=\n, \n] {};

\coordinate (c0) at (0,1);
\foreach \i [count=\j, evaluate=\j as \k using \j-1, evaluate=\j as \n using { \j>1 ? "blue" : "red" }] in {1,...,3}
\node (c\j) [right=5pt of c\k |- c0, circle, anchor=north west, minimum size=1mm, draw, fill=\n, \n] {};

\coordinate (c0) at (0,2);
\foreach \i [count=\j, evaluate=\j as \k using \j-1, evaluate=\j as \n using { \j>2 ? "blue" : "red" }] in {1,...,3}
\node (c\j) [right=5pt of c\k |- c0, circle, anchor=north west, minimum size=1mm, draw, fill=\n, \n] {};
\end{tikzpicture}
\end{document}

Which renders like this:

enter image description here

I have a few problems with that:

  • I can't find a way to make the vertical and horizontal margins between the circles equal. Meaning all margins inside the arrangement of circles should be equal. In the above image, the circles are too large, the vertical distances are too much and the horizontal margins are too small.
  • It requires quite a bit of code, I wonder if there is a more convenient way to tell which circles should have which color.

2 Answers 2

5

A matrix is an easy way of placing this circles:

\documentclass[tikz]{standalone}
\usetikzlibrary{matrix}
\begin{document}
\begin{tikzpicture}[
    b/.style={fill=blue}]
\matrix(A)[matrix of nodes,
    nodes in empty cells, nodes={circle, fill=red, minimum size=5mm},
    column sep=2mm, row sep=2mm]
    { &&&\\
      &&&|[b]|\\
     &&|[b]|&|[b]|\\
      &|[b]|&|[b]|&|[b]|\\
       |[b]|&|[b]|&|[b]|&|[b]|\\};
\end{tikzpicture}
\end{document}

enter image description here

You can also list which circles have a different color and avoid to marking them inside the matrix:

\documentclass[tikz]{standalone}
\usetikzlibrary{matrix}
\begin{document}
\begin{tikzpicture}[
    circles matrix/.style={
        matrix of nodes,
        nodes in empty cells,
        nodes = {circle, fill=red, minimum size=5mm}
    },
    alternative color/.style args={(#1,#2)}{%
        row #1 column #2/.style={nodes={fill=blue}}}        
    ]
\matrix(A)[circles matrix,
    alternative color/.list={(1,1),(2,3),(3,2),(4,4),(5,1),(5,3)},
    column sep=2mm, row sep=2mm]
    { &&&\\ &&&\\ &&&\\ &&&\\ &&&\\};
\end{tikzpicture}
\end{document}

enter image description here

But you can also use a foreach loop:

\documentclass[tikz]{standalone}
\begin{document}
\begin{tikzpicture}[x=8mm,y=8mm]
\foreach \y in {1,...,5}{
    \foreach \x in {1,...,4}{
        \ifnum\numexpr6-\y>\x
            \node [circle, fill=red, minimum size=5mm] at (\x,-\y) {};
        \else%
            \node [circle, fill=blue, minimum size=5mm] at (\x,-\y) {};
        \fi%
    }
    }
\end{tikzpicture}
\end{document}
4

This code is rewrite from the above code of Ignasi using syntax x ? y : z of PGF. Sizes are controled via [scale], radius of circles, number of columns n, number of rows m.

enter image description here

\documentclass[tikz,border=2mm]{standalone}
\begin{document}
\begin{tikzpicture}[scale=.5]
\def\n{4} % number of rows
\def\m{5} % number of columns
\foreach \i in {1,...,\n} 
\foreach \j in {1,...,\m}
{
\pgfmathparse{\m-\i<\j ? "violet" : "teal"}
\fill[\pgfmathresult] (\i,-\j) circle(.3);
}
\end{tikzpicture}
\end{document}

You must log in to answer this question.

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