pgfplots:将图例中的一行居中

pgfplots:将图例中的一行居中

对于以下 MWE,我想:

  1. 将长条目的图例图像与其文本置于中心,
  2. 展开图例框以包含其条目,
  3. 并根据图例宽度将第二行条目置于中心。

\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=3,
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{long long long long long entry}
\end{axis}
\end{tikzpicture}

\end{document}

在此处输入图片描述

答案1

这是解决前两个问题或您的愿望的部分答案。

\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=3,
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{\begin{minipage}[b]{1in}
long long long
long long entry\end{minipage}}
\end{axis}
\end{tikzpicture}
\end{document}

在此处输入图片描述

我甚至没有尝试解决第三个问题,因为我认为结果看起来不太好,但这只是我的个人观点。或者,我可能会使用

\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=3,
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{\begin{minipage}[b]{3in}
long long long
long long entry\end{minipage}}
\end{axis}
\end{tikzpicture}
\end{document}

在此处输入图片描述

我认为它看起来足够好了。

相关内容