添加图例标题

添加图例标题

我想在我的一个 pgfplot 中添加图例标题。到目前为止,我得到了

\documentclass{standalone}
\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}
  \begin{axis}[
    ymin=0,
    ymax=1,
    enlargelimits=false,
    legend pos=north west,
    legend style={area legend},
    legend columns=-1
   ]
   \addlegendimage{empty legend}
   \addlegendentry[yshift=10pt]{Header legend}
   \addplot
     [const plot,fill=blue,draw=black]
     coordinates
     {(0,0.1)    (0.1,0.15)  (0.2,0.5)   (0.3,0.62)
      (0.4,0.56) (0.5,0.58)  (0.6,0.65)  (0.7,0.6)
      (0.8,0.58) (0.9,0.55)  (1,0.52)}
     \closedcycle;
   \addlegendentry{histo 1}
   \addplot
      [const plot,fill=red,draw=black]
     coordinates
     {(0,0.1)    (0.1,0.15)  (0.2,0.5)   (0.3,0.62)
      (0.4,0.56) (0.5,0.58)  (0.6,0.65)  (0.7,0.6)
      (0.8,0.58) (0.9,0.55)  (1,0.52)}
     \closedcycle;
   \addlegendentry{histo 2}
\end{axis}
\end{tikzpicture}
\end{document}

这让我

在此处输入图片描述

我希望第二行相对于第一行左对齐。我不想固定行数,因为可能会有更多的直方图,我希望它们水平对齐(这就是我使用的原因legend columns=-1)。

我读这里pgfplotslegend不过是 tikz 而已,matrix所以也许有办法将第二行 x 移(正如我已经用命令做的那样yshift=10pt)。

答案1

使用legend样式我发现有些东西有点糟糕但有效。这是一个可能的解决方案

\documentclass{standalone}
\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}
  \begin{axis}[
    ymin=0,
    ymax=1,
    enlargelimits=false,
    legend pos=north west,
    legend style={area legend,
      /tikz/row 1 column 2/.append style={column sep=-2.3cm}},
    legend columns=-1
   ]
   \addlegendimage{empty legend}
   \addlegendentry[yshift=10pt]{Header legend}
   \addplot
     [const plot,fill=blue,draw=black]
     coordinates
     {(0,0.1)    (0.1,0.15)  (0.2,0.5)   (0.3,0.62)
      (0.4,0.56) (0.5,0.58)  (0.6,0.65)  (0.7,0.6)
      (0.8,0.58) (0.9,0.55)  (1,0.52)}
     \closedcycle;
   \addlegendentry{histo 1}
   \addplot
      [const plot,fill=red,draw=black]
     coordinates
     {(0,0.1)    (0.1,0.15)  (0.2,0.5)   (0.3,0.62)
      (0.4,0.56) (0.5,0.58)  (0.6,0.65)  (0.7,0.6)
      (0.8,0.58) (0.9,0.55)  (1,0.52)}
     \closedcycle;
   \addlegendentry{histo 2}
  \end{axis}
\end{tikzpicture}
\end{document}

当然,这column sep=-2.3cm实际上取决于标题的文本宽度,因为不同的图表其文本宽度可能会有所不同。

相关内容