向矩阵添加标签和注释

向矩阵添加标签和注释

各位同胞!

我一直在努力画出图中的内容:

在此处输入图片描述

我无法画出指向同一条评论的 3 个箭头。另外还有指向字母“i”和“j”的另外两个箭头。你能帮我一下吗?

这是我目前所做的:

​`\begin{figure}[H]
\centering
\begin{tikzpicture}
\matrix (A) [matrix of math nodes, nodes in empty cells,
nodes={draw, minimum width=8mm, minimum height=5mm, outer sep=0pt, anchor=center},
row sep=-\pgflinewidth, column sep=-\pgflinewidth,
row 3/.style = {nodes={minimum height=8mm}},
row 5/.style = {nodes={minimum height=8mm}},
column 3/.style = {nodes={minimum width=10mm}},
column 4/.style = {nodes={fill=lightgray}},
column 6/.style = {nodes={fill=lightgray}},
column 7/.style = {nodes={minimum width=10mm}},]
{   & & & R & & ? & & &  \\
    & & & |[draw,fill=blue!20]|R & & |[draw,fill=blue!20]|R & & &  \\
    & & & & & & & &  \\
    & & & |[draw,fill=blue!20]|R & & |[draw,fill=blue!20]|R & & &  \\ 
    & & & & & & & &  \\
    & & & ? & & R& & &  \\
    & & & |[draw,fill=blue!20]|R & &|[draw,fill=blue!20]| R& & &  \\};
\foreach \i [count=\xi] in {1,2,\dots,$i$, ,$j$,\dots,$m-1$,$m$}
\node[above, font=\scriptsize] at (A-1-\xi.north) {\i\strut};
\foreach \i [count=\xi] in {1,2,\vdots,$u$,\vdots,$n-1$,$n$}
\node[left, font=\scriptsize] at (A-\xi-1.west) {\i\strut};
\foreach \i in {2,4,7}
\draw[dashed] ([shift={(.5mm,-.5mm)}]A-\i-4.north west) rectangle ([shift={(-.5mm,.5mm)}]A-\i-6.south east);
\end{tikzpicture}
\caption{item based }
\end{figure}`

在此处输入图片描述

看起来一点也不好。另外,我还做了另一个版本,其中我必须计算行之间的相似度。我所要做的就是转置矩阵:

在此处输入图片描述

\begin{figure}[H]
\centering
\begin{tikzpicture}
\matrix (A) [matrix of math nodes, nodes in empty cells,
nodes={draw, minimum width=8mm, minimum height=5mm, outer sep=0pt, anchor=center},
row sep=-\pgflinewidth, column sep=-\pgflinewidth,
row 3/.style = {nodes={minimum height=8mm}},
row 5/.style = {nodes={minimum height=8mm}},
column 3/.style = {nodes={minimum width=10mm}},
column 4/.style = {nodes={fill=lightgray}},
column 6/.style = {nodes={fill=lightgray}},
column 7/.style = {nodes={minimum width=10mm}},]
{   & & & R & & ? & & &  \\
    & & & |[draw,fill=blue!20]|R & & |[draw,fill=blue!20]|R & & &  \\
    & & & & & & & &  \\
    & & & |[draw,fill=blue!20]|R & & |[draw,fill=blue!20]|R & & &  \\ 
    & & & & & & & &  \\
    & & & ? & & R& & &  \\
    & & & |[draw,fill=blue!20]|R & &|[draw,fill=blue!20]| R& & &  \\};
\foreach \i [count=\xi] in {1,2,\dots,$i$, ,$j$,\dots,$m-1$,$m$}
\node[above, font=\scriptsize] at (A-1-\xi.north) {\i\strut};
\foreach \i [count=\xi] in {1,2,\vdots,$u$,\vdots,$n-1$,$n$}
\node[left, font=\scriptsize] at (A-\xi-1.west) {\i\strut};
\foreach \i in {2,4,7}
\draw[dashed] ([shift={(.5mm,-.5mm)}]A-\i-4.north west) rectangle ([shift={(-.5mm,.5mm)}]A-\i-6.south east);
\end{tikzpicture}
\caption{item based }
\end{figure}

