Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I would like to add a legend header to one of my pgfplot. So far I get that

\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}

which gives me that

enter image description here

I would like to have the second row to be left aligned with respect to the first one. I do not want to fix the number of row because there may be more histograms to come and I would like to have them horizonthally aligned (that's why I use legend columns=-1).

I read here that pgfplots legend is nothing else than tikz matrix so maybe there is a way to x-shift the second row (as I already did with the yshift=10pt command).

share|improve this question
    
So you want some sort of multicolumn matrix? This is unsupported by PGF's \matrix as far as I know. A patch has been proposed in tex.stackexchange.com/questions/1388/… - perhaps that might work here as well –  Christian Feuersänger Nov 10 '12 at 17:15
    
@ChristianFeuersänger: I saw this answer yesterday but I didn't get time to try it. I will edit my question if I can make it work. –  Xavier Garrido Nov 10 '12 at 18:43
    
@ChristianFeuersänger: I didn't manage to make it work but I find another way to do it (see the answer below) –  Xavier Garrido Nov 11 '12 at 10:22

1 Answer 1

up vote 0 down vote accepted

Using legend style I find something a bit crappy but working. Here is a possible solution

\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}

Of course, the column sep=-2.3cm really depends on the text width of the header which may vary from one plot to another.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.