我正在编写一个文档,它几乎不需要实际的图,我对 tikz 的美感很满意。但是,我很难找到在 tikz 上制作简单图例的方法。我知道数据可视化库有一种方法,但它对于简单的图表来说似乎太复杂了。有些人建议使用矩阵,结果证明这几乎就是我想要的,如下所示:
\documentclass[11pt]{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\draw[->] (-1,0) -- (8,0) node[right]{$x$};
\draw[->] (0,-2) -- (0,2) node[above]{$y$};
\draw[green,samples=100,domain=-1:8] plot(\x,{sin(deg(\x))});
\draw[red,samples=100,domain=-1:8] plot(\x,{cos(deg(\x))});
\draw[blue] (0,0)--(pi/2,1)--(3*pi/2,-1)--(5*pi/2,1);
\matrix [draw, above left] at (8,-2) {
\node[green,font=\tiny] {$\sin x$}; \\
\node[red,font=\tiny] {$\cos x$}; \\
\node[blue,font=\tiny] {Lines}; \\
};
\end{tikzpicture}
\end{document}
但是,我希望图例能够显示图形的绘制样式。就像我们在 tikz 手册上看到的那样:
我可以用矩阵来实现吗?我还想让图例左对齐,现在它右对齐了,但不知道如何更改。
答案1
我也更喜欢 pfgplots,但为了完整性,这里有一种使用pic
s 的方法。
\documentclass[11pt]{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[pics/legend entry/.style={code={%
\draw[pic actions]
(-0.5,0.25) sin (-0.25,0.4) cos (0,0.25) sin (0.25,0.1) cos (0.5,0.25);}}]
\draw[->] (-1,0) -- (8,0) node[right]{$x$};
\draw[->] (0,-2) -- (0,2) node[above]{$y$};
\draw[green!70!black,samples=100,domain=-1:8] plot(\x,{sin(deg(\x))});
\draw[red,samples=100,domain=-1:8] plot(\x,{cos(deg(\x))});
\draw[blue] (0,0)--(pi/2,1)--(3*pi/2,-1)--(5*pi/2,1);
\matrix [draw, above left] at (8,-2) {
\pic[green!70!black]{legend entry}; & \node[green!70!black,font=\tiny] {$\sin x$}; \\
\pic[red]{legend entry}; & \node[red,font=\tiny] {$\cos x$}; \\
\pic[blue]{legend entry}; & \node[blue,font=\tiny] {Lines}; \\
};
\end{tikzpicture}
\end{document}
要对齐文本节点,您可以使用这些技巧。
\documentclass[11pt]{article}
\usepackage{tikz}
\usepackage{eqparbox}
\begin{document}
\newbox\eqnodebox
\tikzset{lequal size/.style={execute at begin
node={\setbox\eqnodebox=\hbox\bgroup},
execute at end node={\egroup\eqmakebox[#1][l]{\copy\eqnodebox}}},
lequal size/.default=A,}
\begin{tikzpicture}[pics/legend entry/.style={code={%
\draw[pic actions]
(-0.5,0.25) sin (-0.25,0.4) cos (0,0.25) sin (0.25,0.1) cos (0.5,0.25);}}]
\draw[->] (-1,0) -- (8,0) node[right]{$x$};
\draw[->] (0,-2) -- (0,2) node[above]{$y$};
\draw[green!70!black,samples=100,domain=-1:8] plot(\x,{sin(deg(\x))});
\draw[red,samples=100,domain=-1:8] plot(\x,{cos(deg(\x))});
\draw[blue] (0,0)--(pi/2,1)--(3*pi/2,-1)--(5*pi/2,1);
\matrix [draw, above left] at (8,-2) {
\pic[green!70!black]{legend entry}; & \node[lequal size,green!70!black,font=\tiny] {$\sin x$}; \\
\pic[red]{legend entry}; & \node[lequal size,red,font=\tiny] {$\cos x$}; \\
\pic[blue]{legend entry}; & \node[lequal size,blue,font=\tiny] {Lines}; \\
};
\end{tikzpicture}
\end{document}
答案2
为了获得一致的外观,我建议不要使用纯 TikZ 来绘图,而是使用在 TikZ 之上构建的 PGFPlot。
\documentclass[11pt]{article}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[%
samples=100,
domain=-1:8,
xmin=-1, xmax=8,
ymin=-2, ymax=2,
axis lines=middle,
ticks=none,
xlabel={$x$},
ylabel={$y$},
legend pos=south east,
width=\textwidth,
height=0.5*\textwidth]
\addplot[green] {sin(deg(\x))};
\addplot[red] {cos(deg(\x))};
\addplot[blue] coordinates{(0,0) (pi/2,1) (3*pi/2,-1) (5*pi/2,1)};
\addlegendentry{$sin(x)$}
\addlegendentry{$cos(x)$}
\addlegendentry{Lines}
\end{axis};
\end{tikzpicture}
\end{document}