更改矩阵中的单元格颜色

更改矩阵中的单元格颜色

我尝试用灰色(\definecolor{grey}{RGB}{220,250,250})为第一个矩阵中的空单元格着色,但我不知道为什么它不起作用。有什么建议吗?

\documentclass{standalone}
%\usepackage[showframe]{geometry}
\usepackage{tikz}
\definecolor{gris}{RGB}{250,250,250}
\usepackage{lmodern}
\usetikzlibrary{matrix,
    positioning}
\tikzset{
    mymatrix/.style = {matrix of nodes,
        nodes in empty cells,
        nodes={minimum height=3ex, minimum width=1em,
            inner sep=0pt, outer sep=0pt, anchor=center,
            draw, very thin,scale=0.8,transform shape},
        column sep=0pt,
        row sep=0pt,
        inner sep=0.5\pgflinewidth, outer sep=1pt,
        draw, thick},
    empty node/.style = {draw,fill=grey},
    every label/.append style = {font=\large, text=teal}
}
\makeatletter
\def\tikz@lib@matrix@empty@cell{%
    \iftikz@lib@matrix@empty%
    \node[name=\tikzmatrixname-\the\pgfmatrixcurrentrow-\the\pgfmatrixcurrentcolumn,empty node]{};\fi}
\makeatother

\usepackage{bm}
\newcommand{\MatrixVariable}[1]{\bm{{#1}}}



\definecolor{blue}{RGB}{180, 171, 250}
\definecolor{grey}{RGB}{250,250,250}

\begin{document}

    \begin{tikzpicture}[node distance=0pt,scale=0.6,nodes={transform
        shape}]
    \matrix (m1) [mymatrix,
    label={[rotate=90, anchor=south,black]left: Movies},
    label={[black]below:$\MatrixVariable{{R}}$},
    label={[black]above:Users},
    nodes={fill=blue}]
    {
        1   &   & 3 &   &   & 5 &   &   & 5 &   & 4 &   \\
        &   & 5 & 4 &   &   & 4 &   &   & 2 & 1 & 3 \\
        2   & 4 &   & 1 & 2 &   & 3 &   & 4 & 3 & 5 &   \\
        & 2 & 4 &   & 5 &   &   & 4 &   &   & 2 &   \\
        &   & 4 & 3 & 4 & 2 &   &   &   &   & 2 & 5 \\
        1   &   & 3 &   & 3 &   &   & 2 &   &   & 4 &   \\
    };
    \node (approx) [right=of m1,scale=2] {$\approx$};
    \matrix (m2) [mymatrix=none, right=0.1em of approx,
    label={[rotate=90, anchor=south east,xshift=-0.7em,black]left: Movies},
     label={[black]below:$\MatrixVariable{{Q}}$},
label={[black]above:$f$-factors},
    nodes={minimum width=2.2em,fill=grey}]
    {
        0.1& -0.4  & -0.2 \\
        -0.5& 0.6 & 0.5 \\
        -0.2& 0.3 &0.5  \\
        1.1& 2.1 & 0.3 \\
        -0.7&2.1  & -2 \\
        -1& 0.7 & 0.3 \\
    };
    \node (times) [right=of m2,scale=2] {$\cdot$};
    \matrix (m3) [mymatrix=none, right=of times,
     label={[rotate=90, anchor=north,black]right: $f$-factors},
  label={[black]below:$\MatrixVariable{{P}^{T}}$},
label={[black]above:Users},
    nodes={minimum width=2em,fill=grey}]
    {
        1.1&-2  & 0.3 & 0.5 &-2  &  -0.5 & 0.8 & -0.4 & 0.3 & 1.4 & 2.4 &  -0.9\\
        -0.8& 0.7 & 0.5 & 1.4 &0.3  & -1 & 1.4 & 2.9 & 0.7 & - 1.2 & 1.3& 0.4\\
        2.1& -0.4 & 0.6 & 1.7 & 2.4 & 0.9 & -0.3 & 0.4 &  0.8 &   0.7 & -0.6&0.1 \\
    };
    \end{tikzpicture}

\end{document}

在此处输入图片描述

答案1

grey您只需在空节点gray中替换即可。tikzset

\documentclass{standalone}
%\usepackage[showframe]{geometry}
\usepackage{tikz}

\usepackage{lmodern}
\usetikzlibrary{matrix,
    positioning}
\tikzset{
    mymatrix/.style = {matrix of nodes,
        nodes in empty cells,
        nodes={minimum height=3ex, minimum width=1em,
            inner sep=0pt, outer sep=0pt, anchor=center,
            draw, very thin,scale=0.8,transform shape},
        column sep=0pt,
        row sep=0pt,
        inner sep=0.5\pgflinewidth, outer sep=1pt,
        draw, thick},
    empty node/.style = {draw,fill=gray},
    every label/.append style = {font=\large, text=teal}
}
\makeatletter
\def\tikz@lib@matrix@empty@cell{%
    \iftikz@lib@matrix@empty%
    \node[name=\tikzmatrixname-\the\pgfmatrixcurrentrow-\the\pgfmatrixcurrentcolumn,empty node]{};\fi}
\makeatother

\usepackage{bm}
\newcommand{\MatrixVariable}[1]{\bm{{#1}}}



\definecolor{blue}{RGB}{180, 171, 250}


\begin{document}

    \begin{tikzpicture}[node distance=0pt,scale=0.6,nodes={transform
        shape}]
    \matrix (m1) [mymatrix,
    label={[rotate=90, anchor=south,black]left: Movies},
    label={[black]below:$\MatrixVariable{{R}}$},
    label={[black]above:Users},
    nodes={fill=blue}]
    {
        1   &   & 3 &   &   & 5 &   &   & 5 &   & 4 &   \\
        &   & 5 & 4 &   &   & 4 &   &   & 2 & 1 & 3 \\
        2   & 4 &   & 1 & 2 &   & 3 &   & 4 & 3 & 5 &   \\
        & 2 & 4 &   & 5 &   &   & 4 &   &   & 2 &   \\
        &   & 4 & 3 & 4 & 2 &   &   &   &   & 2 & 5 \\
        1   &   & 3 &   & 3 &   &   & 2 &   &   & 4 &   \\
    };
    \node (approx) [right=of m1,scale=2] {$\approx$};
    \end{tikzpicture}

\end{document}

要得到:

在此处输入图片描述

笔记:三倍的RGB={250,250,250}结果将是白色。

相关内容