在此处输入图片描述

答案1

这是一个可行的选择,使用fitpositioning库,以及顶部箭头的相对坐标。请参阅代码中的注释,询问我是否应该扩展任何内容。

在此处输入图片描述

\documentclass[border=5mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{
matrix,
positioning, % added
fit          % added
}
\begin{document}
\begin{tikzpicture}
\matrix (A) [matrix of math nodes, nodes in empty cells,
nodes={draw, minimum width=8mm, minimum height=5mm, outer sep=0pt, anchor=center},
row sep=-\pgflinewidth, column sep=-\pgflinewidth,
row 3/.style = {nodes={minimum height=8mm}},
row 5/.style = {nodes={minimum height=8mm}},
column 3/.style = {nodes={minimum width=10mm}},
row 4/.style = {nodes={fill=lightgray}},
row 6/.style = {nodes={fill=lightgray}},
column 7/.style = {nodes={minimum width=10mm}},]
{
   & & &  & &  &   \\
   & & &  & &  &   \\
   & & &  & &  &   \\
 |[draw,fill=blue!20]|R  & &|[draw,fill=blue!20]|R &  & & ? & |[draw,fill=blue!20]|R  \\
   & & &  & &  &   \\
 |[draw,fill=blue!20]|R  & &|[draw,fill=blue!20]|R &  & & R & |[draw,fill=blue!20]|R  \\
   & & &  & &  &   \\
   & & &  & &  &   \\
   & & &  & &  &   \\
};

% draw the box below
\node [draw,
       text width=3cm,
       below=of A,
       name=textbox
       ] {Lorem ipsum dolor sit amet etc. etc. ad infinitum};


\foreach \i [count=\xi] in {1,2,\vdots,$i$, ,$j$,\vdots,$m-1$,$m$}
    \node[left, font=\scriptsize,name=l-\xi] at (A-\xi-1.west) {\i\strut};
\foreach \i [count=\xi] in {1,2,\dots,$u$,\dots,$n-1$,$n$}
   \node[above, font=\scriptsize] at (A-1-\xi.north) {\i\strut};

\foreach \i in {1,3,7}
    {
    % use the fit library instead of drawing box manually, then you get a node you can refer to
    \node [fit=(A-4-\i)(A-6-\i), inner sep=-0.5mm, draw, dashed, name=R\i] {};
    % draw arrow from textbox node
    \draw [<-] (R\i.south) -- (textbox);
    }


% draw arrows from the nodes using the names defined with the modification of the loop above
% and relative coordinates. Add helper coordinate on the first arrow 
\draw [<-] (l-4) -- coordinate[pos=0.6] (s1) ++(-15mm,0);
\draw [<-] (l-6) -- ++(-15mm,0);
% draw arrow between the helper coordinate s1 and the point that has the x-coordinate of t-6 and the y-coordinate of s1
\draw [<->] (s1) -- node[fill=white,font=\scriptsize,inner sep=1pt] {$s_{i,j}=?$}  (l-6 -| s1);

\end{tikzpicture}
\begin{tikzpicture}
\matrix (A) [matrix of math nodes, nodes in empty cells,
nodes={draw, minimum width=8mm, minimum height=5mm, outer sep=0pt, anchor=center},
row sep=-\pgflinewidth, column sep=-\pgflinewidth,
row 3/.style = {nodes={minimum height=8mm}},
row 5/.style = {nodes={minimum height=8mm}},
column 3/.style = {nodes={minimum width=10mm}},
column 4/.style = {nodes={fill=lightgray}},
column 6/.style = {nodes={fill=lightgray}},
column 7/.style = {nodes={minimum width=10mm}},]
{   & & & R & & ? & & &  \\
    & & & |[draw,fill=blue!20]|R & & |[draw,fill=blue!20]|R & & &  \\
    & & & & & & & &  \\
    & & & |[draw,fill=blue!20]|R & & |[draw,fill=blue!20]|R & & &  \\ 
    & & & & & & & &  \\
    & & & ? & & R& & &  \\
    & & & |[draw,fill=blue!20]|R & &|[draw,fill=blue!20]| R& & &  \\};

% draw the box on the right
\node [draw,
       text width=3cm,
       right=of A,
       name=textbox
       ] {Lorem ipsum dolor sit amet etc. etc. ad infinitum};


\foreach \i [count=\xi] in {1,2,\dots,$i$, ,$j$,\dots,$m-1$,$m$}
    % added name=t-\xi to the following
    \node[above, font=\scriptsize, name=t-\xi] at (A-1-\xi.north) {\i\strut};
\foreach \i [count=\xi] in {1,2,\vdots,$u$,\vdots,$n-1$,$n$}
    \node[left, font=\scriptsize] at (A-\xi-1.west) {\i\strut};
\foreach \i in {2,4,7}
    {
    % use the fit library instead of drawing box manually, then you get a node you can refer to
    \node [fit=(A-\i-4)(A-\i-6), inner sep=-0.5mm, draw, dashed, name=R\i] {};
    % draw arrow from textbox node
    \draw [<-] (R\i.east) -- (textbox);
    }


% draw arrows from the nodes using the names defined with the modification of the loop above
% and relative coordinates. Add helper coordinate on the first arrow 
\draw [<-] (t-4) -- coordinate[pos=0.6] (s1) ++(0,15mm);
\draw [<-] (t-6) -- ++(0,15mm);
% draw arrow between the helper coordinate s1 and the point that has the x-coordinate of t-6 and the y-coordinate of s1
\draw [<->] (s1) -- node[fill=white,font=\scriptsize,inner sep=1pt] {$s_{i,j}=?$}  (t-6 |- s1);

\end{tikzpicture}

\end{document}

答案2

与 Torbjørn T 的回答非常相似,只是评论的边框与您的图中一样(并通过绘制path picture,即,如果您更频繁地使用这样的东西,您可以将其设为一种样式)。

\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{matrix,fit,positioning}

\begin{document}
\begin{tikzpicture}
\matrix (A) [matrix of math nodes, nodes in empty cells,
nodes={draw, minimum width=8mm, minimum height=5mm, outer sep=0pt, 
anchor=center},row sep=-\pgflinewidth, column sep=-\pgflinewidth,
row 3/.style = {nodes={minimum height=8mm}},
row 5/.style = {nodes={minimum height=8mm}},
column 3/.style = {nodes={minimum width=10mm}},
column 4/.style = {nodes={fill=lightgray}},
column 6/.style = {nodes={fill=lightgray}},
column 7/.style = {nodes={minimum width=10mm}},]
{   & & & R & & ? & & &  \\
    & & & |[draw,fill=blue!20]|R & & |[draw,fill=blue!20]|R & & &  \\
    & & & & & & & &  \\
    & & & |[draw,fill=blue!20]|R & & |[draw,fill=blue!20]|R & & &  \\ 
    & & & & & & & &  \\
    & & & ? & & R& & &  \\
    & & & |[draw,fill=blue!20]|R & &|[draw,fill=blue!20]| R& & &  \\};
\foreach \i [count=\xi] in {1,2,\dots,$i$, ,$j$,\dots,$m-1$,$m$}
\node[above, font=\scriptsize] at (A-1-\xi.north) {\i\strut};
\foreach \i [count=\xi] in {1,2,\vdots,$u$,\vdots,$n-1$,$n$}
\node[left, font=\scriptsize] at (A-\xi-1.west) {\i\strut};
\node[right=1cm of A,align=left,text width=4cm,path picture={
\draw ([xshift=1em,yshift=-\pgflinewidth]path picture bounding box.north west) -|
 ([xshift=\pgflinewidth,yshift=\pgflinewidth]path picture bounding box.south west) -- ++ (1em,0);}] (txt){%
Some very long text about ducks, koalas and marmots which is very long and deals
with ducks, koalas and marmots. \dots}; 
\foreach \i  in {2,4,7}
{\node[draw,rounded corners,fit=(A-\i-4)(A-\i-6),inner xsep=-1ex,
inner ysep=-0.3ex] (F-\i){};
\draw[-latex] (txt) -- (F-\i.east);
}

\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容