\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}
我需要用不同的颜色填充每个矩形,并保留每个矩形的黑色边框。我不知道从哪里开始。
答案1
只需使用fill = <color>
和改变绘图顺序:
\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}
答案2
从我之前的回答来看——https://tex.stackexchange.com/a/525323/197451
下面的代码将为你指明填充矩形内部的正确方向——大小由你选择——尽情尝试吧
\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}
答案3
我建议:
\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}