2

I have a problem with a weird offset with horizontal bars in a pgfplots legend. They seem to be moved "up" a bit, so a part of them is hidden under overflow. An image of the situation which explains it quicker:

the picture with legends

Minimal TeX to reproduce (I am compiling with pdflatex):

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
    xbar,
    enlargelimits=0.25,
    legend style={},
    symbolic y coords={A,B,C},
    ytick=data,
    y tick label style={rotate=90},
    ]
\addplot coordinates {(2.5,A) (5.1,B) (5.1,C)};
\addplot coordinates {(7.1,A) (5.4,B) (8.7,C)};
\addplot coordinates {(6.5,A) (5.0,B) (2.0,C)};
\legend{legend1, legend2, legend3}
\end{axis}
\end{tikzpicture}
\end{document}

Q: How do I get the correct image of the horizontal bars? Expected output for me is to have the two vertical bars just like in other bar-style legends from pgfplots, e.g. in the question here: Reason for multiple bars in Legend entries

Q: or is it correct this way? If so, is it possible to modify the "thumbnail" to show something else?

2

Update

With the new pgfplots version 1.13 the code of the OP works as expected:

enter image description here

So if you can use version 1.13 the workaround in the answer below is not needed anymore.


Original answer (used with version 1.12):

As you can see in the documentation or in the pgfplots.code.tex the xbar legend image is defined by

/pgfplots/xbar legend/.style={
  /pgfplots/legend image code/.code={%
    \draw[##1,/tikz/.cd,bar width=3pt,yshift=-0.2em,bar shift=0pt]
    plot coordinates {(0cm,0.8em)(2*\pgfplotbarwidth,0.6em)};}
}

But I think the x and y coordinates should be interchanged:

/pgfplots/xbar legend/.style={
  /pgfplots/legend image code/.code={%
    \draw[##1,/tikz/.cd,bar width=3pt,yshift=-0.2em,bar shift=0pt]
    plot coordinates {(0.8em,0cm)(0.6em,2*\pgfplotbarwidth)};}% <- changed
}

enter image description here

Code:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{pgfplots}
\pgfplotsset{
  /pgfplots/xbar legend/.style={
    /pgfplots/legend image code/.code={%
      \draw[##1,/tikz/.cd,bar width=3pt,yshift=-0.2em,bar shift=0pt]
      plot coordinates {(0.8em,0cm)(0.6em,2*\pgfplotbarwidth)};}% <- changed
  }
}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
    xbar,
    enlargelimits=0.25,
    %legend style={},
    symbolic y coords={A,B,C},
    ytick=data,
    y tick label style={rotate=90}
    ]
\addplot coordinates {(2.5,A) (5.1,B) (5.1,C)};
\addplot coordinates {(7.1,A) (5.4,B) (8.7,C)};
\addplot coordinates {(6.5,A) (5.0,B) (2.0,C)};
\legend{legend1, legend2, legend3}
\end{axis}
\end{tikzpicture}
\end{document}
  • Thanks a lot, works perfectly. I will check if pgfplots already have this fixed upstream and report if not. EDIT you filled it already at sourceforge.net/p/pgfplots/bugs/100 , thanks again :] – exa Dec 26 '15 at 17:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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