以下是我制作的网格笑脸代码:
\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
% Grid
\draw[step=1cm,black!50] (0,0) grid (16,16);
% Filling black squares
\foreach \x/\y in {
5/1, 6/1, 7/1, 8/1, 9/1, 10/1, 11/2, 12/2, 13/3, 13/4, 14/5, 14/6, 14/7, 14/8, 14/9, 14/10, 13/11, 13/12, 12/13, 11/13, 10/14, 9/14, 8/14, 7/14, 6/14, 5/14, 4/13, 3/13, 2/12, 2/11, 1/10, 1/9, 1/8, 1/7, 1/6, 1/5, 2/4, 2/3, 3/2, 4/2, 4/6, 5/5, 6/4, 7/4, 8/4, 9/4, 10/5, 11/6, 10/9, 9/9, 10/10, 9/10, 6/9, 5/9, 6/10, 5/10
}
\fill[black] (\x,\y) rectangle ++(1,1);
% Filling yellow squares
\foreach \x/\y in {
5/11, 5/12, 5/13, 6/13, 7/13, 8/13, 9/13, 10/13, 10/12, 10/11, 9/11, 8/11, 7/11, 6/11, 8/12, 7/12, 6/12, 9/12, 5/6, 6/6, 7/6, 8/6, 9/6, 10/6, 9/5, 8/5, 7/5, 6/5, 7/8, 2/8, 3/8, 4/8, 5/8, 6/8, 8/8, 9/8, 10/8, 11/8, 12/8, 13/8, 13/7, 12/7, 11/7, 10/7, 9/7, 8/7, 7/7, 6/7, 5/7, 4/7, 3/7, 7/9, 8/9, 7/10, 8/10, 13/6, 13/5, 12/5, 12/6, 11/5, 11/4, 11/3, 12/3, 12/4, 10/4, 10/3, 10/2, 9/2, 8/2, 7/2, 6/2, 5/2, 6/3, 7/3, 8/3, 9/3, 5/3, 4/3, 3/3, 3/4, 4/4, 5/4, 4/5, 3/5, 2/5, 2/6, 2/7, 3/6, 2/10, 2/9, 3/10, 3/9, 4/9, 4/10, 4/11, 4/12, 3/11, 3/12, 13/10, 13/9, 12/9, 11/9, 11/10, 12/10, 12/11, 12/12, 11/11, 11/12
}
\fill[yellow] (\x,\y) rectangle ++(1,1);
\end{tikzpicture}
\end{document}
然而,这就是我想要实现的:
我怎样才能实现这个目标?例如,使网格线更清晰,添加轴等。
答案1
从技术上讲,您可以让 TikZ 绘制每个矩形,这样您就不必使用,grid
但网格只会绘制每条线一次。
还存在其他解决方案,即循环遍历字母/数字列表,而,
它们之间没有 <!-- SIPO <DP n="1"> -->联系,但PGF/TikZ带来的解决\pgfmathtodigitlist
方案不仅对数字有效,而且对字母也有效。
从技术上讲,尾随-
是没有必要的,因为您一开始就不对这些矩形进行任何操作。
代码
\documentclass[tikz]{standalone}
\usetikzlibrary{arrows.meta}
\begin{document}
\begin{tikzpicture}[
x=5mm, y=+-5mm, >=Latex,
rect -/.style={fill=none},
rect y/.style={fill=yellow},
rect b/.style={fill=black},
n/.code n args={3}{%
\ifnum#1=1 \tikzset{edge node={node[at start,#2]{$1$}}}\fi
\ifnum#1=16 \tikzset{edge node={node[at start,#2]{$#3$}}}\fi}]
\foreach[count=\iRow] \Row in {
----- ------ -----,
----- bbbbbb -----,
---bb yyyyyy bb---,
--byy yyyyyy yyb--,
--byy yyyyyy yyb--,
-byyy bbyybb yyyb-,
-byyy bbyybb yyyb-,
-byyy yyyyyy yyyb-,
-byyy yyyyyy yyyb-,
-byyb yyyyyy byyb-,
-byyy byyyyb yyyb-,
--byy ybbbby yyb--,
--byy yyyyyy yyb--,
---bb yyyyyy bb---,
----- bbbbbb -----,
----- ------ -----
}{
\pgfmathtodigitlist{\Columns}{\Row}
\foreach[count=\iCol] \Col in \Columns
\fill[rect \Col] (\iCol, \iRow) rectangle +(1,1);
}
\draw[gray, xstep=1, ystep=-1] (1,1) grid +(16, 16);
\path[->, at end, thick] (-1, 0) edge node[right]{columns} (right:18)
( 0,-1) edge node[below]{rows} (up:18);
\foreach \i in {1, ..., 16}
\path[semithick] (\i.5, 0) edge[n={\i}{above}{w}] ++(down:3pt)
(0, \i.5) edge[n={\i}{left}{h}] ++(right:3pt);
\end{tikzpicture}
\end{document}
输出
答案2
像这样吗?
\documentclass[10pt,border=3mm,tikz]{standalone}
% ~~~ to do, see pgfmanual for details:
% \usetikzlibrary{arrows.meta}
\begin{document}
\begin{tikzpicture}
% % Grid
% \draw[step=1cm,black!50] (0,0) grid (16,16);
% Filling black squares
\foreach \x/\y in {
5/1, 6/1, 7/1, 8/1, 9/1, 10/1, 11/2, 12/2, 13/3, 13/4, 14/5, 14/6, 14/7, 14/8, 14/9, 14/10, 13/11, 13/12, 12/13, 11/13, 10/14, 9/14, 8/14, 7/14, 6/14, 5/14, 4/13, 3/13, 2/12, 2/11, 1/10, 1/9, 1/8, 1/7, 1/6, 1/5, 2/4, 2/3, 3/2, 4/2, 4/6, 5/5, 6/4, 7/4, 8/4, 9/4, 10/5, 11/6, 10/9, 9/9, 10/10, 9/10, 6/9, 5/9, 6/10, 5/10
}
\fill[black] (\x,\y) rectangle ++(1,1);
% Filling yellow squares
\foreach \x/\y in {
5/11, 5/12, 5/13, 6/13, 7/13, 8/13, 9/13, 10/13, 10/12, 10/11, 9/11, 8/11, 7/11, 6/11, 8/12, 7/12, 6/12, 9/12, 5/6, 6/6, 7/6, 8/6, 9/6, 10/6, 9/5, 8/5, 7/5, 6/5, 7/8, 2/8, 3/8, 4/8, 5/8, 6/8, 8/8, 9/8, 10/8, 11/8, 12/8, 13/8, 13/7, 12/7, 11/7, 10/7, 9/7, 8/7, 7/7, 6/7, 5/7, 4/7, 3/7, 7/9, 8/9, 7/10, 8/10, 13/6, 13/5, 12/5, 12/6, 11/5, 11/4, 11/3, 12/3, 12/4, 10/4, 10/3, 10/2, 9/2, 8/2, 7/2, 6/2, 5/2, 6/3, 7/3, 8/3, 9/3, 5/3, 4/3, 3/3, 3/4, 4/4, 5/4, 4/5, 3/5, 2/5, 2/6, 2/7, 3/6, 2/10, 2/9, 3/10, 3/9, 4/9, 4/10, 4/11, 4/12, 3/11, 3/12, 13/10, 13/9, 12/9, 11/9, 11/10, 12/10, 12/11, 12/12, 11/11, 11/12
}
\fill[yellow] (\x,\y) rectangle ++(1,1);
% Grid
\draw[step=1cm,black!50] (0,0) grid (16,16);
% ~~~ axis sketch ~~~~~~~~~~~
\draw[->] (-2,18) -- +(20,0) node[xshift=14mm,font=\Large] {columns};
\end{tikzpicture}
\end{document}