如何在矩阵图的右侧添加数字,并在其下方或上方添加额外的彩色 1 线数组

如何在矩阵图的右侧添加数字,并在其下方或上方添加额外的彩色 1 线数组

从这个先例问题在 TikZ 中将大型二进制矩阵绘制为彩色网格我有以下矩阵图:

\documentclass{article}
\usepackage{filecontents}
\usepackage[table]{xcolor}
\usepackage{pgfplotstable}
\usetikzlibrary{calc}
\pgfplotsset{compat=1.8}

\begin{filecontents}{matrix.cvs}
0 1 1 0 1 1 0 1 0 0 0 0 0 1 1 0 0 0 1 0 0 0 1 0 1 1 0 1 1 1 1 0 0 0 1 0 1 1 0 0 1 0 1 0 1 0 0 0 1 1
0 0 1 1 0 1 0 0 0 1 0 0 0 0 1 0 0 1 1 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 1 1 0 0 0 0 0 1 0 0 0 0 0 0 1 0
0 1 0 1 0 0 0 0 0 1 1 0 1 0 0 1 0 1 0 0 1 0 0 1 0 0 0 0 0 0 1 0 0 1 1 1 0 0 1 0 0 0 1 0 1 1 0 1 1 0
0 0 1 0 0 0 1 1 0 1 0 1 1 0 0 0 1 1 0 0 1 1 0 0 1 0 1 1 0 0 0 0 0 1 1 0 1 0 1 0 0 0 1 0 0 0 0 1 1 0
1 0 1 1 0 0 1 1 0 1 0 1 0 0 1 0 0 1 1 0 0 0 1 0 0 0 0 0 1 0 1 1 1 1 0 0 0 0 0 0 1 0 0 0 1 1 1 0 0 0
0 0 0 1 0 0 0 1 0 1 0 0 0 1 0 0 1 0 0 0 0 0 1 0 1 1 1 0 1 0 0 1 0 1 0 1 0 0 0 0 1 0 0 0 0 1 0 1 1 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0
1 0 1 0 0 0 1 1 1 1 1 1 0 1 1 1 1 1 0 1 1 1 1 1 1 1 0 1 1 0 1 1 1 1 1 0 1 1 0 1 0 0 1 1 1 0 0 1 1 1
\end{filecontents}

\makeatletter
\tikzset{
    zero color/.initial=white,
    zero color/.get=\zerocol,
    zero color/.store in=\zerocol,
    one color/.initial=red,
    one color/.get=\onecol,
    one color/.store in=\onecol,
    cell wd/.initial=1ex,
    cell wd/.get=\cellwd,
    cell wd/.store in=\cellwd,
    cell ht/.initial=1ex,
    cell ht/.get=\cellht,
    cell ht/.store in=\cellht,
}

\newcommand{\drawgrid}[2][]{
\medskip
\begin{tikzpicture}[#1]
  \pgfplotstableforeachcolumn#2\as\col{
    \pgfplotstableforeachcolumnelement{\col}\of#2\as\colcnt{%
      \ifnum\colcnt=0
        \fill[\zerocol]($ -\pgfplotstablerow*(0,\cellht) + \col*(\cellwd,0) $) rectangle+(\cellwd,\cellht);
      \fi
      \ifnum\colcnt=1
        \fill[\onecol]($ -\pgfplotstablerow*(0,\cellht) + \col*(\cellwd,0) $) rectangle+(\cellwd,\cellht);
      \fi
    }
  }
\end{tikzpicture}
\medskip
}
\makeatother

\begin{document}
% read the file
\pgfplotstableread{matrix.cvs}{\matrixfile}


\begin{figure}[!t]
    \centering
        \drawgrid[zero color=orange, 
          one color=violet,
          cell ht=0.75em,
          cell wd=0.75em]{\matrixfile}
    \caption{caption}
    \label{fig:label}
\end{figure}

\end{document}

绘制一个 50 列的矩阵:

在此处输入图片描述

我想在矩阵的每一行添加一个数字

0.0293
0.0211
0.0165
0.0163
0.0134
0.0382
0.0150
0.8501

并且(对齐)在底部添加一行矩阵图,用白色(0)到黑色(1)表示这些权重。

0.2833
0.0336
0.7766
0.0468
0.0228
0.0143
0.6877
0.0700
0.1287
0.6659
0.7123
0.0143
0.0143
0.6325
0.8049
0.7282
0.1624
0.6346
0.0564
0.0176
0.0143
0.0271
0.6463
0.7268
0.4798
0.1637
0.0297
0.3054
0.5039
0.0246
0.7140
0.3091
0.4256
0.3418
0.6493
0.0508
0.0143
0.2927
0.0291
0.6622
0.0143
0.0177
0.1610
0.1287
0.7471
0.0563
0.0143
0.3275
0.6773
0.5992

得到如下结果:

在此处输入图片描述

(我还没有完成一行灰度矩阵)

为了更好地区分行和列,控制矩阵的白色分隔线将成为绘图的一个特征。

答案1

\documentclass{article}
\usepackage{filecontents}
\usepackage[table]{xcolor}
\usepackage{pgfplotstable}
\usetikzlibrary{calc}
\pgfplotsset{compat=1.16}

