如何从节点绘制虚线而不连接到任何其他节点或 tikz 中的形状

如何从节点绘制虚线而不连接到任何其他节点或 tikz 中的形状

我想使用 tikz 生成隐马尔可夫模型。我已经成功生成节点并连接它们。但我想生成虚线,在节点下方生成虚线到空白处,就像这样在此处输入图片描述。我尝试使用以下代码使其工作:

\begin{tikzpicture}[->,>=stealth',shorten >=1pt,auto,node distance=2.8cm,
                semithick,scale=1.5,transform shape]
\tikzstyle{every state}=[fill=red,draw=none,text=white]

\node[state]    (A)                    {$S_1$};
\node[state]    (B) [right of=A]       {$S_2$};


\path (A) edge [loop above] node {$a_{11}$} (A)
          edge [bend left]  node {$a_{12}$} (B)
      (B) edge [loop above] node {$a_{22}$} (B)
          edge [bend left]  node {$a_{21}$} (A);

\draw [dashed] (A) edge [bend below] node {$b_{11}$};
\end{tikzpicture}

但这段代码对我来说不起作用。它生成如下内容在此处输入图片描述 如何从图中精确显示的节点(带有雨天和晴天的节点)绘制虚线?

答案1

像这样吗?标签并不精确,你可以将标签更改为任何你想要的。

在此处输入图片描述

\documentclass[border=2mm,tikz]{standalone}
\usetikzlibrary{arrows,automata,positioning}
\begin{document}

\begin{tikzpicture}[->,>=stealth',shorten >=1pt,node distance=2.8cm,
                semithick,scale=1.5,transform shape]
\tikzstyle{every state}=[fill=red,draw=none,text=white]

\node[state]    (A)                    {$S_1$};
\node[state]    (B) [right of=A]       {$S_2$};


\path[auto] (A) edge [loop above] node {$a_{11}$} (A)
          edge [bend left]  node {$a_{12}$} (B)
      (B) edge [loop above] node {$a_{22}$} (B)
          edge [bend left]  node {$a_{21}$} (A);

\node (01) at ([shift={(-3em,-4em)}]A.south) {$b12$};
\node (04) at ([shift={(0,-5em)}]A.south) {$b11$};
\node (05) at ([shift={(3em,-4em)}]A.south) {$b13$};

\draw [shorten >=0pt,dashed] (A.south) --node[pos=.7,fill=white,inner sep=1pt]{0.4} (04);
\draw [shorten >=0pt,dashed] (A.south) --node[fill=white,inner sep=1pt]{0.1} (01);
\draw [shorten >=0pt,dashed] (A.south) --node[fill=white,inner sep=1pt]{0.5} (05);

\end{tikzpicture}

\end{document}

答案2

下面的示例将节点定位在$b_{11}$下方,并在其和之前创建的节点A.south之间添加一个虚线矢量。A.south

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows}
\tikzset{state/.style={circle, fill=red,text=white}}
\begin{document}
\begin{tikzpicture}[->,>=stealth',shorten >=1pt,auto,node distance=2.8cm,
                semithick,scale=1.5,transform shape]
\tikzstyle{every state}=[fill=red,draw=none,text=white]

\node[state]    (A)                    {$S_1$};
\node[state]    (B) [right of=A]       {$S_2$};


\path (A) edge [loop above] node {$a_{11}$} (A)
          edge [bend left]  node {$a_{12}$} (B)
      (B) edge [loop above] node {$a_{22}$} (B)
          edge [bend left]  node {$a_{21}$} (A);

\draw[dashed, ->, inner sep=0pt] (A.south) +(-90:2em) node (tmp) {$b_{11}$}
  (A.south) -- (tmp);
\end{tikzpicture}
\end{document}

结果

一个更复杂的例子:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows}
\tikzset{state/.style={circle, fill=red,text=white}}
\begin{document}
\begin{tikzpicture}[->,>=stealth',shorten >=1pt,auto,node distance=2.8cm,
                semithick,scale=1.5,transform shape]
\tikzstyle{every state}=[fill=red,draw=none,text=white]

\node[state]    (A)                    {$S_1$};
\node[state]    (B) [right of=A]       {$S_2$};


\path (A) edge [loop above] node {$a_{11}$} (A)
          edge [bend left]  node {$a_{12}$} (B)
      (B) edge [loop above] node {$a_{22}$} (B)
          edge [bend left]  node {$a_{21}$} (A);

\begin{scope}[densely dashed, ->, inner sep=.2em]
  \draw[shorten >=-2pt]
    (A.south) ++(-90:1.7em) node[font=\scriptsize] (tmp1) {0.4}
              ++(-90:1.8em) node[inner sep=0pt]      (tmp2) {$b_{11}$}
    (A.south) -- (tmp1) -- (tmp2);
  \draw[shorten >=4pt]
    (A.south) ++(-45:1.5em) node[font=\scriptsize] (tmp1) {0.5}
              ++(-45:2em)   node[inner sep=0pt]    (tmp2) {$b_{12}$}
    (A.south) -- (tmp1) -- (tmp2);
  \draw[shorten >=-3pt]
    (A.south) ++(-135:1.5em) node[font=\scriptsize] (tmp1) {0.1}
              ++(-135:2em) node[inner sep=0pt]      (tmp2) {$b_{10}$}
    (A.south) -- (tmp1) -- (tmp2);
\end{scope}
\end{tikzpicture}
\end{document}

结果

相关内容