TikZ 图片:如何添加图例?

TikZ 图片:如何添加图例?

我使用 tikzpicture 环境创建了以下图表: enter image description here

使用以下 MWE:

%Begin with any document class:
\documentclass{standalone}
%Load the tkz-graph package:
\usepackage{tkz-graph}

%Begin the document:
\begin{document}

%Start a tikzpicture environment:
\begin{tikzpicture}
%[options](node ID) at (position) {Label}
\node[shape=circle, draw=black, line width=2](FD) at (0,0) {FD};
\node[shape=circle, draw=blue, line width=2](TD) at (0,-3.5) {TD};
\draw [->] (FD) to (TD);
\node[shape=circle, draw=blue, line width=2](ND) at (2,-3) {ND};
\draw [->] (FD) to (ND);
\node[shape=circle, draw=blue, line width=2](RUD) at (-2,-3) {RUD};
\draw [->] (FD) to (RUD);
\node[shape=circle, draw=blue, line width=2](OD) at (-4,-3) {OD};
\draw [->] (FD) to (OD);
\node[shape=circle, draw=green, line width=2](ODEA) at (-2.5,-5.5) {$OD_{EA}$};
\draw [->] (OD) to (ODEA);
\node[shape=circle, draw=blue, line width=2](ODK) at (-4,-7) {$OD_{K}$};
\draw [->] (OD) to (ODK);
\node[shape=circle, draw=blue, line width=2](POD) at (-5.5,-5.5) {POD};
\draw [->] (OD) to (POD);
\node[shape=circle, draw=blue, line width=2](OFD) at (-7,-5) {OFD};
\draw [->] (OD) to (OFD);
\node[shape=circle, draw=blue, line width=2](preFD) at (-9,-5) {preFD};
\draw [->] (OFD) to (preFD);
\node[shape=circle, draw=blue, line width=2](SD) at (-7,-3) {SD};
\draw [->] (OD) to (SD);
\node[shape=circle, draw=green, line width=2](CSD) at (-9,-3) {CSD};
\draw [->] (SD) to (CSD);
\node[shape=circle, draw=green, line width=2](PAC) at (-4,-1) {PAC};
\draw [->] (FD) to (PAC);
\node[shape=circle, draw=blue, line width=2](NuD) at (-7,0) {NuD};
\draw [->] (FD) to (NuD);

\node[shape=circle, draw=blue, line width=2](FFD) at (4,-3) {FFD};
\draw [->] (FD) to (FFD);
\node[shape=circle, draw=blue, line width=2](TMFD) at (4,-5) {TMFD};
\draw [->] (FFD) to (TMFD);
\node[shape=circle, draw=blue, line width=2](SFD) at (7,-5) {SFD};
\draw [->] (FFD) to (SFD);
\node[shape=circle, draw=blue, line width=2](MD) at (5,-2) {MD};
\draw [->] (FD) to (MD);
\node[shape=circle, draw=green, line width=2](CMD) at (7,-3.5) {CMD};
\draw [->] (MD) to (CMD);
\node[shape=circle, draw=blue, line width=2](CoD) at (7,-2) {CoD};
\draw [->] (MD) to (CoD);
\node[shape=circle, draw=green, line width=2](ACoD) at (9,-2) {ACoD};
\draw [->] (CoD) to (ACoD);
\node[shape=circle, draw=blue, line width=2](MFD) at (5,0) {MFD};
\draw [->] (FD) to (MFD);
\draw [->] (MFD) to (CoD);
\node[shape=circle, draw=blue, line width=2](DD) at (7,0) {DD};
\draw [->] (MFD) to (DD);
\node[shape=circle, draw=green, line width=2](ADD) at (9,0) {ADD};
\draw [->] (DD) to (ADD);

\node[shape=circle, draw=green, line width=2](A0XFD) at (-4,1.5) {$\alpha0XFD$};
\draw [->] (FD) to (A0XFD);
\node[shape=circle, draw=red, line width=2](XCFD) at (-4.5,3.5) {XCFD};
\draw [->] (FD) to (XCFD);
\node[shape=circle, draw=green, line width=2](CFDC) at (-7,5) {$CFD^C$};
\draw [->] (NuD) to (CFDC);
\node[shape=circle, draw=red, line width=2](CFD) at (-3,5) {CFD};
\draw [->] (CFD) to (CFDC);
\draw [->] (FD) to (CFD);
\node[shape=circle, draw=red, line width=2](CFDP) at (-5,7) {$CFD^P$};
\draw [->] (CFD) to (CFDP);
\node[shape=circle, draw=red, line width=2](ECFD) at (-3,7) {eCFD};
\draw [->] (CFD) to (ECFD);
\node[shape=circle, draw=red, line width=2](CD) at (-1,5) {CD};
\draw [->] (FD) to (CD);
\node[shape=circle, draw=red, line width=2](PD) at (1,5) {PD};
\draw [->] (FD) to (PD);
\node[shape=circle, draw=red, line width=2](softFD) at (3,5) {softFD};
\draw [->] (FD) to (softFD);
\node[shape=circle, draw=red, line width=2](pFD) at (2,7) {pFD};
\draw [->] (PD) to (pFD);
\draw [->] (softFD) to (pFD);
\node[shape=circle, draw=red, line width=2](PuD) at (4.5,3.5) {PuD};
\draw [->] (FD) to (PuD);
\node[shape=circle, draw=red, line width=2](AFD) at (4,1.5) {AFD};
\draw [->] (FD) to (AFD);

\end{tikzpicture}
\end{document}

我怎样才能创建这样的图例: enter image description here 并将其放在右上角?

答案1

我要做的第一件事是为各种类型定义一些样式,这使得更改事物变得更加容易(更改样式,而不是更改每个节点):

