9

I have a lattice of nodes generated by two \foreach commands. So each node has two numbers which define its location (essentially x and y coordinates). Is there any way to fill a circle at that node according to a value in a table.

I wish to do this as the colour I would like is generated by a piece of Matlab script based on a model. So basically the ideal would be to have TikZ look up the current nodes location say (4,5) in a table which would give it the colour it should be.

Is there any way to do this or am I asking too much?

2
  • This is definitely doable, though the details depend on your exact requirements. Could you provide a sample of dummy data to show what your lookup table looks like, and a mock-up of what you want your output to look like?
    – Jake
    Jun 8 '12 at 11:37
  • 2
    Maybe it is easier to do the contrary. Instead of you said to tikz to read the table you could said to matlab (or python or what programming language you know) to generate the tikz code according to the values included in the table.
    – Ger
    Jun 8 '12 at 12:36
12

if you can provide the colors as a list like color1 & color2 & color3 & ... you could do it like this:

\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}

enter image description here


Edit 1: As suggested by Jake, here's a variant where your color data would be numbered, again separated by &:

\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}

enter image description here

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.