0
    \begin{tikzpicture}[scale=1.1]  
    \def\firstrectangle {(6,6) rectangle (4,4.5)};
    \draw[color=black] (6,6) rectangle (5,5.5);
    \draw[color=black] (6,5) rectangle (5,5.5);
    \draw[color=black] (6,4.5) rectangle (5,5.5);

    \draw[color=black] (4,6) rectangle (5,5.5);
    \draw[color=black] (4,5) rectangle (5,5.5);
    \draw[color=black] (4,4.5) rectangle (5,5.5);

    \draw[black,|-|] (4,4,0) -- (6,4,0) node[midway,right] {};
    \draw[black,|-|] (3.5,6,0) -- (3.5,4.5,0) node[midway,right] {};
    \end{tikzpicture}

I need to fill each rectangle with a different color, keeping the black border of each rectangle. I do not know where to start.

leandriis
  • 62,593
  • 3
    You can use the fill option and a color of your choice as in \draw[color=black, fill=green]. – leandriis Feb 01 '20 at 21:49
  • Try \draw[fill=green] .... . Default color of borders are black, so you need only define fill color of rectangles. – Zarko Dec 29 '22 at 08:04

3 Answers3

2

just use fill = <color> and change the order of drawing:

\documentclass[tikz, border = 2 mm]{standalone}

\usepackage{tikz}
\begin{document}

\pagestyle{empty}
\begin{tikzpicture}[scale=1.1]  
    \def\firstrectangle {(6,6) rectangle (4,4.5)};
    \draw[color=black, fill = blue] (4,4.5) rectangle (5,5.5);
    \draw[color=black, fill = brown] (6,4.5) rectangle (5,5.5);
    \draw[color=black, fill=red] (6,6) rectangle (5,5.5);
    \draw[color=black,fill = green] (6,5) rectangle (5,5.5);
    \draw[color=black, fill = yellow] (4,6) rectangle (5,5.5);
    \draw[color=black, fill = purple] (4,5) rectangle (5,5.5);


    \draw[black,|-|] (4,4,0) -- (6,4,0) node[midway,right] {};
    \draw[black,|-|] (3.5,6,0) -- (3.5,4.5,0) node[midway,right] {};
  \end{tikzpicture}

  \end{document}

output.png[1]

hkh
  • 311
0

From my previous answer here -- https://tex.stackexchange.com/a/525323/197451

enter image description here

The code below will point you in the right direction for filling the innards of a rectangle -- size is your choice -- experiment to your hearts content

\documentclass[border=5pt]{standalone}
    \usepackage{pgfplots}
    \pgfplotsset{
    % use `compat' level 1.11 or higher so coordinates don't have to be
    % prefixed with `axis cs:' (any more)
    compat=1.11,
    }
    \begin{document}
    \begin{tikzpicture}[
    region/.style={
        draw=black!50,
        dashed,
    },
    Node/.style={
        midway,
        red,
    },
    declare function={
        xmin=0;
        xmax=12;
        ymin=0;
        ymax=300;
    },
    ]
    \begin{axis}[hide axis,
        xlabel={},
        ylabel={},
        xmin=xmin,
        xmax=xmax,
        ymin=ymin,
        ymax=ymax,
        axis background/.style={},
        extra x ticks={},
        extra y ticks={},
        title=title,
        ]
        \draw [region,fill=brown!40] (xmin,ymin) rectangle (8,ymax)  node [Node, xshift=-55, yshift=-76] {68\%};
        \draw [region,fill=blue!40] (8,ymin)  rectangle (10,50)  node [Node, xshift=-10, yshift=-9] {5\%};
        \draw [region,fill=red!40] (10,ymin)   rectangle (xmax,50)  node [Node, xshift=-10, yshift=-9] {5\%};
        \draw [region,fill=gray!40] (8,50)   rectangle (xmax,ymax) node [Node, xshift=-24, yshift=-64] {22\%};

        \end{axis}
\end{tikzpicture}
\end{document}
js bibra
  • 21,280
0

I suggest that:

\begin{tikzpicture}
\fill[red] (0,0) rectangle (2,2);
\fill[blue] (2,0) rectangle (4,2);
\fill[green] (4,0) rectangle (6,2);
\fill[yellow] (6,0) rectangle (8,2);
\fill[orange] (8,0) rectangle (10,2);
\fill[purple] (10,0) rectangle (12,2);
\fill[pink] (12,0) rectangle (14,2);
\fill[cyan] (14,0) rectangle (16,2);
\fill[magenta] (16,0) rectangle (18,2);
\fill[brown] (18,0) rectangle (20,2);
\fill[gray] (20,0) rectangle (22,2);
\end{tikzpicture}
miltos
  • 2,605
Jamel
  • 9
  • Where are black borders of rectangles? Using for example \draw[fill=red] (0,0) rectangle (2,2); is what OP is after ... – Zarko Dec 29 '22 at 08:01