2

I'm creating a bar chart using the following code:

\documentclass[tikz]{standalone}
\usepackage[english]{babel}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\pgfplotsset{compat=newest}

\begin{document}

\begin{tikzpicture}
\begin{axis}[
    ybar=12pt,
    ymin=0,ymax=150,
    xtick=data,
    enlarge x limits={abs=2cm},
    symbolic x coords={blond, brown},
    bar width = 20pt,
    ylabel= number, 
        ytick align=outside, 
        ytick pos=left,
        major x tick style = transparent,
        legend style={at={(0.04,0.96)},anchor=north west, font=\footnotesize, legend cell align=left,},
        ]    
    \addplot[ybar,fill=blue, area legend] coordinates {
        (blond,20)
        (brown,100)};
    \addplot[ybar,fill=purple, area legend] coordinates {
        (blond,110)
        (brown,105)}; 
 \legend{Boys, Girls}  
\end{axis}
\end{tikzpicture}

\end{document}

Hower, I would like to add a label under and in between the x-axis labels as shown in the attached figure. How can I create this label?

enter image description here

4

You just need xlabel by analogy with ylabel. The labels on the x-axis are just tick labels, like those on the y-axis. The fact that they happen to be words rather than numbers doesn't prevent you from also labelling the axis as a whole, just as you can label the y-axis ;). At least, it seems to work for me:

labelled axes

\documentclass[tikz,border=10pt]{standalone}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\pgfplotsset{compat=newest}

\begin{document}

\begin{tikzpicture}
\begin{axis}[
    ybar=12pt,
    ymin=0,ymax=150,
    xtick=data,
    enlarge x limits={abs=2cm},
    symbolic x coords={blond, brown},
    bar width = 20pt,
    ylabel= number,
    xlabel= hair colour,
        ytick align=outside,
        ytick pos=left,
        major x tick style = transparent,
        legend style={at={(0.04,0.96)},anchor=north west, font=\footnotesize, legend cell align=left,},
        ]
    \addplot[ybar,fill=blue, area legend] coordinates {
        (blond,20)
        (brown,100)};
    \addplot[ybar,fill=purple, area legend] coordinates {
        (blond,110)
        (brown,105)};
 \legend{Boys, Girls}
\end{axis}
\end{tikzpicture}

\end{document}
  • No, you have not missunderstood my questions. That's exactly what I wanted. I just started using tikz and thought symbolic x coords "overwrites" the xlabel option. Thanks! – Shery Dec 22 '14 at 1:29
  • @Shery Oh. Fair enough. It was really that your code seemed rather sophisticated in comparison with what seemed a simple solution. Given that I wouldn't know how to draw what you drew, and given that I couldn't even remember the xlabel command and didn't read the manual, I didn't expect to come up with something you hadn't! (Also, if it were me, I'd have tried it before posting a question because trying it is very, very low risk. That is, the cost is negligible in comparison with the cost (time & effort) of posting a question here. But I'm always wasting time on ideas which fail, too. :( ) – cfr Dec 22 '14 at 1:34
  • @Shery I've edited my answer a little: I think the symbolic coordinate thing is just tick labels, just like the numbers on the y-axis. That is, neither stop you from labelling the axis as a whole ;). – cfr Dec 22 '14 at 1:47
  • Thanks, that makes sense. I have to admit I got most of the code using copy & paste of codes snippets I found on this website :) Now the charts finaly looks like I wanted it to look. Thanks again! – Shery Dec 22 '14 at 2:10
1

An alternative is use of xticklabel* coordinate system to put the title of the plot. clip=false is required.

 \node (title) at (xticklabel* cs: 0.5,25pt) {Hair Color};

enter image description here

Code

\documentclass[tikz]{standalone}
\usepackage[english]{babel}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\pgfplotsset{compat=newest}

\begin{document}

\begin{tikzpicture}
\begin{axis}[
    ybar=12pt,
    ymin=0,ymax=150,
    xtick=data,
    enlarge x limits={abs=2cm},
    symbolic x coords={blond, brown},
    bar width = 20pt,
    ylabel= number, clip=false,
        ytick align=outside, 
        ytick pos=left,
        major x tick style = transparent,
        legend style={at={(0.04,0.96)},anchor=north west, font=\footnotesize, legend cell align=left,},
        ]    
    \addplot[ybar,fill=blue, area legend] coordinates {
        (blond,20)
        (brown,100)};
    \addplot[ybar,fill=purple, area legend] coordinates {
        (blond,110)
        (brown,105)}; 
 \legend{Boys, Girls}  
 \node (title) at (xticklabel* cs: 0.5,25pt) {Hair Color};
\end{axis}
\end{tikzpicture}

\end{document}
  • Is there an advantage to doing it this way? I don't really use pgfplots so I'm pretty ignorant about it and really just tried xlabel on analogy with ylabel, but perhaps there are pit-falls I didn't see? – cfr Dec 22 '14 at 2:16
  • 1
    @cfr your solution is valid. I was working on that too, but you posted first, so I switched focus on the title that I learned before. The scenario is what if xlabel had been used and a line of title is needed. Then my solution provides an alternative. – Jesse Dec 22 '14 at 2:41
  • (+1) Ah, I see. Thank you. I was worried I'd done something obviously bad. And... (+1). – cfr Dec 22 '14 at 3:06
  • You are humble. Thank you. I admire your knowledge shown in this site. – Jesse Dec 22 '14 at 3:09
  • I don't really know anything. I just fiddle... and sometimes things work ;). Anyway, look who's talking about knowledge! [And I really don't use pgfplots much at all. So it wouldn't surprise me at all if I did something obviously idiotic with it. I think I've drawn one graph with it ever. And that was probably a copy-paste-modify job.] – cfr Dec 22 '14 at 3:12

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.