我正在尝试设计一个类似图片的图例。它有两列,每列都有标题文本。此外,我如何控制第一列中的哪个项目以及第二列中的哪个项目?
\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.6}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
legend columns=2,
legend style={
% the /tikz/ prefix is necessary here...
% otherwise, it might end-up with `/pgfplots/column 2`
% which is not what we want. compare pgfmanual.pdf
/tikz/column 2/.style={
column sep=5pt,
},
},
]
\addplot {x};
\addlegendentry{ }
\addplot {x+4};
\addlegendentry{$x$+4}
\addplot {2*x};
\addlegendentry{ }
\addplot {2*x+4};
\addlegendentry{$2x$+4}
\addplot {4*x};
\addlegendentry{ }
\addplot {4*x+4};
\addlegendentry{$4x$+4}
\end{axis}
\end{tikzpicture}
\end{document}
答案1
在下面的解决方案中,我使用了作者本人在在 pgfplot 中的图例中添加自定义条目以及在 pgfplots 中正确对齐基线上的垂直文本这使:
我已将图例单元格与左侧对齐,并将图例放在东南方,但您当然可以根据需要进行更改:)
% arara: pdflatex
% !arara: indent: {overwrite: yes}
\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.6}
% https://tex.stackexchange.com/questions/84127/correctly-align-vertical-text-on-a-baseline-in-pgfplots
\def\mystrut{\vphantom{hg}}
% https://tex.stackexchange.com/questions/204395/add-custom-entry-into-legend-in-pgfplot
\pgfplotsset{
legend image with text/.style={
legend image code/.code={%
\node[anchor=center] at (0.3cm,0cm) {#1};
}
},
}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
legend columns=2,
legend style={
font=\mystrut,
legend cell align=left,
},
legend pos=south east,
]
% column 1 heading
\addlegendimage{legend image with text=$ax$}
\addlegendentry{}
% column 2 heading
\addlegendimage{legend image with text=$ax+4$}
\addlegendentry{}
% the plots of the functions follow
\addplot {x};
\addlegendentry{}
\addplot {x+4};
\addlegendentry{$x+4$}
\addplot {2*x};
\addlegendentry{}
\addplot {2*x+4};
\addlegendentry{$2x+4$}
\addplot {4*x};
\addlegendentry{}
\addplot {4*x+4};
\addlegendentry{$4x+4$}
\end{axis}
\end{tikzpicture}
\end{document}
答案2
可能有更简单的方法可以做到这一点,但这肯定是最适合的。(别忘了运行两次。)
\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.6}
\begin{document}
\begin{tikzpicture}
\begin{axis}[name=boundary]
\addplot {x};\label{pgfplots:c1r1}
\addplot {x+4};\label{pgfplots:c2r1}
\addplot {2*x};\label{pgfplots:c1r2}
\addplot {2*x+4};\label{pgfplots:c2r2}
\addplot {4*x};\label{pgfplots:c1r3}
\addplot {4*x+4};\label{pgfplots:c2r3}
\end{axis}
\node[draw,fill=white,inner sep=0pt,above left=0.5em] at (boundary.south east) {\small
\begin{tabular}{ccl}
$ax$ & $ax+4$ \\
\ref{pgfplots:c1r1} & \ref{pgfplots:c2r1} & $a=1$\\
\ref{pgfplots:c1r2} & \ref{pgfplots:c2r2} & $a=2$\\
\ref{pgfplots:c1r3} & \ref{pgfplots:c2r3} & $a=4$
\end{tabular}};
\end{tikzpicture}
\end{document}
答案3
该解决方案与以下解决方案非常相似约翰·科米洛但使用\matrix
相应库中的节点来生成自定义图例。
(本回答改编自这重复问题答案。
% used PGFPlots v1.14
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
% load `matrix' library so we can use the `matrix of nodes' feature
\usetikzlibrary{
matrix,
}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
% just because it looks a bit better than the default
cycle list name=exotic,
]
\addplot {x}; \label{plot:line1}
\addplot {x+4}; \label{plot:line2}
\addplot {2*x}; \label{plot:line3}
\addplot {2*x+4}; \label{plot:line4}
\addplot {4*x}; \label{plot:line5}
\addplot {4*x+4}; \label{plot:line6}
% create a (dummy) coordinate where we want to place the legend
%
% (The matrix cannot be placed inside the `axis' environment
% directly, because then a catcode error is raised.
% I guess that this is caused by the `matrix of nodes' feature)
\coordinate (legend) at (axis description cs:0.97,0.03);
\end{axis}
% create the legend matrix which is placed at the created (dummy) coordinate
% and recall the plot specification using the `\ref' command
%
% adapt the style of that node to your needs
% (e.g. if you like different spacings between the rows or columns
% or a fill color)
\matrix [
draw,
matrix of nodes,
anchor=south east,
] at (legend) {
$ax$ & $ax+4$ & \\
\ref{plot:line1} & \ref{plot:line2} & $a=1$ \\
\ref{plot:line3} & \ref{plot:line4} & $a=2$ \\
\ref{plot:line5} & \ref{plot:line6} & $a=4$ \\
};
\end{tikzpicture}
\end{document}