TikZ 矩阵中带有链的线的起点和终点

TikZ 矩阵中带有链的线的起点和终点

我正在尝试可视化马尔可夫链,经过一番研究后,我使用了带有链的矩阵。这很有效,但我无法在我想要的位置开始和终止线条。这是我的代码,首先是主文件:

\documentclass[10pt, conference, letterpaper]{IEEEtran}

\usepackage{tikz}
\usepackage{amsmath}
\usetikzlibrary{arrows,chains,matrix,positioning,scopes,shapes,snakes}
\makeatletter
\tikzset{join/.code=\tikzset{after node path={
\ifx\tikzchainprevious\pgfutil@empty\else(\tikzchainprevious)
edge[every join]#1(\tikzchaincurrent)\fi}}}

\begin{document}
\begin{figure}[h]
\begin{center}
\resizebox{\columnwidth}{!}{
\input{matrix.tikz}
}
\end{center}
\end{figure}
\end{document}

和 tikz 文件(由于某种原因,它不能在一个文件中工作):

\begin{tikzpicture}
\tikzset{>=stealth',every on chain/.append style={join},
         every join/.style={->}}
\tikzstyle{labeled}=[execute at begin node=$\scriptstyle,
   execute at end node=$]
\tikzstyle{state}=[ellipse, fill=white,draw=black,thick,text=black,scale=1]
\tikzstyle{stateinv}=[ellipse, fill=white,draw=white,thick,text=black,scale=1]
\tikzstyle{point}=[circle, fill=white,draw=white,thick,text=white,scale=1,inner sep=0pt,minimum size=0]
  \matrix (m) [matrix of nodes, row sep=3em, column sep=3em,nodes={
        anchor=center
    }]
    { |[point]| & |[point]| & |[point]| & |[point]| & |[point]| & |[point]| \\
      |[point]| & |[state]| $0,0$ & |[state]| $0,1$  & $\ldots$  & |[state]| $0,W_0-2$  & |[state]| $0,W_0-1$ \\
      |[point]| & |[stateinv]| \vdots & |[stateinv]| \vdots & |[stateinv]| \vdots & |[stateinv]| \vdots & |[stateinv]| \vdots \\
      [-1cm]
      |[point]| & |[state]| $i,0$ & |[state]| $i,1$  & $\ldots$  & |[state]| $i,W_i-2$  & |[state]| $i,W_i-1$ \\
      |[point]| & |[state]| $i+1,0$ & |[state]| $i+1,1$  & $\ldots$  & |[state]| $i+1,W_{i+1}-2$  & |[state]| $i+1,W_{i+1}-1$ \\
      [-1cm]
      |[point]| & |[stateinv]| \vdots & |[stateinv]| \vdots & |[stateinv]| \vdots & |[stateinv]| \vdots & |[stateinv]| \vdots \\
      |[point]| & |[state]| $m,0$ & |[state]| $m,1$  & $\ldots$  & |[state]| $m,W_m-2$  & |[state]| $m,W_m-1$ \\};
  \node[draw=none] (P1)[right of=m-3-3,,yshift=0.5cm]  {$\dfrac{p}{W_1}$};
  \node[draw=none] (P2)[right of=m-5-3,,yshift=0.5cm,xshift=1cm] {$\dfrac{p}{W_{i+1}}$};
  \node[draw=none] (P3)[right of=m-7-3,,yshift=0.5cm,xshift=1cm] {$\dfrac{p}{W_m}$};
  { [start chain] \chainin (m-1-1);
     \chainin (m-1-2);
    { [start branch=A] \chainin (m-2-2);}
    { [start branch=A] \chainin (m-2-3);}
    { [start branch=A] \chainin (m-2-5);}
    \chainin (m-2-6);}
  { [start chain] \chainin (m-2-6);
    \chainin (m-2-5) [join={node[above,labeled] {1}}];
    \chainin (m-2-4) [join={node[above,labeled] {1}}];
    \chainin (m-2-3) [join={node[above,labeled] {1}}];
    \chainin (m-2-2) [join={node[above,labeled] {1}}]; 
    \chainin (m-2-1) [join={node[above,labeled] {1-p}}]; }
  { [start chain] \chainin (m-2-2);
    { [start branch=A] \chainin (m-3-2);}
    { [start branch=A] \chainin (m-3-3);}
    { [start branch=A] \chainin (m-3-5);}
    \chainin (m-3-6);}
  { [start chain] \chainin (m-4-6);
    \chainin (m-4-5) [join={node[above,labeled] {1}}];
    \chainin (m-4-4) [join={node[above,labeled] {1}}];
    \chainin (m-4-3) [join={node[above,labeled] {1}}];
    \chainin (m-4-2) [join={node[above,labeled] {1}}];
    \chainin (m-4-1) [join={node[above,labeled] {1-p}}]; }
  { [start chain] \chainin (m-4-2);
    { [start branch=A] \chainin (m-5-2);}
    { [start branch=A] \chainin (m-5-3);}
    { [start branch=A] \chainin (m-5-5);}
    \chainin (m-5-6);}
  { [start chain] \chainin (m-5-6);
    \chainin (m-5-5) [join={node[above,labeled] {1}}];
    \chainin (m-5-4) [join={node[above,labeled] {1}}];
    \chainin (m-5-3) [join={node[above,labeled] {1}}];
    \chainin (m-5-2) [join={node[above,labeled] {1}}];
    \chainin (m-5-1) [join={node[above,labeled] {1-p}}]; }
   { [start chain] \chainin (m-6-2);
    { [start branch=A] \chainin (m-7-2);}
    { [start branch=A] \chainin (m-7-3);}
    { [start branch=A] \chainin (m-7-5);}
    \chainin (m-7-6);}
  { [start chain] \chainin (m-7-6);
    \chainin (m-7-5) [join={node[above,labeled] {1}}];
    \chainin (m-7-4) [join={node[above,labeled] {1}}];
    \chainin (m-7-3) [join={node[above,labeled] {1}}];
    \chainin (m-7-2) [join={node[above,labeled] {1}}];
    \chainin (m-7-1) [join={node[above,labeled] {1-p}}]; }
  { [start chain] \chainin (m-7-1);
    \chainin (m-1-1) [join={node[above,labeled] {1}}]; }
\end{tikzpicture}

最终结果如下: 最终结果

如您所见,一行到下一行的线条从任何地方开始,也从任何地方结束。我想要实现的是以下内容:

  • 起点应始终位于节点的底部中间,以便看起来像第一行。虽然这有点欺骗,因为它起源于一个点。但风格应该是一样的。
  • 线的末端应始终位于节点的顶部中间。

我猜可以在一定程度上使用锚点,因为我见过用路径来实现这一点,但我不确定它如何与链一起使用。由于我对 TikZ 还不太熟悉,所以如果能提供任何解释,我将不胜感激。

如果有任何其他意见可以改进其他部分,我们将不胜感激!

先感谢您!

答案1

我没有看到任何好的方法可以用这个chains库来实现这一点,因为它似乎在处理节点锚点时遇到了麻烦。另一方面,除了使用 已经很容易做到的事情之外,你似乎没有充分利用这个库\draw[->]

此外,虽然您的应用程序有很多节点和边,但其中许多节点和边可以通过算法指定。这是使用该\foreach命令的一个很好的选择!

\documentclass[tikz, border=3pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix, shapes, arrows}
\makeatletter
\DeclareRobustCommand{\rvdots}{%
  \vbox{
    \baselineskip4\p@\lineskiplimit\z@
    \kern-\p@
    \hbox{.}\hbox{.}\hbox{.}
  }}
\makeatother

\begin{document}
\begin{tikzpicture}
  \tikzset{>=stealth'}
  \tikzstyle{state}=[ellipse, draw]
  \tikzstyle{stateinv}=[inner sep=5pt]
  \tikzstyle{point}=[coordinate]
  \matrix (m) [matrix of nodes, row sep=4em, column sep=3em, nodes={anchor=center}]
    { |[point]| & |[point]| \\
      |[point]| & |[state]| $0,0$ & |[state]| $0,1$  & $\ldots$  & |[state]| $0,W_0-2$  & |[state]| $0,W_0-1$ \\
      |[point]| & |[stateinv]| \rvdots & |[stateinv]| \rvdots & |[stateinv]| \rvdots & |[stateinv]| \rvdots & |[stateinv]| \rvdots \\
      [-1cm]
      |[point]| & |[state]| $i,0$ & |[state]| $i,1$  & $\ldots$  & |[state]| $i,W_i-2$  & |[state]| $i,W_i-1$ \\
      |[point]| & |[state]| $i+1,0$ & |[state]| $i+1,1$  & $\ldots$  & |[state]| $i+1,W_{i+1}-2$  & |[state]| $i+1,W_{i+1}-1$ \\
      [-1cm]
      |[point]| & |[stateinv]| \rvdots & |[stateinv]| \rvdots & |[stateinv]| \rvdots & |[stateinv]| \rvdots & |[stateinv]| \rvdots \\
      |[point]| & |[state]| $m,0$ & |[state]| $m,1$  & $\ldots$  & |[state]| $m,W_m-2$  & |[state]| $m,W_m-1$ \\};

  \foreach \i [evaluate=\i as \ii using int(\i+1)] in {1,2,4,6}{
    \foreach \j in {2,3,6}{
      \draw[->] (m-\i-2.south) to (m-\ii-\j.north);
    }
  }
  \draw[->] (m-1-2) -- (m-2-5.north);
  \foreach \i/\label [evaluate=\i as \ii using int(\i+1)] in {2/i,4/i+1,6/m}{
    \draw[->] (m-\i-2.south) to node[below] {$\frac{p}{W_{\label}}$} (m-\ii-5.north);
  }
  \foreach \i in {2,4,5,7}{
    \foreach \j [remember=\j as \jj (initially 6)] in {5,...,2}{
      \draw[->] (m-\i-\jj) -- node[above] {\scriptsize$1$} (m-\i-\j);
    }
    \draw[->] (m-\i-2) -- node[above] {\scriptsize$p-1$} (m-\i-1);
  }
  \draw[->] (m-7-1) -- (m-1-1);
  \draw[->] (m-1-1) -- (m-1-2);
\end{tikzpicture}
\end{document}

马尔可夫链

(归功于这个答案用于\rvdots上下具有相同间距的命令。)

你可能还对绘制曲线边缘感兴趣

\draw[->, out=180, in=90] (m-7-2) to node[left, pos=.2] {\scriptsize$p-1$} (m-2-2);

相关内容