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 am trying to get pgfplots to draw some bargraph like this:

enter image description here

but I cannot figure out how to get pgfplots to add the second X-axis-labeling so I could actually draw the data in a way that would be easily readable. I checked out the manual but could not quite find anything about it. I've been trying to figure this out quite a while now but I cannot get it to work.

Can somebody give me a minimal example on how I can get this to work?

share|improve this question
Take a look at Two Level Labels in Bar Plot‌​. Would that approach work for you? – Jake Apr 28 '13 at 20:49
Welcome to TeX-SX. Can you give us the part that draws the plot without the second axis to avoid replicating the actual plot? – percusse Apr 28 '13 at 20:49
Thanks Jake, that approach seems pretty nice and helps a lot. Seems like it includes some additional content for pgfplots? percusse, I do not have a working sample that draws without the second axis yet since I did not figure how I can add the same content to an axis multiple times. (i.e. I have a measurement at setting a x and at b x, yet I did not see how to get that to work since \xticklabels merges same entries into one. – locutus Apr 29 '13 at 7:54
@locutus We only need a plot that draws the bars such that we don't start from scratch writing \documentclass{... just to try out some ideas about a possible answer. That's usually called MWE(minimum workable example). Well the original MWE stands for working example but it doesn't have to be working to work on. – percusse Apr 29 '13 at 8:35

So here is the solution I managed to put up. I used the example from Jakes link and it solved the problem pretty much the way it should. Also, with using the table, I was able to use the same axis-label several times, so all is fine.

Diagram with 2 x-axis labels



    draw group line/.style n args={5}{
        after end axis/.append code={
                        \coordinate [yshift=#4] (startgroup) at (axis cs:\pgfplotsretval,0);
                        \coordinate [yshift=#4] (endgroup) at (axis cs:\pgfplotsretval,0);
                        \draw [
                            shorten >=-#5,
                            shorten <=-#5
                        ] (startgroup) -- node [anchor=north] {#3} (endgroup);
                        \draw [
                            shorten >=-#5,
                            shorten <=-#5
                        ] (startgroup) -- node [anchor=north] {#3} (endgroup);

1  15 4 1
2  13 3 1
3  18 5 2
4  10 1 2

    xticklabels={x, y, x, y},
    enlarge y limits=false,
    enlarge x limits=0.1,
    bar width=20pt,
    legend style={
      legend columns=5,
      /tikz/every even column/.append style={column sep=0.2cm}
    draw group line={[index]3}{1}{a}{-3.5ex}{7pt},
    draw group line={[index]3}{2}{b}{-3.5ex}{7pt},

\addplot+[error bars/.cd,y dir=both,y explicit] table[x index=0,y index=1,y error index=2] \datatable;
share|improve this answer
I noticed a minor flaw with this one though: If you have a group that has only one bar, it throws an error. The solution is pretty simple to just add the content of the first else to the first ifnum. – locutus Apr 29 '13 at 10:00

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.