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 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
1  
Take a look at Two Level Labels in Bar Plot‌​. Would that approach work for you? –  Jake Apr 28 '13 at 20:49
1  
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
add comment

1 Answer

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

\documentclass{standalone}

\usepackage{tikz}
\usepackage{pgfplots}

\newcounter{groupcount}
\pgfplotsset{
    draw group line/.style n args={5}{
        after end axis/.append code={
            \setcounter{groupcount}{0}
            \pgfplotstableforeachcolumnelement{#1}\of\datatable\as\cell{%
                \def\temp{#2}
                \ifx\temp\cell
                    \ifnum\thegroupcount=0
                        \stepcounter{groupcount}
                        \pgfplotstablegetelem{\pgfplotstablerow}{[index]0}\of\datatable
                        \coordinate [yshift=#4] (startgroup) at (axis cs:\pgfplotsretval,0);
                    \else
                        \pgfplotstablegetelem{\pgfplotstablerow}{[index]0}\of\datatable
                        \coordinate [yshift=#4] (endgroup) at (axis cs:\pgfplotsretval,0);
                    \fi
                \else
                    \ifnum\thegroupcount=1
                        \setcounter{groupcount}{0}
                        \draw [
                            shorten >=-#5,
                            shorten <=-#5
                        ] (startgroup) -- node [anchor=north] {#3} (endgroup);
                    \fi
                \fi
            }
            \ifnum\thegroupcount=1
                        \setcounter{groupcount}{0}
                        \draw [
                            shorten >=-#5,
                            shorten <=-#5
                        ] (startgroup) -- node [anchor=north] {#3} (endgroup);
            \fi
        }
    }
}


\pgfplotstableread{
1  15 4 1
2  13 3 1
3  18 5 2
4  10 1 2
}\datatable

\begin{document}
\makeatletter
\begin{tikzpicture}
\begin{axis}[
    ylabel=label,
    xtick=data,
    xticklabels={x, y, x, y},
    enlarge y limits=false,
    enlarge x limits=0.1,
    ymin=0,ymax=25,
    ybar,
    bar width=20pt,
    legend style={
      font=\footnotesize,
      cells={anchor=west},
      legend columns=5,
      at={(0.5,-0.2)},
      anchor=north,
      /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;
\end{axis}
\end{tikzpicture}
\end{document}
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
add comment

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.