0

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.

current output

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}

\pgfplotstableread{
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}
  ]  

  \addplot table [x=n,y=A,draw=black,fill=blue!20] {\mytable};
  \addplot table [x=n,y=B,draw=black,fill=blue!20] {\mytable};
  \addplot table [x=n,y=C,draw=black,fill=blue!20] {\mytable};

\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 Apr 10 '18 at 4:14

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 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
1

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,
    }
    \pgfplotstableread{
        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},
        % ---------------------------------------------------------------------
        % (adapted solution from <https://tex.stackexchange.com/a/141006/95441>)
        % 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}

image showing the result of above code

2

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}

\pgfplotstableread{
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}
  ]  

  \addplot table [x=n,y=A,draw=black,fill=blue!20] {\mytable};
  \addplot table [x=n,y=B,draw=black,fill=blue!20] {\mytable};
  \addplot table [x=n,y=C,draw=black,fill=blue!20] {\mytable};

\end{axis}
\foreach \X in {0,...,3}
{
\node[above=1mm of xlabel\X]{A\,B\,C};
}
\end{tikzpicture}

\end{document}

enter image description here

Not the answer you're looking for? Browse other questions tagged or ask your own question.