TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have the following code:

\pgfplotstableread{ % Read the data into a table macro
Label   First   Second  Third
test      30     30     40

    xbar stacked,   % Stacked horizontal bars
    xmin=0,         % Start x axis at 0
    xmax = 100.01,
     xticklabels={0, 0\%, 20\%,40\%,60\%,80\%,100\%},%
    ytick=data,     % Use as many tick labels as y coordinates
    yticklabels from table={\datatable}{Label}  % Get the labels from the Label column of the \datatable
\addplot [fill=yellow] table [x=First, y expr=\coordindex] {\datatable};    % Plot the "First" column against the data index
\addplot [fill=green!70!blue]table [x=Second, y expr=\coordindex] {\datatable};
\addplot [fill=red!80!yellow] table [x=Third, y expr=\coordindex] {\datatable};
\legend{test1, test2, test3}

enter image description here

Can someone please explain why the "colour-boxes" do not show for each legend, and help me add them?

It seems to be working if I make the bar chart vertical, but I would like to keep it horizontal.

Thank you very much for any help!

share|improve this question

This looks like a bug in PGFPlots: The legend image code for the xbar stacked plot type is defined incorrectly. If you put the following code snippet in your preamble, the legend is displayed correctly:

    single xbar legend/.style={
        legend image code/.code={\draw[##1,/tikz/.cd,bar width=6pt,bar shift=0pt,xbar] plot coordinates {(0.8em,0pt)};},

share|improve this answer
Wow! Thank you so much for the answer. It works:-) – Martha Marie Jun 6 '13 at 22:56
whoops, while reading this answer I accidentally clicked on the downvote button and didn't notice it until now... I'm very sorry, to compensate I upvoted another random answer of yours. – domenico Jul 26 '13 at 16:38
@dcmst: Haha, no problem! For the future: You can undo a downvote by clicking the downvote button again, but that only works for a short time (5 minutes, I think?) – Jake Jul 26 '13 at 16:57
yes, unfortunately I noticed only 44 minutes later... – domenico Jul 26 '13 at 16:58

Your Answer


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.