![如何用 TikZ 创建图例?](https://linux22.com/image/417806/%E5%A6%82%E4%BD%95%E7%94%A8%20TikZ%20%E5%88%9B%E5%BB%BA%E5%9B%BE%E4%BE%8B%EF%BC%9F.png)
我想知道如何创建如图所示的颜色图例。
\definecolor{BrightGreen}{rgb}{0.4, 1.0, 0.0}
\begin{tikzpicture}[scale = 0.7]
\tikzset{roundnode/.style = {thick, draw = black, fill = black, outer sep = 1.5, circle, minimum size = 2pt, scale = 0.75}}
%%%%%%%%% Nodes %%%%%%%%%
\node[roundnode = black, label = left: {$s$}] (s) at (2, 9){};
\node[roundnode = black, label = left: {$t$}] (t) at (2, 7){};
\node[roundnode = black, label = left: {$C_1$}] (c1) at (2, 5){};
\node[roundnode = black, label = left: {$C_2$}] (c2) at (2, 3){};
\node[roundnode = black, label = left: {$C_3$}] (c3) at (2, 1){};
\node[roundnode = black, label = right: {$v_1$}] (v1) at (10, 8){};
\node[roundnode = black, label = right: {$v_2$}] (v2) at (10, 6){};
\node[roundnode = black, label = right: {$v_3$}] (v3) at (10, 4){};
\node[roundnode = black, label = right: {$v_4$}] (v4) at (10, 2){};
%%%%%%%%% Edges %%%%%%%%%
\draw[Cerulean, very thick] (s) to node[left, above, sloped]{}(v1);
\draw[Cerulean, very thick] (s) to node[left, above, sloped]{}(v2);
\draw[Cerulean, very thick] (s) to node[left, above, sloped]{}(v3);
\draw[Cerulean, very thick] (s) to node[left, above, sloped]{}(v4);
\draw[Red, very thick] (t) to node[left, above, sloped]{}(v1);
\draw[Red, very thick] (t) to node[left, above, sloped]{}(v2);
\draw[Red, very thick] (t) to node[left, above, sloped]{}(v3);
\draw[Red, very thick] (t) to node[left, above, sloped]{}(v4);
\draw[Cerulean, very thick] (c1) to node[left, above, sloped]{}(v1);
\draw[Cerulean, very thick] (c1) to node[left, above, sloped]{}(v2);
\draw[Red, very thick] (c1) to node[left, above, sloped]{}(v3);
\draw[Plum, very thick] (c1) to node[left, above, sloped]{}(v4);
\draw[Red, very thick] (c2) to node[left, above, sloped]{}(v1);
\draw[Cerulean, very thick] (c2) to node[left, above, sloped]{}(v2);
\draw[Cerulean, very thick] (c2) to node[left, above, sloped]{}(v3);
\draw[BrightGreen, very thick] (c2) to node[left, above, sloped]{}(v4);
\draw[BurntOrange, very thick] (c3) to node[left, above, sloped]{}(v1);
\draw[Cerulean, very thick] (c3) to node[left, above, sloped]{}(v2);
\draw[Cerulean, very thick] (c3) to node[left, above, sloped]{}(v3);
\draw[Red, very thick] (c3) to node[left, above, sloped]{}(v4);
\end{tikzpicture}
答案1
另一种方法是使用\matrix
。
\documentclass[tikz,border=0.5cm]{standalone}
\begin{document}
\definecolor{BrightGreen}{rgb}{0.4, 1.0, 0.0}
\tikzset{
Cerulean/.style={green},
Red/.style={red},
Plum/.style={gray},
BurntOrange/.style={orange},
% pics/legend line/.style={
% /tikz/anchor=center,
% code={
% \draw[yshift=.5ex,thick,#1] (0, 0) -- (0.6, 0);
% }
% },
legend line/.pic={
\draw[yshift=.5ex,thick,#1] (0, 0) -- (0.6, 0);
},
legend area/.pic={
\draw[yshift=.5ex,fill=#1] (0, -0.2) rectangle (0.6, 0.2);
}
}
\begin{tikzpicture}[scale = 0.7]
\tikzset{roundnode/.style = {thick, draw = black, fill = black, outer sep = 1.5, circle, minimum size = 2pt, scale = 0.75}}
%%%%%%%%% Nodes %%%%%%%%%
\node[roundnode = black, label = left: {$s$}] (s) at (2, 9){};
\node[roundnode = black, label = left: {$t$}] (t) at (2, 7){};
\node[roundnode = black, label = left: {$C_1$}] (c1) at (2, 5){};
\node[roundnode = black, label = left: {$C_2$}] (c2) at (2, 3){};
\node[roundnode = black, label = left: {$C_3$}] (c3) at (2, 1){};
\node[roundnode = black, label = right: {$v_1$}] (v1) at (10, 8){};
\node[roundnode = black, label = right: {$v_2$}] (v2) at (10, 6){};
\node[roundnode = black, label = right: {$v_3$}] (v3) at (10, 4){};
\node[roundnode = black, label = right: {$v_4$}] (v4) at (10, 2){};
%%%%%%%%% Edges %%%%%%%%%
\draw[Cerulean, very thick] (s) to node[left, above, sloped]{}(v1);
\draw[Cerulean, very thick] (s) to node[left, above, sloped]{}(v2);
\draw[Cerulean, very thick] (s) to node[left, above, sloped]{}(v3);
\draw[Cerulean, very thick] (s) to node[left, above, sloped]{}(v4);
\draw[Red, very thick] (t) to node[left, above, sloped]{}(v1);
\draw[Red, very thick] (t) to node[left, above, sloped]{}(v2);
\draw[Red, very thick] (t) to node[left, above, sloped]{}(v3);
\draw[Red, very thick] (t) to node[left, above, sloped]{}(v4);
\draw[Cerulean, very thick] (c1) to node[left, above, sloped]{}(v1);
\draw[Cerulean, very thick] (c1) to node[left, above, sloped]{}(v2);
\draw[Red, very thick] (c1) to node[left, above, sloped]{}(v3);
\draw[Plum, very thick] (c1) to node[left, above, sloped]{}(v4);
\draw[Red, very thick] (c2) to node[left, above, sloped]{}(v1);
\draw[Cerulean, very thick] (c2) to node[left, above, sloped]{}(v2);
\draw[Cerulean, very thick] (c2) to node[left, above, sloped]{}(v3);
\draw[BrightGreen, very thick] (c2) to node[left, above, sloped]{}(v4);
\draw[BurntOrange, very thick] (c3) to node[left, above, sloped]{}(v1);
\draw[Cerulean, very thick] (c3) to node[left, above, sloped]{}(v2);
\draw[Cerulean, very thick] (c3) to node[left, above, sloped]{}(v3);
\draw[Red, very thick] (c3) to node[left, above, sloped]{}(v4);
\begin{scope}[xshift=12cm,yshift=2cm]
\end{scope}
\matrix [column sep=2mm, draw, anchor=west, xshift=1cm] at (v2 |- c3){
\pic {legend line=Cerulean}; & \node {$C_1$}; &
\pic {legend line=Red}; & \node {$C_5$}; \\
\pic {legend line=Plum}; & \node {$C_2$}; &
\pic {legend area=orange}; & \node {$C_6$}; \\
\pic {legend line=brown}; & \node {$C_3$}; & &\\
\pic {legend line=blue}; & \node {$C_4$}; & &\\
};
\end{tikzpicture}
\end{document}
答案2
如果您乐意手动格式化所有内容,则可以使用这种方法:
\documentclass[tikz,border=0.5cm]{standalone}
\begin{document}
\definecolor{BrightGreen}{rgb}{0.4, 1.0, 0.0}
\tikzset{Cerulean/.style={green},Red/.style={red},Plum/.style={gray},BurntOrange/.style={orange},legend/.style={right,black}}
\begin{tikzpicture}[scale = 0.7]
\tikzset{roundnode/.style = {thick, draw = black, fill = black, outer sep = 1.5, circle, minimum size = 2pt, scale = 0.75}}
%%%%%%%%% Nodes %%%%%%%%%
\node[roundnode = black, label = left: {$s$}] (s) at (2, 9){};
\node[roundnode = black, label = left: {$t$}] (t) at (2, 7){};
\node[roundnode = black, label = left: {$C_1$}] (c1) at (2, 5){};
\node[roundnode = black, label = left: {$C_2$}] (c2) at (2, 3){};
\node[roundnode = black, label = left: {$C_3$}] (c3) at (2, 1){};
\node[roundnode = black, label = right: {$v_1$}] (v1) at (10, 8){};
\node[roundnode = black, label = right: {$v_2$}] (v2) at (10, 6){};
\node[roundnode = black, label = right: {$v_3$}] (v3) at (10, 4){};
\node[roundnode = black, label = right: {$v_4$}] (v4) at (10, 2){};
%%%%%%%%% Edges %%%%%%%%%
\draw[Cerulean, very thick] (s) to node[left, above, sloped]{}(v1);
\draw[Cerulean, very thick] (s) to node[left, above, sloped]{}(v2);
\draw[Cerulean, very thick] (s) to node[left, above, sloped]{}(v3);
\draw[Cerulean, very thick] (s) to node[left, above, sloped]{}(v4);
\draw[Red, very thick] (t) to node[left, above, sloped]{}(v1);
\draw[Red, very thick] (t) to node[left, above, sloped]{}(v2);
\draw[Red, very thick] (t) to node[left, above, sloped]{}(v3);
\draw[Red, very thick] (t) to node[left, above, sloped]{}(v4);
\draw[Cerulean, very thick] (c1) to node[left, above, sloped]{}(v1);
\draw[Cerulean, very thick] (c1) to node[left, above, sloped]{}(v2);
\draw[Red, very thick] (c1) to node[left, above, sloped]{}(v3);
\draw[Plum, very thick] (c1) to node[left, above, sloped]{}(v4);
\draw[Red, very thick] (c2) to node[left, above, sloped]{}(v1);
\draw[Cerulean, very thick] (c2) to node[left, above, sloped]{}(v2);
\draw[Cerulean, very thick] (c2) to node[left, above, sloped]{}(v3);
\draw[BrightGreen, very thick] (c2) to node[left, above, sloped]{}(v4);
\draw[BurntOrange, very thick] (c3) to node[left, above, sloped]{}(v1);
\draw[Cerulean, very thick] (c3) to node[left, above, sloped]{}(v2);
\draw[Cerulean, very thick] (c3) to node[left, above, sloped]{}(v3);
\draw[Red, very thick] (c3) to node[left, above, sloped]{}(v4);
\begin{scope}[xshift=12cm,yshift=2cm]
\draw[Cerulean] (0,0) -- ++(1,0)node[legend]{$C_1$};
\draw[Red] (0,-0.5) -- ++(1,0)node[legend]{$C_2$};
\draw[Plum] (0,-1.0) -- ++(1,0)node[legend]{$C_3$};
\draw[BurntOrange] (0,-1.5) -- ++(1,0)node[legend]{$C_4$};
\draw[] (2,0) -- ++(1,0)node[legend]{$C_5$};
\node[fill=yellow,draw=black] at (2.5,-0.5) {};
\node[legend] at (3,-0.5) {$C_6$};
\draw (-0.5,0.5) rectangle (4.5,-2);
\end{scope}
\end{tikzpicture}
\end{document}
其结果是:
PGFplots 提供了图例格式化,但您必须通过其机制定义样式。