向马尔可夫链添加图例

向马尔可夫链添加图例

我正在尝试向使用 Tikz 库创建的马尔可夫链添加图例。图片和代码已附上。我检查了一些可能的解决方案,但没有成功。如果有人能帮忙,我将不胜感激。

在此处输入图片描述

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

答案1

如果你只是添加

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

你得到

在此处输入图片描述

但是,由于你反复使用和标记这些样式,我会给它们起一个名字,然后这样做

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

在此处输入图片描述

我还将您的箭头替换为可能更有吸引力的latex,但如果您不喜欢,只需删除即可>=latex

答案2

您可以像这样手动定义箭头

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

在此处输入图片描述

相关内容