PGFPLOTS 转置图例,图例条目为空

PGFPLOTS 转置图例,图例条目为空

我在将图例条目正确放置在 3x3 形的转置图例矩阵中时遇到了问题,其中以下 MWE 的条目为空(跳过):

\documentclass{standalone}

\usepackage{pgfplots}
\pgfplotsset{compat=1.16}

\begin{document}

  \begin{tikzpicture}
    \begin{axis}[%
      legend columns = 3,
      transpose legend,
    ]

    \addplot {-x+1};\addlegendentry{A}
    \addplot {-x+2};\addlegendentry{B}
    \addplot {-x+3};\addlegendentry{C}
    \addplot {-x+4};\addlegendentry{D}
    \addplot {-x+5};\addlegendentry{E}
    \addlegendimage{empty legend}\addlegendentry{skip me}
    \addplot {-x+7};\addlegendentry{F}
    %\addlegendimage{empty legend}\addlegendentry{skip me}
    %\addlegendimage{empty legend}\addlegendentry{skip me}

    \end{axis}
  \end{tikzpicture}
\end{document}

我得到的是:

失败的例子

但我期望的是:

A   D   F
B   E
C

我发现通过取消注释最后两empty legend行可以得到预期的结果:

\documentclass{standalone}

\usepackage{pgfplots}
\pgfplotsset{compat=1.16}

\begin{document}

  \begin{tikzpicture}
    \begin{axis}[%
      legend columns = 3,
      transpose legend,
    ]

    \addplot {-x+1};\addlegendentry{A}
    \addplot {-x+2};\addlegendentry{B}
    \addplot {-x+3};\addlegendentry{C}
    \addplot {-x+4};\addlegendentry{D}
    \addplot {-x+5};\addlegendentry{E}
    \addlegendimage{empty legend}\addlegendentry{skip me}
    \addplot {-x+7};\addlegendentry{F}
    \addlegendimage{empty legend}\addlegendentry{skip me} % <-- Uncommented
    \addlegendimage{empty legend}\addlegendentry{skip me} % <-- Uncommented

    \end{axis}
  \end{tikzpicture}
\end{document}

这样,我得到了预期的结果:

工作示例

虽然我找到了最初问题的解决方案,但我想知道这是否是预期的行为!?我在PGFPLOTS 文档

答案1

如果这符合您的要求,请勾选左侧的复选标记

在此处输入图片描述

\documentclass{standalone}

\usepackage{pgfplots}
\pgfplotsset{compat=1.16}

\begin{document}

  \begin{tikzpicture}
    \begin{axis}[%
      legend columns = 3,
      transpose legend,
    ]

    \addplot {-x+1};\addlegendentry{A}
    \addplot {-x+2};\addlegendentry{B}
    \addplot {-x+3};\addlegendentry{C}
    \addplot {-x+4};\addlegendentry{D}
    \addplot {-x+5};\addlegendentry{E}
    \addlegendimage{empty legend}\addlegendentry{}
    \addplot {-x+7};\addlegendentry{F}
    \addlegendimage{empty legend}\addlegendentry{} % <-- Uncommented
    \addlegendimage{empty legend}\addlegendentry{} % <-- Uncommented

    \end{axis}
  \end{tikzpicture}
\end{document}

请记住您还必须通过这种方式指定图例的锚点:

legend style={at={(0.03,0.5)},anchor=west}

锚点定义图例框的哪个点将放置在您用 定义的坐标处at={(<>,<>)}

如果仅使用at={(<>,<>)}插入的坐标,则该坐标是轴框的坐标,其中点(0,0)是左下角和(1,1)右上角。

如果您使用,则at={(axis cs:<>,<>)}指定轴的实际坐标,与您的图相同。

例子

legend style={at={(axis cs:0.5,1)},anchor=south west}

或者

\begin{axis}[legend pos=north west]

或者

\begin{axis}[legend style={at={(0.5,-0.1)},anchor=north}]

或者

\begin{axis}
[%
    axis lines = middle,
    xlabel = $x$,
    ylabel = {$f(x)$},
    enlarge y limits=upper,
    yticklabel style = {font=\tiny},
    xticklabel style = {font=\tiny},
    legend style={xshift=1.5cm},
    thick,
]%

默认位置是north east

相关内容