Beamer 中的矩阵图像

Beamer 中的矩阵图像

我正在准备一个 Beamer 演示文稿,并且想要一种函数(我将使用不同的输入多次执行此操作),给定一个元素介于 0 和 1 之间的矩阵(如 matlab 的输出:)X = rand(10,20),显示一个彩色图像(如 的输出imagesc(X)),单元格周围有框架。因此,例如,如果输入是X =[0.72, 0.70, 0.45, 0.58, 0.34, 0.17, 0.39]输出看起来像这幅图像

在此处输入图片描述

如果它是一个矩阵,则如下图所示: 在此处输入图片描述

MATLBA 对 imagesc(X) 所做的是首先制作一个带有 RGB 值的颜色图,例如

colormap

答 =

     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

然后,根据这张图,为元素分配颜色。

我想知道这在 tikz 中是否可行?

答案1

1.随机颜色:

以下是改编绘制用随机选择的颜色填充的网格并连接它们它允许您指定矩阵的维度:

在此处输入图片描述

2. 每个单元格指定的颜色:

如果您希望为每个单元格指定颜色,那么您可以使用包裹datatool读入包含从数字到颜色的映射的 CSV 文件。这需要您指定每个单元格的颜色。然后输入:

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

产量:

在此处输入图片描述

笔记:

  • 包裹filecontents 用于设置一个文件来读取此测试用例从索引到颜色的映射。实际用例中不需要它。

代码:随机颜色

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

代码:每个单元格指定的颜色

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

相关内容