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'm trying to change the spacing of a single legend entry in a PGF bar plot. This is my MWE:

\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\begin{document}
  \begin{tikzpicture}
    \begin{axis}[ybar]
        \addplot coordinates {(1,3) (2,5) (3,2)};
        \addlegendentry{(a)}
        \addplot coordinates {(1,2) (2,6) (3,1)};
        \addlegendentry{(b)}
        \addplot coordinates {(1,4) (2,4) (3,4)};
        \addlegendentry{(c)}
    \end{axis}
  \end{tikzpicture}
\end{document}

which generates the following figure

Minimal Working Example for this question

What I want is to create a space between (a) and (b) in the legend, while not creating a space between (b) and (c) (that is, I don't want to change the row sep). The ideia is to separate (a) from (b) and (c) in order to highlight that (a) is the result of a work and (b) and (c) are the result of another work in a presentation.

I tried to create a custom legend using \label and \ref, but this changes the bars (in the legend) to lines.

share|improve this question

2 Answers 2

up vote 3 down vote accepted

You can adjust the spacing of the legend entry by including text depth=2ex, anchor=mid in the \addlegendentry options:

\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\begin{document}
  \begin{tikzpicture}
    \begin{axis}[ybar]
        \addplot coordinates {(1,3) (2,5) (3,2)};
        \addlegendentry[text depth=2ex, anchor=mid]{(a)}
        \addplot coordinates {(1,2) (2,6) (3,1)};
        \addlegendentry{(b)}
        \addplot coordinates {(1,4) (2,4) (3,4)};
        \addlegendentry{(c)}
    \end{axis}
  \end{tikzpicture}
\end{document}
share|improve this answer
    
This works, but it seams that conflicts with every axis legend/.append style={nodes={right}} for some reason (which is not in my MWH, but it is in my real code). But I changed to every axis legend/.append style={nodes={left}} and obtained the desired result. –  Rafael Schouery Feb 6 at 10:59
1  
@RafaelSchouery: In that case, you can use anchor=mid west instead of anchor=mid. –  Jake Feb 6 at 11:02

Not the best solution, but you could add an empty legend entry with

\addlegendimage{empty legend}
\addlegendentry{}

The spacing can be increased by adding a zero width rule in the legend entry, e.g.

\addlegendentry{\rule{0pt}{3pt}}

Change 3pt to modify the spacing.

enter image description here

\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\begin{document}
  \begin{tikzpicture}
    \begin{axis}[ybar]
        \addplot coordinates {(1,3) (2,5) (3,2)};
        \addlegendentry{(a)}
        \addlegendimage{empty legend}
        \addlegendentry{}
        \addplot coordinates {(1,2) (2,6) (3,1)};
        \addlegendentry{(b)}
        \addplot coordinates {(1,4) (2,4) (3,4)};
        \addlegendentry{(c)}
    \end{axis}
  \end{tikzpicture}
\end{document}
share|improve this answer
    
But I can't adjust the spacing, right? –  Rafael Schouery Feb 6 at 11:00
    
@RafaelSchouery You can, to a certain extent at least, by adding a zero width rule in the legend entry. I'll update my answer. Edit: Jake's answer is of course much better anyway. –  Torbjørn T. Feb 6 at 11:54

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.