代码

代码

我怎样才能使用 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 的代码(作为答案发布),必须添加垂直线作为\begin{tabular}{|*{12}{R|}}和水平线通过\hlines。F 更改线条的颜色,加载colortbl并发出\arrayrulecolor{green}。我还将 更改为 ,\fboxsep这样8mm内容就不会超出页面。对于tables ,我总是更喜欢\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}

在此处输入图片描述

相关内容