2

I'd like another set of hard-coded labels that sit on top, near the bottom (0) of each bar. I.e., like the nodes near coords labels, but shifted down towards 0 and with my own custom text. So in the following example, there would be an additional four labels (not shown). Is there an easy way to do that? Thanks!

example

MWE:

\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\usepackage{pgfplotstable}
\renewcommand*{\familydefault}{\sfdefault}
\usepackage{sfmath}

\pgfplotsset{compat=1.12}
\pgfplotsset{
    width=\textwidth,
}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
ybar,axis on top, ymax=100,
ymin=30,
height=8cm,
width=5cm,
enlarge x limits=0.5,
ymajorgrids, tick align=inside,
major grid style={draw=white},
axis x line*=left,
ylabel={\%},
xtick=data,
legend cell align=left,
max space between ticks=20pt,
legend style={ at={(0.57,1.05)} ,
                column sep=1ex },
symbolic x coords={label1, label2},
nodes near coords=\rotatebox{90}{\scriptsize\pgfmathprintnumber\pgfplotspointmeta},
]
 \addplot[draw=none,fill=blue!40!white] coordinates      {(label1,80.2)      (label2,90.7)   }; 
 \addplot[draw=none,fill=red!40!white] coordinates      {(label1,70)      (label2,80) }; 

\legend{Data1, Data2}
\end{axis}
\end{tikzpicture}

\end{document}
3

The following works for two data series. The idea is to add two labels below each x value, one to the left (anchor=east) and one to the right (anchor=west). The x position is taken from the symbolic x coordinates system, the y coordinate is taken from the xticklabel cs, which lies on the x axis. If we would just put the corresponding \node command into the axis environment, it would be clipped away. The option \after end axis/.code allows to specify code to be executed after the clipping.

\begin{tikzpicture}
\begin{axis}[
ybar,axis on top, ymax=100,
ymin=30,
height=8cm,
width=5cm,
enlarge x limits=0.5,
ymajorgrids, tick align=inside,
major grid style={draw=white},
axis x line*=left,
ylabel={\%},
xtick=data,
legend cell align=left,
max space between ticks=20pt,
legend style={ at={(0.57,1.05)} ,
                column sep=1ex },
symbolic x coords={label1, label2},
nodes near coords=\rotatebox{90}{\scriptsize\pgfmathprintnumber\pgfplotspointmeta},
after end axis/.code={
\node(L1) at (axis cs:label1,1) {};
\node(L2) at (axis cs:label2,1) {};
\node(XTL) at (xticklabel cs:0)  {};
\node[anchor=north east] at (L1 |- XTL) {L11};
\node[anchor=north west] at (L1 |- XTL) {L12};
\node[anchor=north east] at (L2 |- XTL) {L21};
\node[anchor=north west] at (L2 |- XTL) {L22};
},
]
 \addplot[draw=none,fill=blue!40!white] coordinates      {(label1,80.2)      (label2,90.7)   }; 
 \addplot[draw=none,fill=red!40!white] coordinates      {(label1,70)      (label2,80) }; 

\legend{Data1, Data2}
\end{axis}
\end{tikzpicture}

enter image description here

EDIT: Labels above x axis

I overread that you wanted to have the labels above the xaxis. This can be achieved with the following change to the above code:

after end axis/.code={
\node(L1) at (axis cs:label1,1) {};
\node(L2) at (axis cs:label2,1) {};
\node(XTL) at (xticklabel* cs:0)  {};
\node[anchor=south east] at (L1 |- XTL) {\rotatebox{90}{\scriptsize L11}};
\node[anchor=south west] at (L1 |- XTL) {\rotatebox{90}{\scriptsize L12}};
\node[anchor=south east] at (L2 |- XTL) {\rotatebox{90}{\scriptsize L21}};
\node[anchor=south west] at (L2 |- XTL) {\rotatebox{90}{\scriptsize L22}};
},

enter image description here

| improve this answer | |
  • Thanks @jarauh! How would you generalise this to more than two datasets? – jodles Oct 26 '15 at 9:55

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.