# How to add additional x-axis labels to each bar in a ybar chart? [duplicate]

I've drawn a ybar chart in pgfplots as below, and I want to add additional x-axis labels, as indicated by what I've drawn by hand in orange.

Question: How do I add additional x-axis labels to each bar in a ybar chart?

My current code is the following:

\documentclass[crop,margin=1mm]{standalone}

\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.13}

n  A  B  C
C1 32 24 57
C2 34 35 12
C3 45 47 34
C4 10 20 30
}\mytable

\begin{document}

\begin{tikzpicture}

\begin{axis}[
ybar,
width=6in,
height=2.5in,
ymin=0,
ymax=100,
bar width=0.2cm,
ymajorgrids=true,
major grid style={dotted,black},
ylabel={y axis},
symbolic x coords={C1,C2,C3,C4},
xtick=data,
x tick label style={yshift=-0.5cm}
]

\end{axis}

\end{tikzpicture}

\end{document}


Importantly, the labels A, B, and C repeat (they usually occur in a legend, when they're not repeated).

I attempted to use nodes near coords={A}, but putting it inside the axis[ .. ] puts an A above every bar, and putting it inside the table[ .. ] does nothing.

The labels are from the header (namely the line n A B C) within \pgfplotstableread{ ... }.

## marked as duplicate by Stefan Pinnow pgfplots StackExchange.ready(function() { if (StackExchange.options.isMobile) return; $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() { var$hover = $(this).addClass('hover-bound'),$msg = $hover.siblings('.dupe-hammer-message');$hover.hover( function() { $hover.showInfoMessage('', { messageElement:$msg.clone().show(), transient: false, position: { my: 'bottom left', at: 'top center', offsetTop: -7 }, dismissable: false, relativeToBody: true }); }, function() { StackExchange.helpers.removeMessages(); } ); }); }); Apr 10 '18 at 4:14

• What would be the benefit to repeat the labels at every bar instead of just stating them once in a legend? – Stefan Pinnow Mar 27 '18 at 10:23
• I’m asking for a college who asked me to help him with this figure; I mentioned that it was normal to have a legend, but he prefers it labeled this way. (I’m working on a toy example, so I’m not sure what he has in mind.) – Rebecca J. Stones Mar 27 '18 at 10:26

So I guess you are searching for something like the following, right? This solution works independently from the specified bar width.

For details please have a look at the comments in the code.

% used PGFPlots v1.16
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{
compat=1.3,
}
n  A  B  C
C1 32 24 57
C2 34 35 12
C3 45 47 34
C4 10 20 30
}\mytable
\begin{document}
\begin{tikzpicture}
\begin{axis}[
ybar,
ymin=0,
ymax=100,
bar width=2mm,
ymajorgrids=true,
major grid style={dotted,black},
ylabel={$y$-axis},
symbolic x coords={C1,C2,C3,C4},
xtick=data,
x tick label style={yshift=-5mm},
% ---------------------------------------------------------------------
% we want to provide *absolute* at' values ...
scatter/position=absolute,
node near coords style={
% ... to provide axis coordinates at ymin' for the nodes near coords'
at={(axis cs:\pgfkeysvalueof{/data point/x},\pgfkeysvalueof{/pgfplots/ymin})},
% then also the anchor' has to be changed ...
anchor=north,
% ... and we apply a correction for the major thick length'
yshift={-\pgfkeysvalueof{/pgfplots/major tick length}},
% -------------
% (in case the labels get a bit longer, use these two options
%  instead of the above anchor')
%            anchor=east,
%            rotate=90,
% -------------
},
% ---------------------------------------------------------------------
table/x=n,
]
\addplot + [nodes near coords=A] table [y=A] {\mytable};
\addplot + [nodes near coords=B] table [y=B] {\mytable};
\addplot + [nodes near coords=C] table [y=C] {\mytable};
\end{axis}
\end{tikzpicture}
\end{document}


One possibility is just to add those labels "by hand".

\documentclass[crop,margin=1mm]{standalone}

\usepackage{tikz}
\usetikzlibrary{positioning}
\usepackage{pgfplots}
\pgfplotsset{compat=1.13}

n  A  B  C
C1 32 24 57
C2 34 35 12
C3 45 47 34
C4 10 20 30
}\mytable

\begin{document}

\begin{tikzpicture}

\begin{axis}[
ybar,
width=6in,
height=2.5in,
ymin=0,
ymax=100,
bar width=0.2cm,
ymajorgrids=true,
major grid style={dotted,black},
ylabel={y axis},
symbolic x coords={C1,C2,C3,C4},
xtick=data,
x tick label style={yshift=-0.5cm,name=xlabel\ticknum}
]