\begin{tikzpicture}[
  blacknode/.style={shape=circle, draw=black, line width=2},
  bluenode/.style={shape=circle, draw=blue, line width=2},
  greennode/.style={shape=circle, draw=green, line width=2},
  rednode/.style={shape=circle, draw=red, line width=2}
]

要生成图例,您可以例如使用,每行\matrix一个,并使用为节点生成图例文本:\nodelabel

\matrix [draw,below left] at (current bounding box.north east) {
  \node [blacknode,label=right:Foo] {}; \\
  \node [bluenode,label=right:Bar] {}; \\
  \node [greennode,label=right:Baz] {}; \\
  \node [rednode,label=right:Foobar] {}; \\
};

节点current bounding box是任何一点都包含整个图表的节点,因此锚点north east位于图表的右上方,就像在该点一样。我将图例放在右上方,因为那里有更多空间。

output of code

\documentclass[border=5mm]{standalone}
%Load the tkz-graph package:
\usepackage{tkz-graph}

%Begin the document:
\begin{document}

%Start a tikzpicture environment:
\begin{tikzpicture}[
  blacknode/.style={shape=circle, draw=black, line width=2},
  bluenode/.style={shape=circle, draw=blue, line width=2},
  greennode/.style={shape=circle, draw=green, line width=2},
  rednode/.style={shape=circle, draw=red, line width=2}
]
%[options](node ID) at (position) {Label}
\node[blacknode](FD) at (0,0) {FD};
\node[bluenode](TD) at (0,-3.5) {TD};
\draw [->] (FD) to (TD);
\node[bluenode](ND) at (2,-3) {ND};
\draw [->] (FD) to (ND);
\node[bluenode](RUD) at (-2,-3) {RUD};
\draw [->] (FD) to (RUD);
\node[bluenode](OD) at (-4,-3) {OD};
\draw [->] (FD) to (OD);
\node[greennode](ODEA) at (-2.5,-5.5) {$OD_{EA}$};
\draw [->] (OD) to (ODEA);
\node[bluenode](ODK) at (-4,-7) {$OD_{K}$};
\draw [->] (OD) to (ODK);
\node[bluenode](POD) at (-5.5,-5.5) {POD};
\draw [->] (OD) to (POD);
\node[bluenode](OFD) at (-7,-5) {OFD};
\draw [->] (OD) to (OFD);
\node[bluenode](preFD) at (-9,-5) {preFD};
\draw [->] (OFD) to (preFD);
\node[bluenode](SD) at (-7,-3) {SD};
\draw [->] (OD) to (SD);
\node[greennode](CSD) at (-9,-3) {CSD};
\draw [->] (SD) to (CSD);
\node[greennode](PAC) at (-4,-1) {PAC};
\draw [->] (FD) to (PAC);
\node[bluenode](NuD) at (-7,0) {NuD};
\draw [->] (FD) to (NuD);

\node[bluenode](FFD) at (4,-3) {FFD};
\draw [->] (FD) to (FFD);
\node[bluenode](TMFD) at (4,-5) {TMFD};
\draw [->] (FFD) to (TMFD);
\node[bluenode](SFD) at (7,-5) {SFD};
\draw [->] (FFD) to (SFD);
\node[bluenode](MD) at (5,-2) {MD};
\draw [->] (FD) to (MD);
\node[greennode](CMD) at (7,-3.5) {CMD};
\draw [->] (MD) to (CMD);
\node[bluenode](CoD) at (7,-2) {CoD};
\draw [->] (MD) to (CoD);
\node[greennode](ACoD) at (9,-2) {ACoD};
\draw [->] (CoD) to (ACoD);
\node[bluenode](MFD) at (5,0) {MFD};
\draw [->] (FD) to (MFD);
\draw [->] (MFD) to (CoD);
\node[bluenode](DD) at (7,0) {DD};
\draw [->] (MFD) to (DD);
\node[greennode](ADD) at (9,0) {ADD};
\draw [->] (DD) to (ADD);

\node[greennode](A0XFD) at (-4,1.5) {$\alpha0XFD$};
\draw [->] (FD) to (A0XFD);
\node[rednode](XCFD) at (-4.5,3.5) {XCFD};
\draw [->] (FD) to (XCFD);
\node[greennode](CFDC) at (-7,5) {$CFD^C$};
\draw [->] (NuD) to (CFDC);
\node[rednode](CFD) at (-3,5) {CFD};
\draw [->] (CFD) to (CFDC);
\draw [->] (FD) to (CFD);
\node[rednode](CFDP) at (-5,7) {$CFD^P$};
\draw [->] (CFD) to (CFDP);
\node[rednode](ECFD) at (-3,7) {eCFD};
\draw [->] (CFD) to (ECFD);
\node[rednode](CD) at (-1,5) {CD};
\draw [->] (FD) to (CD);
\node[rednode](PD) at (1,5) {PD};
\draw [->] (FD) to (PD);
\node[rednode](softFD) at (3,5) {softFD};
\draw [->] (FD) to (softFD);
\node[rednode](pFD) at (2,7) {pFD};
\draw [->] (PD) to (pFD);
\draw [->] (softFD) to (pFD);
\node[rednode](PuD) at (4.5,3.5) {PuD};
\draw [->] (FD) to (PuD);
\node[rednode](AFD) at (4,1.5) {AFD};
\draw [->] (FD) to (AFD);


\matrix [draw,below left] at (current bounding box.north east) {
  \node [blacknode,label=right:Foo] {}; \\
  \node [bluenode,label=right:Bar] {}; \\
  \node [greennode,label=right:Baz] {}; \\
  \node [rednode,label=right:Foobar] {}; \\
};
\end{tikzpicture}
\end{document}

相关内容