如何使图例条目x
跨度并在整个第一行居中,同时继续在后续的行和列中打印其余条目?
\RequirePackage{luatex85}
\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
legend style={
at={([yshift=30pt]0.5,1)},anchor=north,
text width=1in,
cells={align=left},
minimum height=2mm
},
legend columns=2,
xmin=0, xmax=10,
ymin=0, ymax=10,
]
\addplot [red, line width = 5pt, domain=0:10] {x};
\addlegendentry{$x$}
%
\addplot [black, line width = 5pt, domain=0:10] {(x-5)^2};
\addlegendentry{$(x-5)^2$}
%
\addplot [yellow, line width = 5pt, domain=0:10] {10-(x-5)^2}; \addlegendentry{$10-(x-5)^2$}
%
\addplot [orange, line width = 5pt, domain=0:10] {10-x};
\addlegendentry{$10-x$}
%
\addplot [green, line width = 5pt, domain=0:10] {5};
\addlegendentry{5}
\end{axis}
\end{tikzpicture}
\end{document}
答案1
使用\label
和\ref
可以提取单个图例条目(仅限图像)并使用表格构建您自己的图例。这基本上就是 pgfplots 所做的,只有一个人可以通过\multicolumn
这种方式添加。
请注意,我没有尝试重现您的定位,假设您确实不希望它与您的情节重叠。
\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\begin{document}
\begin{tikzpicture}
\begin{axis}[name=border,
xmin=0, xmax=10,
ymin=0, ymax=10,
]
\addplot [red, line width = 5pt, domain=0:10] {x};
\label{legend1}
%
\addplot [black, line width = 5pt, domain=0:10] {(x-5)^2};
\label{legend2}
%
\addplot [yellow, line width = 5pt, domain=0:10] {10-(x-5)^2};
\label{legend3}
%
\addplot [orange, line width = 5pt, domain=0:10] {10-x};
\label{legend4}
%
\addplot [green, line width = 5pt, domain=0:10] {5};
\label{legend5}
\end{axis}
\node[draw,above=2pt] at (border.north) {\begin{tabular}{ll}
\multicolumn{2}{c}{\ref{legend1} $x$} \\
\ref{legend2} $(x-5)^2$ & \ref{legend3} $10-(x-5)^2$ \\
\ref{legend4} $10-x$ & \ref{legend5} 5
\end{tabular}};
\end{tikzpicture}
\end{document}