我有一个由两个命令生成的节点格\foreach
。因此每个节点都有两个数字来定义其位置(本质上是 x 和 y 坐标)。有没有办法根据表中的值填充该节点上的圆圈。
我希望这样做,因为我想要的颜色是由基于模型的一段 Matlab 脚本生成的。因此,基本上理想的做法是让 TikZ 查找当前节点的位置(例如(4,5)
在表中),从而为其提供应有的颜色。
有什么办法可以做到这一点吗,或者我的要求太多了?
答案1
如果您可以将颜色作为列表提供,color1 & color2 & color3 & ...
则可以这样做:
\documentclass[parskip]{scrartcl}
\usepackage[margin=15mm]{geometry}
\usepackage{tikz}
\usepackage{arrayjobx}
\usepackage{trimspaces}
\makeatletter
\def\trimspace#1{\trim@spaces@in{#1}}
\makeatother
\newarray\temp
\readarray{temp}{yellow&blue&red&red&yellow&blue&red&blue&green&yellow&green&green&yellow&red&blue&yellow&
red&red&blue&green&yellow&yellow&yellow&green&green&red&green&green&green&red&green&red&red&blue&yellow&red&
red&green&yellow&blue&blue&blue&blue&red&red&blue&blue&green&red&red&red&blue&red&blue&red&green&red&red&
green&red&yellow&green&green&yellow&blue&green&red&green&green&green&green&blue&red&yellow&yellow&yellow&
blue&yellow&yellow&green&blue&yellow&blue&yellow&blue&yellow&green&red&blue&yellow&blue&blue&red&yellow&
yellow&green&red&green&green&blue&blue&yellow&green&green&green&red&yellow&blue&red&yellow}
\dataheight=10
\begin{document}
\begin{tikzpicture}
\foreach \x in {1,...,10}
{ \foreach \y in {1,...,10}
{ \pgfmathtruncatemacro{\xt}{\x}
\pgfmathtruncatemacro{\yt}{\y}
\checktemp(\xt,\yt)
\trimspace\cachedata
\node[inner sep=0.3pt, fill=\cachedata, minimum width=0.9cm, minimum height=0.9cm] at (\xt,\yt) {};
}
}
\end{tikzpicture}
\end{document}
编辑1:根据建议杰克,这里有一个变体,其中您的颜色数据将被编号,同样用以下方式分隔&
:
\documentclass[parskip]{scrartcl}
\usepackage[margin=15mm]{geometry}
\usepackage{tikz}
\usepackage{arrayjobx}
\usepackage{trimspaces}
\makeatletter
\def\trimspace#1{\trim@spaces@in{#1}}
\makeatother
%\newarray\temp
%\readarray{temp}{yellow&blue&red&red&yellow&blue&red&blue&green&yellow&green&green&yellow&red&blue&yellow&
%red&red&blue&green&yellow&yellow&yellow&green&green&red&green&green&green&red&green&red&red&blue&yellow&red&
%red&green&yellow&blue&blue&blue&blue&red&red&blue&blue&green&red&red&red&blue&red&blue&red&green&red&red&
%green&red&yellow&green&green&yellow&blue&green&red&green&green&green&green&blue&red&yellow&yellow&yellow&
%blue&yellow&yellow&green&blue&yellow&blue&yellow&blue&yellow&green&red&blue&yellow&blue&blue&red&yellow&
%yellow&green&red&green&green&blue&blue&yellow&green&green&green&red&yellow&blue&red&yellow}
%\dataheight=10
\newarray\numbers
\readarray{numbers}{2 & 6 & 2 & 4 & 5 & 6 & 1 & 5 & 6 & 3 & 3 & 2 & 6 & 4 & 5 & 4 & 6 & 4 & 5 & 6 & 5 & 1 & 3 & 6 & 5 & 6 & 4 & 6 & 1 & 6 & 5 & 4 & 4 & 4 & 3 & 2 & 3 & 3 & 2 & 6 & 2 & 4 & 5 & 2 & 4 & 1 & 3 & 5 & 1 & 4 & 1 & 1 & 5 & 4 & 2 & 6 & 2 & 2 & 1 & 2 & 2 & 1 & 5 & 1 & 6 & 4 & 4 & 2 & 1 & 3 & 4 & 3 & 3 & 5 & 1 & 1 & 3 & 2 & 5 & 6 & 2 & 3 & 2 & 4 & 6 & 2 & 1 & 1 & 4 & 6 & 1 & 1 & 1 & 1 & 3 & 4 & 3 & 6 & 5 & 5}
\dataheight=10
\begin{document}
%\begin{tikzpicture}
% \foreach \x in {1,...,10}
% { \foreach \y in {1,...,10}
% { \pgfmathtruncatemacro{\xt}{\x}
% \pgfmathtruncatemacro{\yt}{\y}
% \checktemp(\xt,\yt)
% \trimspace\cachedata
% \node[inner sep=0pt, fill=\cachedata, minimum width=0.9cm, minimum height=0.9cm] at (\xt,\yt) {};
% }
% }
%\end{tikzpicture}
\begin{tikzpicture}
[ 1/.style={fill=red!50!gray},
2/.style={fill=yellow!50!gray},
3/.style={fill=blue!50!gray},
4/.style={fill=orange!50!gray},
5/.style={fill=violet!50!gray},
6/.style={fill=green!50!gray}
]
\foreach \x in {1,...,10}
{ \foreach \y in {1,...,10}
{ \pgfmathtruncatemacro{\xt}{\x}
\pgfmathtruncatemacro{\yt}{\y}
\checknumbers(\xt,\yt)
\trimspace\cachedata
\node[inner sep=0pt, \cachedata, minimum width=0.95cm, minimum height=0.95cm,rounded corners=0.1cm] at (\xt,\yt) {};
}
}
\end{tikzpicture}
\end{document}