4

I am trying to add a legend to a Markov chain I created using Tikz library. The picture and code are attached. I checked some possible solutions but they didn't work out. I would appreciate if someone can help on this.

enter image description here

 \documentclass[tikz]{standalone}
 \usetikzlibrary{positioning}
 \tikzset{state/.style={circle,draw=black}}
    \begin{document}
        \begin{tikzpicture}[transform shape, scale = 0.75, thick,node distance=1.1cm, text centered, text width = 1.45em]
        [dotnode/.style={dotted, draw=black, line width=2}]
        %\tikzstyle{every state}=[fill=none,text=black]
        \node[state] (1)  [fill = lightgray!30!white]                           {\tiny (1)};
        \node[state] (2)  [right=of 1, fill = lightgray!30!white]           {\tiny (2)};
        \node[state] (3)  [right=of 2, fill = lightgray!30!white]           {\tiny (3)};
        \node[state] (4)  [right=of 3, fill = lightgray]            {\tiny (4)};
        \node[state] (5)  [right=of 4, fill = lightgray]            {\tiny  (5)};
        \node[state] (6)  [right=of 5, fill = lightgray]            {\tiny  (6)};
        \node[state] (7)  [below=2cm of 4]        {\tiny  (7)};
        \node[state] (8)  [below=2cm of 5]        {\tiny  (8)};
        \node[state] (9)  [below=2cm of 6]        {\tiny (9)};
        \node[state] (10) [above right= 3.4cm of 2]  {\tiny (10)};
        \draw[every loop, line width = 0.32mm]
        (1) edge[loop above, blue, dashed] node {} (1)
        (2) edge[loop above, blue, dashed] node {} (2)
        (3) edge[loop above, blue, dashed] node {} (3)
        (4) edge[loop above, violet] node {} (4)
        (5) edge[loop above, violet] node {} (5)
        (6) edge[loop above, violet] node {} (6)
        (7) edge[loop right, green!30!black, dotted] node {} (7)
        (8) edge[loop right, green!30!black, dotted] node {} (8)
        (9) edge[loop right, green!30!black, dotted] node {} (9)
        (10) edge[loop right, green!30!black, dotted] node {} (10)
        (1) edge[bend left,looseness=0, blue, dashed]  node {} (2)
        (2) edge[bend left,looseness=0, blue, dashed]  node {} (1)
        (2) edge[bend left,looseness=0, blue, dashed]  node {} (3)
        (3) edge[bend left,looseness=0, blue, dashed]  node {} (2)
        (3) edge[bend left,looseness=0, blue, dashed] node {} (4)
        (4) edge[bend left,looseness=0, violet] node {} (3)
        (4) edge[bend left,looseness=0, violet] node {} (5)
        (5) edge[bend left,looseness=0, violet] node {} (4)
        (5) edge[violet]                        node {} (6)
        (2) edge[bend left=55, blue, dashed]  node {} (4)
        (3) edge[bend left=55, blue, dashed]  node {} (5)
        (3) edge[bend left=40,looseness=1, blue, dashed]  node {} (1)
        (4) edge[bend left=45,looseness=1, violet]  node {} (1)
        (4) edge[bend left=40,looseness=1, violet]  node {} (2)
        (5) edge[bend left=50,looseness=1, violet]  node {} (1)
        (5) edge[bend left=45,looseness=1, violet]  node {} (2)
        (5) edge[bend left=40,looseness=1, violet]  node {} (3)
        (4) edge[red, dash dot]             node {} (7)
        (5) edge[red, dash dot]             node {} (8)
        (6) edge[red, dash dot]             node {} (9)
        (1) edge[green!30!black, dotted]             node {} (10)
        (2) edge[green!30!black, dotted]             node {} (10)
        (3) edge[green!30!black, dotted]             node {} (10)
        (4) edge[green!30!black, dotted]             node {} (10)
        (5) edge[green!30!black, dotted]             node {} (10)
        (6) edge[green!30!black, dotted]             node {} (10)   ;
    \end{tikzpicture}
    \end{document}
  • I just made you code compilable. Could you edit in order to have your implementation of \tikzset{state/.style={<style options>} ? – BambOo Sep 7 '18 at 16:35
4

You could define the arrows manually like so

\draw [->,green!30!black, dotted] (0,-3)--++(1,0) node [right,text=black] {A};
\draw [->,blue, dashed] (0,-3.5)--++(1,0) node [right,text=black] {B};
\draw [->,violet] (0,-4)--++(1,0) node [right,text=black] {C};
\draw [->,red, dash dot] (0,-4.5)--++(1,0) node [right,text=black] {D};

enter image description here

4

If you just add

    \foreach \X/\Y [count=\Z] in {green!30!black/A,blue/B,red/C,violet/D}
    {\draw[\X,->] (0,-2-0.5*\Z) -- (1,-2-0.5*\Z) node[right]{\Y};
    }

you get

enter image description here

However, since you are using and labeling these styles over and over, I would give them a name and just do

\documentclass[tikz]{standalone}
 \usetikzlibrary{positioning}
 \tikzset{state/.style={circle,draw=black}}
    \begin{document}
        \begin{tikzpicture}[transform shape, scale = 0.75, thick,node distance=1.1cm, text centered, text width = 1.45em,
        dotnode/.style={dotted, draw=black, line width=2},
        A/.style={dotted,green!30!black},
        B/.style={dashed,blue},
        C/.style={dash dot,red},
        D/.style={violet},>=latex]
        %\tikzstyle{every state}=[fill=none,text=black]
        \node[state] (1)  [fill = lightgray!30!white]                           {\tiny (1)};
        \node[state] (2)  [right=of 1, fill = lightgray!30!white]           {\tiny (2)};
        \node[state] (3)  [right=of 2, fill = lightgray!30!white]           {\tiny (3)};
        \node[state] (4)  [right=of 3, fill = lightgray]            {\tiny (4)};
        \node[state] (5)  [right=of 4, fill = lightgray]            {\tiny  (5)};
        \node[state] (6)  [right=of 5, fill = lightgray]            {\tiny  (6)};
        \node[state] (7)  [below=2cm of 4]        {\tiny  (7)};
        \node[state] (8)  [below=2cm of 5]        {\tiny  (8)};
        \node[state] (9)  [below=2cm of 6]        {\tiny (9)};
        \node[state] (10) [above right= 3.4cm of 2]  {\tiny (10)};
        \draw[every loop, line width = 0.32mm]
        (1) edge[loop above, B] node {} (1)
        (2) edge[loop above, B] node {} (2)
        (3) edge[loop above, B] node {} (3)
        (4) edge[loop above, D] node {} (4)
        (5) edge[loop above, D] node {} (5)
        (6) edge[loop above, D] node {} (6)
        (7) edge[loop right, A] node {} (7)
        (8) edge[loop right, A] node {} (8)
        (9) edge[loop right, A] node {} (9)
        (10) edge[loop right, A] node {} (10)
        (1) edge[bend left,looseness=0, B]  node {} (2)
        (2) edge[bend left,looseness=0, B]  node {} (1)
        (2) edge[bend left,looseness=0, B]  node {} (3)
        (3) edge[bend left,looseness=0, B]  node {} (2)
        (3) edge[bend left,looseness=0, B] node {} (4)
        (4) edge[bend left,looseness=0, D] node {} (3)
        (4) edge[bend left,looseness=0, D] node {} (5)
        (5) edge[bend left,looseness=0, D] node {} (4)
        (5) edge[D]                        node {} (6)
        (2) edge[bend left=55, B]  node {} (4)
        (3) edge[bend left=55, B]  node {} (5)
        (3) edge[bend left=40,looseness=1, B]  node {} (1)
        (4) edge[bend left=45,looseness=1, D]  node {} (1)
        (4) edge[bend left=40,looseness=1, D]  node {} (2)
        (5) edge[bend left=50,looseness=1, D]  node {} (1)
        (5) edge[bend left=45,looseness=1, D]  node {} (2)
        (5) edge[bend left=40,looseness=1, D]  node {} (3)
        (4) edge[C]             node {} (7)
        (5) edge[C]             node {} (8)
        (6) edge[C]             node {} (9)
        (1) edge[A]             node {} (10)
        (2) edge[A]             node {} (10)
        (3) edge[A]             node {} (10)
        (4) edge[A]             node {} (10)
        (5) edge[A]             node {} (10)
        (6) edge[A]             node {} (10)   ;
        \foreach \X/\Y [count=\Z] in
        {A/{A1,A2,A3,A4},B/{B1,B2,B3,B4},C/{C1,C2,C3,C4},D/{D1,D2,D3,D4}}
        {\draw[\X,->] (0,-2-0.5*\Z) -- (1,-2-0.5*\Z) node[right]{\Y};
        }
    \end{tikzpicture}
    \end{document}

enter image description here

I also replaced your arrow head by the arguably a bit more appealing latex, but if you do not like that, just remove >=latex.

  • Thanks marmot. This is really helpful. It is exactly what I wanted except that instead of A, I have a1, a2, a3, similarly instead of B, I have to write b1, b2, b3 etc. Same goes with C and D. The way that you have defined them in a set Z, does not allow me to manipulate it to make them as I wish to have. Kindly let me know, how this is possible. – M. DA Sep 10 '18 at 11:29
  • @M.Ebadi I do not understand. Do you mean you have a1, a2, a3 instead of A, B, C? If so, you could just replace A, B, C by a1, a2, a3. – user121799 Sep 10 '18 at 11:32
  • Not exactly, instead of A, I have to write A1, A2, A3 and A4. similarly instead of B, I have to write B1, B2, B3 and B4. Basically, there can be more than one "letter" as legends. – M. DA Sep 10 '18 at 11:43
  • 1
    @M.Ebadi That's easy. I made an update. Note that, if these entries always have the form A1,A2,A3,A4, you could also generate them in loops, but maybe doing this here would be a bit of an overkill. – user121799 Sep 10 '18 at 11:56
  • 1
    @M.Ebadi If the answer works for, could you perhaps consider accepting it by clicking on the check mark left of it? – user121799 Sep 10 '18 at 12: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.