使用 Tikz 绘制马尔可夫链

使用 Tikz 绘制马尔可夫链

我想使用 tikz 绘制以下马尔可夫链,但我有几个问题不知道如何处理:我希望过渡网格看起来与图片完全一样。所有传出边都是平行的,所有传入边也是如此。在每个交叉点处,必须有一个箭头,如图所示,标签出现在交叉点上方。此外,有些边不连续,中间有点。 马尔可夫链-手绘

以下是我迄今为止所解决的问题,但它非常不完整,存在许多我不知道如何解决的问题。

\documentclass[12pt]{article}

\usepackage[landscape]{geometry}
\usepackage{tikz}
\usetikzlibrary{arrows,automata}

\begin{document}

\begin{tikzpicture}[->,>=stealth',shorten >=1pt,node
     distance=2cm,looseness=2,bend right=40]

\draw[help lines] (-1,-3) grid (16,1);

\tikzset{every state/.style={minimum size=35pt,scale=0.6}}
\node [state] (0)                    {$0$};
\node [state] (1)   [right of = 0]   {$1$};
\node [state] (2)   [right of = 1]   {$2$};
\node         (d1)  [right of = 2]   {$\cdots$};
\node [state] (km1) [right of = d1]  {$k-1$};
\node [state] (k)   [right of = km1] {$k$};
\node [state] (kp1) [right of = k]   {$k+1$};
\node         (d2)  [right of = kp1] {$\cdots$};
\node [state] (mmk) [right of = d2]  {$m-k$};
\node         (d3)  [right of = mmk] {$\cdots$};
\node [state] (mm2) [right of = d3]  {$m-2$};
\node [state] (mm1) [right of = mm2] {$m-1$};

\path (0)   edge (1)
            edge (2)
            edge (km1)
            edge [loop above] node {$\mu_0$} (0);
\path (1)   edge (2)
            edge (km1)
            edge (k)
            edge [loop above] node {$\mu_0$} (1);
\path (2)   edge (km1)
            edge (k)
            edge (kp1)
            edge [loop above] node {$\mu_0$} (2);
\path (km1) edge (k)
            edge (kp1)
            edge [loop above] node {$\mu_0$} (km1);
\path (k)   edge (kp1)
            edge [loop above] node {$\mu_0$} (k);
\path (mmk) edge (mm2)
            edge (mm1)
            edge [loop above] node {$0$} (mmk);
\path (mm2) edge (mm1)
            edge [loop above] node {$0$} (mm2);
\path (mm1) edge [loop above] node {$1$} (mm1);
\end{tikzpicture}

\end{document}

答案1

我的解决方案的诀窍是使用scoperotate=45然后将所有链接都用|-。我还定义了三个命令来分解代码。

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows}

\newcommand\reflexive[2]{% node, label
  \draw[->] (#1.100) to[reflexive] node[trans,above=-2mm] {#2} (#1.80); 
}
\newcommand\link[3]{% start node, end node, label
  \draw[edge] (#1) -- (#1 |- #2);
  \node[trans] at (#1 |- #2) {#3} (1);
  \draw[edge] (#1 |- #2) -- (#2);
}
\newcommand\linkwithdots[4]{% start node, end node, dots node, label
  \coordinate (dots1) at (#1 |- #3);
  \draw (#1) -- ([yshift=5mm]dots1);
  \draw[dotted] ([yshift=5mm]dots1) -- ([yshift=-5mm]dots1);
  \draw[edge] ([yshift=-5mm]dots1) -- (#1 |- #2);
  \node[trans] at (#1 |- #2) {#4} (1);
  \coordinate (dots2) at (#3 |- #2);
  \draw (#1 |- #2) -- ([xshift=-5mm]dots2);
  \draw[dotted] ([xshift=-5mm]dots2) -- ([xshift=5mm]dots2);
  \draw[edge] ([xshift=5mm]dots2) -- (#2);
}
\begin{document}
\begin{tikzpicture}
  \tikzset{
    >=stealth',
    node distance=1.5cm,
    state/.style={minimum size=30pt,font=\small,circle,draw},
    dots/.style={state,draw=none},
    edge/.style={->},
    trans/.style={font=\footnotesize,above=2mm},
    reflexive/.style={out=120,in=60,looseness=5,relative},
  }
  \node [state] (0)                    {$0$};
  \node [state] (1)   [right of = 0]   {$1$};
  \node [state] (2)   [right of = 1]   {$2$};
  \node [dots]  (d1)  [right of = 2]   {$\cdots$};
  \node [state] (km1) [right of = d1]  {$k-1$};
  \node [state] (k)   [right of = km1] {$k$};
  \node [state] (kp1) [right of = k]   {$k+1$};
  \node [dots]  (d2)  [right of = kp1] {$\cdots$};
  \node [state] (mmk) [right of = d2]  {$m-k$};
  \node [dots]  (d3)  [right of = mmk] {$\cdots$};
  \node [state] (mm2) [right of = d3]  {$m-2$};
  \node [state] (mm1) [right of = mm2] {$m-1$};

  \begin{scope}[rotate=45]
    \reflexive{0}{$\mu_0$}
    \reflexive{1}{$\mu_0$}
    \reflexive{2}{$\mu_0$}
    \link{0}{1}{$\mu_1$}
    \link{0}{2}{$\mu_1$}
    \link{1}{2}{$\mu_1$}
    \linkwithdots{0}{km1}{d1}{$\mu_{k-1}$}
    \linkwithdots{1}{km1}{d1}{$\mu_{k-2}$}
    \linkwithdots{2}{km1}{d1}{$\mu_{k-3}$}
  \end{scope}
\end{tikzpicture}
\end{document}

在此处输入图片描述

现在,您只需完成一些链接......

相关内容