我正在寻找一种在常规 tikzpicture 中创建图例的方法。我发现了这个
在普通的 tikzpicture 中使用 pgfplots 样式的图例
但我不知道
- 如何将图例放在想要的位置以及
- 如何添加仅包含数字而不是符号的条目,例如
'1 number of ...'
。
我不需要这个。我只想轻松创建一个框,里面有彩色线条、彩色圆圈/矩形……还有彩色数字和旁边的描述。
谁能帮我?
答案1
这是一个基于的解决方案在普通的 tikzpicture 中使用 pgfplots 样式的图例。有两个例子:第一个例子中,图例中的描述是居中的,而第二个例子中,图例中的描述是左对齐的。
为了定义图例中的数字,我添加了一个键number in legend
:
% definition to insert numbers
\pgfkeys{/pgfplots/number in legend/.style={%
/pgfplots/legend image code/.code={%
\node at (0.295,0){#1};
},%
},
}
居中描述
这个例子:
\documentclass[a4paper,11pt]{article}
\usepackage{tikz}
\usetikzlibrary{calc,shadings}
\usepackage{pgfplots}
% Code from Christian Feuersänger
% https://tex.stackexchange.com/questions/54794/using-a-pgfplots-style-legend-in-a-plain-old-tikzpicture#54834
% argument #1: any options
\newenvironment{customlegend}[1][]{%
\begingroup
% inits/clears the lists (which might be populated from previous
% axes):
\csname pgfplots@init@cleared@structures\endcsname
\pgfplotsset{#1}%
}{%
% draws the legend:
\csname pgfplots@createlegend\endcsname
\endgroup
}%
% makes \addlegendimage available (typically only available within an
% axis environment):
\def\addlegendimage{\csname pgfplots@addlegendimage\endcsname}
%%--------------------------------
% definition to insert numbers
\pgfkeys{/pgfplots/number in legend/.style={%
/pgfplots/legend image code/.code={%
\node at (0.295,-0.0225){#1};
},%
},
}
\begin{document}
\begin{tikzpicture}
\draw (0,0)--(10,0);
\draw(0,-0.2)--(0,0.2)node[above,red]{\small{0}};
\draw(10,-0.2)--(10,0.2)node[above,blue]{\small{1}};
%------------------%
\foreach \m[count=\mi] in {0.909090909,...,9.090909090}
\draw(\m,-0.1)--(\m,0.1) node[anchor=north](a\mi){};
\draw[dotted]($(a2)+(0,0.1)$)--($(a2)+(0,0.4)$);
\draw[dotted]($(a3)+(0,0.1)$)--($(a3)+(0,0.4)$);
\draw[stealth-stealth,red,opacity=0.4]($(a3)+(0,0.4)$)--($(a2)+(0,0.4)$)node[anchor=south west,xshift=0.23cm,black,opacity=1]{\small{$R$}};
%------------------%
\foreach \x/\xtext in {0.7/$u_1$,1.2/$u_2$,4.5/$u_3$,5.2/$u_4$,5.6/$u_5$,7.5/$u_6$}
\shade[ball color=blue] (\x,0 ) circle (0.1cm) node[below=0.1cm]{\small{\xtext}};
%------------------%
% inf user
\shade[ball color=green] (0,0 ) circle (0.1cm) node[below=0.1cm]{\small{$u_{inf}$}};
\begin{customlegend}[
legend entries={ % <= in the following there are the entries
$R$: infection radius,
$u_i$: users,
$u_{inf}$: initial infected user,
social space max dimension,
social space min dimension,
example of rectangle
},
legend style={at={(4.5,3.5)},font=\footnotesize}] % <= to define position and font legend
% the following are the "images" and numbers in the legend
\addlegendimage{stealth-stealth,red,opacity=0.4}
\addlegendimage{mark=ball,draw=white}
\addlegendimage{mark=ball,ball color=green,draw=white}
\addlegendimage{number in legend=1,blue}
\addlegendimage{number in legend=0,red}
\addlegendimage{area legend,orange,fill=orange!20}
\end{customlegend}
\end{tikzpicture}
\end{document}
这使:
左对齐描述
代码如下:
\documentclass[a4paper,11pt]{article}
\usepackage{tikz}
\usetikzlibrary{calc,shadings}
\usepackage{pgfplots}
% Code from Christian Feuersänger
% https://tex.stackexchange.com/questions/54794/using-a-pgfplots-style-legend-in-a-plain-old-tikzpicture#54834
% argument #1: any options
\newenvironment{customlegend}[1][]{%
\begingroup
% inits/clears the lists (which might be populated from previous
% axes):
\csname pgfplots@init@cleared@structures\endcsname
\pgfplotsset{#1}%
}{%
% draws the legend:
\csname pgfplots@createlegend\endcsname
\endgroup
}%
% makes \addlegendimage available (typically only available within an
% axis environment):
\def\addlegendimage{\csname pgfplots@addlegendimage\endcsname}
%%--------------------------------
% definition to insert numbers
\pgfkeys{/pgfplots/number in legend/.style={%
/pgfplots/legend image code/.code={%
\node at (0.125,-0.0225){#1}; % <= changed x value
},%
},
}
\pgfplotsset{
every legend to name picture/.style={west}
}
\begin{document}
\begin{tikzpicture}
\draw (0,0)--(10,0);
\draw(0,-0.2)--(0,0.2)node[above,red]{\small{0}};
\draw(10,-0.2)--(10,0.2)node[above,blue]{\small{1}};
%------------------%
\foreach \m[count=\mi] in {0.909090909,...,9.090909090}
\draw(\m,-0.1)--(\m,0.1) node[anchor=north](a\mi){};
\draw[dotted]($(a2)+(0,0.1)$)--($(a2)+(0,0.4)$);
\draw[dotted]($(a3)+(0,0.1)$)--($(a3)+(0,0.4)$);
\draw[stealth-stealth,red,opacity=0.4]($(a3)+(0,0.4)$)--($(a2)+(0,0.4)$)node[anchor=south west,xshift=0.23cm,black,opacity=1]{\small{$R$}};
%------------------%
\foreach \x/\xtext in {0.7/$u_1$,1.2/$u_2$,4.5/$u_3$,5.2/$u_4$,5.6/$u_5$,7.5/$u_6$}
\shade[ball color=blue] (\x,0 ) circle (0.1cm) node[below=0.1cm]{\small{\xtext}};
%------------------%
% inf user
\shade[ball color=green] (0,0 ) circle (0.1cm) node[below=0.1cm]{\small{$u_{inf}$}};
\begin{customlegend}[legend cell align=left, %<= to align cells
legend entries={ % <= in the following there are the entries
$R$: infection radius,
$u_i$: users,
$u_{inf}$: initial infected user,
social space max dimension,
social space min dimension,
example of rectangle
},
legend style={at={(4.5,3.5)},font=\footnotesize}] % <= to define position and font legend
% the following are the "images" and numbers in the legend
\addlegendimage{stealth-stealth,red,opacity=0.4}
\addlegendimage{mark=ball,draw=white}
\addlegendimage{mark=ball,ball color=green,draw=white}
\addlegendimage{number in legend=1,blue}
\addlegendimage{number in legend=0,red}
\addlegendimage{area legend,orange,fill=orange!20}
\end{customlegend}
\end{tikzpicture}
\end{document}
结果: