5

I want to generate hidden markov models using tikz. I have successfully generated nodes and connected them. But i want to generate dashed lines generating below the node to an empty space like this enter image description here. I tried to make it work using this code:

\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}

But this code doesn't work for me. It generates something like thisenter image description here How do i draw dashed lines from the nodes exactly shown in the figure with nodes rainy and sunny?

  • @Sigur i tried something like this \draw [dashed] (A.west) edge node {$b_{11}$} (A.center) but didn't work. – MarKS Feb 2 '17 at 23:54
  • @Sigur did this \draw [dashed] (A.west) node {$b_{11}$} (A.center);. I could see b11 but this time there was no dashed line. – MarKS Feb 3 '17 at 0:13
  • Oh, sorry, now I understand what you wish. See answer by @HeikoOberdiek. – Sigur Feb 3 '17 at 0:16
6

Like this? The labels are not precise, you can change the labels to whatever you want.

enter image description here

\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}
| improve this answer | |
  • thank you so much @AboAmmar it worked perfectly. I need to go through how the code works but it was really helpful. – MarKS Feb 3 '17 at 0:32
6

The following example positions the node with $b_{11}$ below A.south and adds a dashed vector between A.south and the previously created node.

\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}

Result

A more complicate example:

\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}

Result

| improve this answer | |
  • thank you very much @Heiko it works. But i wonder is it possible for b11 to be near the center of the dashed line? And how do i create angle to draw 2 more dashed lines? Unfortunately, i want exactly like shown in the image. – MarKS Feb 3 '17 at 0:18
  • i wish i could mark 2 answers but thank you for all the help @HeikoOberdiek. – MarKS Feb 3 '17 at 0:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.