1

I am preparing a Beamer presentation, and would like to have kind of a function (I will do this several times with different inputs) that given a matrix with elements between 0 and 1 (like an output from matlab: X = rand(10,20), shows a color image (like the output of imagesc(X)) with frames around the cells. So, for example if the input is X =[0.72, 0.70, 0.45, 0.58, 0.34, 0.17, 0.39] the output looks like this image

enter image description here

and if it is a matrix, the following image: enter image description here

What MATLBA does for imagesc(X) is first making a colormap with RGB values, something like

colormap

ans =

     0         0         0
     0         0    0.6250
     0         0    0.6875
     0         0    0.7500
     0         0    0.8125
     0         0    0.8750
     0         0    0.9375
     0         0    1.0000
     0    0.0625    1.0000
     0    0.1250    1.0000
     0    0.1875    1.0000
     0    0.2500    1.0000
     0    0.3125    1.0000
     0    0.3750    1.0000
     0    0.4375    1.0000
     0    0.5000    1.0000
     0    0.5625    1.0000
     0    0.6250    1.0000
     0    0.6875    1.0000
     0    0.7500    1.0000
     0    0.8125    1.0000
     0    0.8750    1.0000
     0    0.9375    1.0000
     0    1.0000    1.0000
0.0625    1.0000    0.9375
0.1250    1.0000    0.8750
0.1875    1.0000    0.8125
0.2500    1.0000    0.7500
0.3125    1.0000    0.6875
0.3750    1.0000    0.6250
0.4375    1.0000    0.5625
0.5000    1.0000    0.5000
0.5625    1.0000    0.4375
0.6250    1.0000    0.3750
0.6875    1.0000    0.3125
0.7500    1.0000    0.2500
0.8125    1.0000    0.1875
0.8750    1.0000    0.1250
0.9375    1.0000    0.0625
1.0000    1.0000         0
1.0000    0.9375         0
1.0000    0.8750         0
1.0000    0.8125         0
1.0000    0.7500         0
1.0000    0.6875         0
1.0000    0.6250         0
1.0000    0.5625         0
1.0000    0.5000         0
1.0000    0.4375         0
1.0000    0.3750         0
1.0000    0.3125         0
1.0000    0.2500         0
1.0000    0.1875         0
1.0000    0.1250         0
1.0000    0.0625         0
1.0000         0         0
0.9375         0         0
0.8750         0         0
0.8125         0         0
0.7500         0         0
0.6875         0         0
0.6250         0         0
0.5625         0         0
0.5000         0         0

Then, based on this map, assign colors to the elements.

I was wondering if this is possible in tikz?

  • What do the numbers in the input represent? – Peter Grill Nov 2 '14 at 20:48
3

1. Random Colors:

Here is an adaptation of Drawing grids filled with random selected colors and connecting them which alows you to specify the dimensions of the matrix:

enter image description here

2. Colors Specified per Cell:

If you wish to specify the colors for each cell, then you can use the datatool package to read in a CSV file which contains the mapping from a number to a color. This requires that you specify each cell color. Then the input:

\DrawGrid{
    {1,2,3},
    {3,4,5}}

\DrawGrid{
    { 1, 7, 8, 9,12,11,13, 6, 4, 8},
    { 4, 5,12,15,17, 3, 6, 5, 9, 4},
    { 7, 4, 8, 2,12,14, 7, 3, 6, 5},
    { 3,12, 9,11, 4, 7, 9,16,11,10}}

\DrawGrid{
    {1},
    {5},
    {11},
    {13},
    {3}}

yields:

enter image description here

Notes:

  • The filecontents package was used to set up a file to read the mapping from index to color for this test case. It is not needed in you actual use case.

Code: Random Colors

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{calc}

\pgfmathdeclarerandomlist{MyRandomColors}{%
    {red}%
    {red!25}%
    {magenta}%
    {magenta!25}%
    {olive}%
    {olive!25}%
    {brown}%
    {brown!10}%
    {violet}%
    {violet!25}%
    {gray}%
    {purple}%
    {yellow}%
    {orange}%
    {orange!25}%
    {cyan}%
    {green}%    
}%

\newcommand*{\DrawGrid}[2]{%
\begin{tikzpicture}[scale=.7, ultra thick]
    \foreach \y in {1,...,#2} {
        \foreach \x in {1,...,#1} {
            \pgfmathrandomitem{\RandomColor}{MyRandomColors} 
            \draw [fill=\RandomColor, fill opacity=0.4, draw=none, ultra thick] 
                (\x-1,\y-1) rectangle (\x,\y);
        }%
    }%
    \draw (0, 0) grid (#1, #2);
    \draw (0, 0) rectangle (#1, #2);
\end{tikzpicture}
}

\begin{document}
\DrawGrid{4}{1}
\bigskip

\DrawGrid{8}{5}
\bigskip

\DrawGrid{1}{5}
\bigskip

\end{document}

Code: Colors Specified per Cell

%%  Mapping from number to color
\begin{filecontents*}{MyColorMap.csv}
    1, red
    2, red!25
    3, magenta
    4, magenta!25
    5, olive
    6, olive!25
    7, brown
    8, brown!10
    9, violet
    10, violet!25
    11, gray
    12, purple
    13, yellow
    14, orange
    15, orange!25
    16, cyan
    17, green    
\end{filecontents*}

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{calc}

\usepackage{datatool}
\AtBeginDocument{%
   \DTLloaddb[noheader,keys={Index,Color}]{ColorDB}{MyColorMap.csv}%
}%


\newcommand*{\RowData}{}%
\newcommand*{\ColumnData}{}%
\newcommand*{\NumberOfRows}{}%
\newcommand*{\NumberOfColumns}{}%
\newcommand*{\DrawGrid}[1]{%
\begin{tikzpicture}[scale=.7, ultra thick]
    \edef\ColumnData{#1}
    \foreach [count=\j from 1] \y in \ColumnData {
        \edef\RowData{\y}%
        \xdef\NumberOfColumns{\j}%
        \foreach [count=\i from 1] \x in \RowData {
        \xdef\NumberOfRows{\i}%
            \DTLgetvalue{\MyColor}{ColorDB}{\x}{2}% "2" is the column with the color
            \draw [fill=\MyColor, fill opacity=0.4, draw=none, ultra thick] 
                (\i-1,-\j+1) rectangle (\i,-\j)
                %node [midway] {\x}% <--- Comment this out if don't want number in cell
                ;
        }%
    }%
    \draw (0, -\NumberOfColumns) grid (\NumberOfRows, 0);
    \draw (0, -\NumberOfColumns) rectangle (\NumberOfRows, 0);
\end{tikzpicture}
}


\begin{document}
\DrawGrid{
    {1,2,3},
    {3,4,5}}
\bigskip

\DrawGrid{
    { 1, 7, 8, 9,12,11,13, 6, 4, 8},
    { 4, 5,12,15,17, 3, 6, 5, 9, 4},
    { 7, 4, 8, 2,12,14, 7, 3, 6, 5},
    { 3,12, 9,11, 4, 7, 9,16,11,10}}
\bigskip

\DrawGrid{
    {1},
    {5},
    {11},
    {13},
    {3}}

\end{document}
  • Is there a way to input the numbers (matrix elements) and have the colors selected based on the inputs? – user2262793 Nov 2 '14 at 20:59
  • @user2262793: Please update you question with information as to exactly how you want to specify the images you provided, and how to map the numbers to a color. Also, instead of numbers, why not just specify the actual colors? – Peter Grill Nov 2 '14 at 21:11
  • I updated the question with more details. Thanks for your response! – user2262793 Nov 3 '14 at 0:54
  • @user2262793: I still don't know what the numbers mean? – Peter Grill Nov 4 '14 at 1:05

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.