![如何用一些小矩形完成此图](https://linux22.com/image/296655/%E5%A6%82%E4%BD%95%E7%94%A8%E4%B8%80%E4%BA%9B%E5%B0%8F%E7%9F%A9%E5%BD%A2%E5%AE%8C%E6%88%90%E6%AD%A4%E5%9B%BE.png)
我已经有一些 tikz 代码,它是一个框架结构,代表无线网络隐藏问题
\documentclass{standalone}
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{array}
\usetikzlibrary{arrows}
\begin{document}
\thispagestyle{empty}
\begin{tikzpicture}[scale=0.5]
\draw (0,-1)--(12,-1);
\node at (0.5,-2) {\tiny RF};
\draw [fill=gray] (0,-1) rectangle (.25em,0.125/4);
\draw (0 1,-1.25)--(1,-1.25);
\node at (0.5,0.05) {\tiny A};
\draw (0,0)--(0,-2);
\node at (1.5,0.05) {\tiny B};
\draw (1,0)--(1,-2);
\node at (2.5,0.05) {\tiny C};
\draw (2,0)--(2,-2);
\node at (3.5,0.05) {\tiny D};
\draw (3,0)--(3,-2);
\node at (4.5,0.05) {\tiny E};
\draw (4,0)--(4,-2);
\node at (5.5,0.05) {\tiny F};
\draw (5,0)--(5,-2);
\node at (6.5,0.05) {\tiny G};
\draw (6,0)--(6,-2);
\node at (7.5,0.05) {\tiny H};
\draw (7,0)--(7,-2);
\node at (8.5,0.05) {\tiny I};
\draw (8,0)--(8,-2);
\node at (9.5,0.05) {\tiny J};
\draw (9,0)--(9,-2);
\node at (10.5,0.05) {\tiny K};
\draw (10,0)--(10,-2);
\node at (11.5,0.05) {\tiny L};
\draw (11,0)--(11,-2);
\draw (12,0)--(12,-2);
\end{tikzpicture}
\end{document}
现在我想用一些小矩形填充,就像图中第一个 RF“A”一样,后面跟着 RF B、C、D……等,它们都一样。我会尝试 \draw [fill=gray] (0,-1) rectangle (.25em,0.125/4); 下面这段代码并更改坐标。但结果不是我想要的。请帮帮我!清楚了吗?
答案1
这会是您想要实现的目标吗?当我运行您的案例并在代码中添加更多矩形时,似乎矩形与坐标混淆了。因此,此解决方案使用相对坐标\draw (A) rectangle ++(x,y)
,问题似乎已消失。此解决方案还将代码简化为foreach loop
编码。
注意:感谢@Ignasi 的评论,它极大地改进了代码。
代码
\documentclass{standalone}
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{array}
\usetikzlibrary{arrows}
\begin{document}
\thispagestyle{empty}
\begin{tikzpicture}[scale=0.5]
\draw (0,-1)--(12,-1);
\foreach \x[count=\xi from 0] in {A, B, ..., L}{
\node at (\xi.5,0.05) {\tiny \x};
\node at (\xi.5,-2) {\tiny RF};
\draw (\xi,0)--(\xi,-2);
\draw [fill=gray] (\xi,-1) rectangle ++(.25em,1+1/32);
}
\draw (12,0)--(12,-2);
\end{tikzpicture}
\end{tikzpicture}
\end{document}
答案2
简化和清理后的版本,以达到我认为你想要实现的效果。部分内容取自 Jesse 的解决方案并尝试简化代码以便更好地理解。
\documentclass[tikz, border=5mm]{standalone}
\begin{document}
\begin{tikzpicture}[scale=.5]
\draw (0,0) -- ++(12,0);
\foreach \l [count=\x from 0] in {A,...,L} {
\draw [fill=gray] (\x, -1) -- ++(0,1) rectangle ++(.1,1);
\node at (\x.5, 1) {{\tiny \l}};
\node at (\x.5,-1) {{\tiny RF}};
}
\draw (12,-1) -- ++(0,2);
\end{tikzpicture}
\end{document}
y
可以通过修改矩形的第二个(相对)坐标的值来调整矩形的高度。