\begin{filecontents}{matrix.cvs}
0 1 1 0 1 1 0 1 0 0 0 0 0 1 1 0 0 0 1 0 0 0 1 0 1 1 0 1 1 1 1 0 0 0 1 0 1 1 0 0 1 0 1 0 1 0 0 0 1 1
0 0 1 1 0 1 0 0 0 1 0 0 0 0 1 0 0 1 1 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 1 1 0 0 0 0 0 1 0 0 0 0 0 0 1 0
0 1 0 1 0 0 0 0 0 1 1 0 1 0 0 1 0 1 0 0 1 0 0 1 0 0 0 0 0 0 1 0 0 1 1 1 0 0 1 0 0 0 1 0 1 1 0 1 1 0
0 0 1 0 0 0 1 1 0 1 0 1 1 0 0 0 1 1 0 0 1 1 0 0 1 0 1 1 0 0 0 0 0 1 1 0 1 0 1 0 0 0 1 0 0 0 0 1 1 0
1 0 1 1 0 0 1 1 0 1 0 1 0 0 1 0 0 1 1 0 0 0 1 0 0 0 0 0 1 0 1 1 1 1 0 0 0 0 0 0 1 0 0 0 1 1 1 0 0 0
0 0 0 1 0 0 0 1 0 1 0 0 0 1 0 0 1 0 0 0 0 0 1 0 1 1 1 0 1 0 0 1 0 1 0 1 0 0 0 0 1 0 0 0 0 1 0 1 1 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0
1 0 1 0 0 0 1 1 1 1 1 1 0 1 1 1 1 1 0 1 1 1 1 1 1 1 0 1 1 0 1 1 1 1 1 0 1 1 0 1 0 0 1 1 1 0 0 1 1 1
\end{filecontents}

\makeatletter
\tikzset{
    zero color/.initial=white,
    zero color/.get=\zerocol,
    zero color/.store in=\zerocol,
    one color/.initial=red,
    one color/.get=\onecol,
    one color/.store in=\onecol,
    cell wd/.initial=1ex,
    cell wd/.get=\cellwd,
    cell wd/.store in=\cellwd,
    cell ht/.initial=1ex,
    cell ht/.get=\cellht,
    cell ht/.store in=\cellht,
    xlabels/.store in=\myxlabels,
    ylabels/.store in=\myylabels,
}
\newcommand{\drawgrid}[2][]{
\medskip
\begin{tikzpicture}[#1]
  \pgfplotstableforeachcolumn#2\as\col{
    \pgfplotstableforeachcolumnelement{\col}\of#2\as\colcnt{%
      \ifnum\colcnt=0
        \fill[\zerocol]($ (0,-\pgfplotstablerow*\cellht) + (\col*\cellwd,0) $) rectangle+(\cellwd,\cellht);
      \fi
      \ifnum\colcnt=1
        \fill[\onecol]($ (0,-\pgfplotstablerow*\cellht) + (\col*\cellwd,0) $) rectangle+(\cellwd,\cellht);
      \fi
    }
  }
  \pgfplotstablegetrowsof{#2}
  \pgfmathtruncatemacro{\numrows}{\pgfplotsretval}
  \pgfplotstablegetcolsof{#2}
  \pgfmathtruncatemacro{\numcols}{\pgfplotsretval}
  \draw[white] (0,\cellht) grid[xstep=\cellwd,ystep=\cellht]
     (\cellwd*\numcols,\cellht-\cellht*\numrows); %
  \foreach \Z [count=\Y] in \myylabels   
   {\node[anchor=west,font=\sffamily,scale=0.7] at 
    (\cellwd*\numcols,1.5*\cellht-\cellht*\Y) {\Z};}
  \foreach \Z [count=\X,evaluate=\Z as \CC using {int(\Z*100)}] in \myxlabels    
   {\fill[black!\CC]  
   (-\cellwd+\X*\cellwd,0.5*\cellht-\cellht*\numrows)
    rectangle ++ (\cellwd,-\cellht);
    \node[anchor=west,rotate=-90,font=\sffamily,scale=0.7] at 
    (-0.5*\cellwd+\X*\cellwd,-0.5*\cellht-\cellht*\numrows) {\Z};
    }
\end{tikzpicture}
\medskip
}
\makeatother

\begin{document}
% read the file
\pgfplotstableread{matrix.cvs}{\matrixfile}


\begin{figure}[!t]
    \centering
        \drawgrid[zero color=orange, 
          one color=violet,
          cell ht=0.75em,
          cell wd=0.75em,
          xlabels={0.2833,0.0336,0.7766,0.0468,0.0228,0.0143,0.6877,0.0700,0.1287,0.6659,0.7123,0.0143,0.0143,0.6325,0.8049,0.7282,0.1624,0.6346,0.0564,0.0176,0.0143,0.0271,0.6463,0.7268,0.4798,0.1637,0.0297,0.3054,0.5039,0.0246,0.7140,0.3091,0.4256,0.3418,0.6493,0.0508,0.0143,0.2927,0.0291,0.6622,0.0143,0.0177,0.1610,0.1287,0.7471,0.0563,0.0143,0.3275,0.6773,0.5992},
          ylabels={0.0293,0.0211,0.0165,0.0163,0.0134,0.0382,0.0150,0.8501}]{\matrixfile}
    \caption{caption}
    \label{fig:label}
\end{figure}

\end{document}

在此处输入图片描述

请注意,对于给定的页面参数来说,该图形有点太宽,这意味着您可能需要使页面更宽、使图形更小或旋转图形。

相关内容