
如何使用 TikZ 畫出這樣的圖
我嘗試了以下範例,但無法知道如何繪製單元格輪廓(邊框)。
\documentclass[12pt,a4paper]{article}
\usepackage[left=0cm,right=0cm,top=0cm,bottom=0cm]{geometry}
\usepackage{tikz}
\usepackage{collcell}
\newcommand*{\MinNumber}{0}%
\newcommand*{\MaxNumber}{1}%
\newcommand{\ApplyGradient}[1]{%
\pgfmathsetmacro{\PercentColor}{100.0*(#1-\MinNumber)/(\MaxNumber-\MinNumber)}
\hspace{-0.33em}\colorbox{red!\PercentColor!black}{}
}
\newcolumntype{R}{>{\collectcell\ApplyGradient}c<{\endcollectcell}}
\renewcommand{\arraystretch}{0}
\setlength{\fboxsep}{10mm} % box size
\setlength{\tabcolsep}{0pt}
\begin{document}
\begin{table}[ht]
\begin{center}
\begin{tabular}{*{12}{R}}
0.03 & 0.34 & 0.41 & 0.25 & 0.89 & 0.49 & 0.79 & 0.83 & 0.82 & 0.94 & 0.34 & 0.41 \\
0.49 & 0.25 & 0.80 & 0.83 & 0.93 & 0.25 & 0.82 & 0.80 & 0.35 & 0.00 & 0.25 & 0.80 \\
0.21 & 0.13 & 0.53 & 0.07 & 1.00 & 0.66 & 0.07 & 0.18 & 0.73 & 0.05 & 0.13 & 0.53 \\
0.81 & 0.49 & 0.27 & 0.07 & 0.57 & 0.12 & 0.26 & 0.39 & 0.57 & 0.80 & 0.49 & 0.27 \\
0.23 & 0.31 & 0.90 & 0.03 & 0.18 & 0.60 & 0.49 & 0.02 & 0.44 & 0.50 & 0.31 & 0.90 \\
0.40 & 0.91 & 0.84 & 0.88 & 0.04 & 0.59 & 0.75 & 0.91 & 0.69 & 0.88 & 0.91 & 0.84 \\
0.36 & 0.12 & 0.92 & 0.73 & 0.12 & 0.58 & 0.42 & 0.89 & 0.15 & 0.50 & 0.12 & 0.92 \\
0.93 & 0.07 & 0.57 & 0.43 & 0.12 & 0.70 & 0.54 & 0.25 & 0.06 & 0.57 & 0.07 & 0.57 \\
0.09 & 0.98 & 0.46 & 0.83 & 0.69 & 0.56 & 0.08 & 0.77 & 0.40 & 0.88 & 0.98 & 0.46 \\
0.81 & 0.49 & 0.27 & 0.07 & 0.57 & 0.12 & 0.26 & 0.39 & 0.57 & 0.80 & 0.49 & 0.27 \\
0.36 & 0.12 & 0.92 & 0.73 & 0.12 & 0.58 & 0.42 & 0.89 & 0.15 & 0.50 & 0.12 & 0.92 \\
0.93 & 0.07 & 0.57 & 0.43 & 0.12 & 0.70 & 0.54 & 0.25 & 0.06 & 0.57 & 0.07 & 0.57 \\
0.09 & 0.98 & 0.46 & 0.83 & 0.69 & 0.56 & 0.08 & 0.77 & 0.40 & 0.88 & 0.98 & 0.46 \\
0.81 & 0.49 & 0.27 & 0.07 & 0.57 & 0.12 & 0.26 & 0.39 & 0.57 & 0.80 & 0.49 & 0.27 \\
0.09 & 0.98 & 0.46 & 0.83 & 0.69 & 0.56 & 0.08 & 0.77 & 0.40 & 0.88 & 0.98 & 0.46 \\
0.07 & 0.44 & 0.65 & 0.46 & 0.65 & 0.75 & 0.80 & 0.56 & 0.89 & 0.50 & 0.44 & 0.65 \\
\end{tabular}
\end{center}
\end{table}
\end{document}
答案1
我會用 TikZmatrix
來畫這個。
程式碼
\documentclass[border=2pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix,backgrounds}
\begin{document}
\begin{tikzpicture}
\matrix(m)[matrix of nodes,nodes in empty cells,
nodes={inner sep=20pt,draw={green!20!yellow},line width=2pt},
row sep=-\pgflinewidth,
column sep=-\pgflinewidth,
]{
&&&&&&&&&&&\\
&&&&&&&&&&&\\
&&&&&&&&&&&\\
&&&&&&&&&&&\\
&&&&&&&&&&&\\
&&&&&&&&&&&\\
&&&&&&&&&&&\\
&&&&&&&&&&&\\
&&&&&&&&&&&\\
&&&&&&&&&&&\\
&&&&&&&&&&&\\
&&&&&&&&&&&\\
};
\begin{scope}[on background layer]
\foreach \i in {1,...,12} \foreach \j in {1,...,12}{
\pgfmathrandominteger\x{10}{70}
\node[fill=black!\x!white,inner sep=20pt]at(m-\i-\j){};
}
\end{scope}
\end{tikzpicture}
\end{document}
輸出
答案2
這使用了 Kevin C 的想法來放置顏色,但沒有matrix
。
\documentclass[border=2pt,tikz]{standalone}
\begin{document}
\begin{tikzpicture}
%% draw the color boxes
\foreach \i in {0.5,...,11.5} {
\foreach \j in {0.5,...,11.5}{
\pgfmathrandominteger\x{10}{70}
\node[fill=black!\x!white,inner sep=0pt,minimum width=1cm,minimum height=1cm]at(\i,\j){};
}
}
%% draw the borders
\foreach \x in {0,1,...,12}{
\draw[green!20!yellow,line width=2pt] (\x,0) -- (\x,12);
}
\foreach \y in {0,1,...,12}{
\draw[green!20!yellow,line width=2pt] (0,\y) -- (12,\y);
}
\end{tikzpicture}
\end{document}
擊鍵次數本可以進一步減少,但我保留了原樣。
接合水晶球:
從OP(作為答案發布)中獲取程式碼,必須添加垂直線 as\begin{tabular}{|*{12}{R|}}
和水平線到\hline
s 。 F 更改線條顏色、載入colortbl
並發出\arrayrulecolor{green}
。我還把 改為 ,\fboxsep
這樣8mm
事情就不會超出頁面範圍。對於table
s 我總是\centering
比較喜歡\begin{center}
.
完整程式碼:
\documentclass[12pt,a4paper]{article}
\usepackage[left=0cm,right=0cm,top=0cm,bottom=0cm]{geometry}
\usepackage{tikz}
\usepackage{collcell,colortbl}
\newcommand*{\MinNumber}{0}%
\newcommand*{\MaxNumber}{1}%
\newcommand{\ApplyGradient}[1]{%
\pgfmathsetmacro{\PercentColor}{100.0*(#1-\MinNumber)/(\MaxNumber-\MinNumber)}
\hspace{-0.33em}\colorbox{red!\PercentColor!black}{}
}
\newcolumntype{R}{>{\collectcell\ApplyGradient}c<{\endcollectcell}}
\renewcommand{\arraystretch}{0}
\setlength{\fboxsep}{8mm} % box size
\setlength{\tabcolsep}{0pt}
\setlength{\arrayrulewidth}{2pt}
\arrayrulecolor{green}
\begin{document}
\begin{table}[ht]
\centering
\begin{tabular}{|*{12}{R|}}\hline
0.03 & 0.34 & 0.41 & 0.25 & 0.89 & 0.49 & 0.79 & 0.83 & 0.82 & 0.94 & 0.34 & 0.41 \\\hline
0.49 & 0.25 & 0.80 & 0.83 & 0.93 & 0.25 & 0.82 & 0.80 & 0.35 & 0.00 & 0.25 & 0.80 \\\hline
0.21 & 0.13 & 0.53 & 0.07 & 1.00 & 0.66 & 0.07 & 0.18 & 0.73 & 0.05 & 0.13 & 0.53 \\\hline
0.81 & 0.49 & 0.27 & 0.07 & 0.57 & 0.12 & 0.26 & 0.39 & 0.57 & 0.80 & 0.49 & 0.27 \\\hline
0.23 & 0.31 & 0.90 & 0.03 & 0.18 & 0.60 & 0.49 & 0.02 & 0.44 & 0.50 & 0.31 & 0.90 \\\hline
0.40 & 0.91 & 0.84 & 0.88 & 0.04 & 0.59 & 0.75 & 0.91 & 0.69 & 0.88 & 0.91 & 0.84 \\\hline
0.36 & 0.12 & 0.92 & 0.73 & 0.12 & 0.58 & 0.42 & 0.89 & 0.15 & 0.50 & 0.12 & 0.92 \\\hline
0.93 & 0.07 & 0.57 & 0.43 & 0.12 & 0.70 & 0.54 & 0.25 & 0.06 & 0.57 & 0.07 & 0.57 \\\hline
0.09 & 0.98 & 0.46 & 0.83 & 0.69 & 0.56 & 0.08 & 0.77 & 0.40 & 0.88 & 0.98 & 0.46 \\\hline
0.81 & 0.49 & 0.27 & 0.07 & 0.57 & 0.12 & 0.26 & 0.39 & 0.57 & 0.80 & 0.49 & 0.27 \\\hline
0.36 & 0.12 & 0.92 & 0.73 & 0.12 & 0.58 & 0.42 & 0.89 & 0.15 & 0.50 & 0.12 & 0.92 \\\hline
0.93 & 0.07 & 0.57 & 0.43 & 0.12 & 0.70 & 0.54 & 0.25 & 0.06 & 0.57 & 0.07 & 0.57 \\\hline
0.09 & 0.98 & 0.46 & 0.83 & 0.69 & 0.56 & 0.08 & 0.77 & 0.40 & 0.88 & 0.98 & 0.46 \\\hline
0.81 & 0.49 & 0.27 & 0.07 & 0.57 & 0.12 & 0.26 & 0.39 & 0.57 & 0.80 & 0.49 & 0.27 \\\hline
0.09 & 0.98 & 0.46 & 0.83 & 0.69 & 0.56 & 0.08 & 0.77 & 0.40 & 0.88 & 0.98 & 0.46 \\\hline
0.07 & 0.44 & 0.65 & 0.46 & 0.65 & 0.75 & 0.80 & 0.56 & 0.89 & 0.50 & 0.44 & 0.65 \\\hline
\end{tabular}
\end{table}
\end{document}
答案3
根據具體要求,以下內容可能就足夠了:
\documentclass[tikz,border=0.125cm]{standalone}
\begin{document}
\tikz\foreach \i in {0,...,9}
\foreach \j [evaluate={\k=rnd*75;}] in {0,...,9}
\filldraw [fill=gray!\k, draw=green!50!brown, ultra thick]
(\j,\i) rectangle +(1, 1);
\end{document}