0

Let's say you have a grouped bar chart that looks like this:

enter image description here

What I would like to do is add another bar for a new xtick, X0, that belongs to another group, A.

My naive attempt at adding this data gets me this:

enter image description here

This isn't quite what I want because the bars are not centred properly. Can this be fixed?

Here is the code I used to generate the two charts:

\documentclass{article}

\usepackage{pgfplots}


\pgfplotsset{compat=1.16}

\pgfplotstableread[row sep=\\,col sep=&]{
    X  & B   & C   & ErrorB & ErrorC \\
    X1 & 0.9 & 0.7 & 0.025  & 0.030 \\
    X2 & 0.8 & 0.6 & 0.010  & 0.010 \\
    X3 & 0.7 & 0.5 & 0.020  & 0.020 \\
}\resultsb


\begin{document}
  \begin{tikzpicture}
    \begin{axis}[
      width=0.9\textwidth,
      bar width=1.5em,
      ybar,
      xtick=data,
      enlarge x limits=0.2,
      symbolic x coords={X0,X1,X2,X3},
      legend entries={B,C},
      ]
      \addplot+[error bars/.cd,y dir=both,y explicit]
        table [x=X,y=B,y error=ErrorB] {\resultsb};
      \addplot+[error bars/.cd,y dir=both,y explicit]
        table [x=X,y=C,y error=ErrorC] {\resultsb};
    \end{axis}
  \end{tikzpicture}
\end{document}

.

\documentclass{article}

\usepackage{pgfplots}


\pgfplotsset{compat=1.16}

\pgfplotstableread[row sep=\\,col sep=&]{
    X  & A   & ErrorA \\
    X0 & 1.0 & 0.015 \\
}\resultsa

\pgfplotstableread[row sep=\\,col sep=&]{
    X  & B   & C   & ErrorB & ErrorC \\
    X1 & 0.9 & 0.7 & 0.025  & 0.030 \\
    X2 & 0.8 & 0.6 & 0.010  & 0.010 \\
    X3 & 0.7 & 0.5 & 0.020  & 0.020 \\
}\resultsb


\begin{document}
  \begin{tikzpicture}
    \begin{axis}[
      width=0.9\textwidth,
      bar width=1.5em,
      ybar,
      xtick={X0,X1,X2,X3},
      enlarge x limits=0.2,
      symbolic x coords={X0,X1,X2,X3},
      legend entries={A,B,C},
      ]
      \addplot+[error bars/.cd,y dir=both,y explicit]
        table [x=X,y=A,y error=ErrorA] {\resultsa};
      \addplot+[error bars/.cd,y dir=both,y explicit]
        table [x=X,y=B,y error=ErrorB] {\resultsb};
      \addplot+[error bars/.cd,y dir=both,y explicit]
        table [x=X,y=C,y error=ErrorC] {\resultsb};
    \end{axis}
  \end{tikzpicture}
\end{document}

1 Answer 1

2

One thing that you could do is to add some bar shift keys.

\documentclass{article}

\usepackage{pgfplots}


\pgfplotsset{compat=1.16}

\pgfplotstableread[row sep=\\,col sep=&]{
    X  & A   & ErrorA \\
    X0 & 1.0 & 0.015 \\
}\resultsa

\pgfplotstableread[row sep=\\,col sep=&]{
    X  & B   & C   & ErrorB & ErrorC \\
    X1 & 0.9 & 0.7 & 0.025  & 0.030 \\
    X2 & 0.8 & 0.6 & 0.010  & 0.010 \\
    X3 & 0.7 & 0.5 & 0.020  & 0.020 \\
}\resultsb


\begin{document}
  \begin{tikzpicture}
    \begin{axis}[
      width=0.9\textwidth,
      bar width=1.5em,
      ybar,
      xtick={X0,X1,X2,X3},
      enlarge x limits=0.2,
      symbolic x coords={X0,X1,X2,X3},
      legend entries={A,B,C},
      ]
      \addplot+[bar shift=0em,error bars/.cd,y dir=both,y explicit]
        table [x=X,y=A,y error=ErrorA] {\resultsa};
      \addplot+[bar shift=-1em,error bars/.cd,y dir=both,y explicit]
        table [x=X,y=B,y error=ErrorB] {\resultsb};
      \addplot+[bar shift=1em,error bars/.cd,y dir=both,y explicit]
        table [x=X,y=C,y error=ErrorC] {\resultsb};
    \end{axis}
  \end{tikzpicture}
\end{document}

enter image description here

1
  • Thanks! That's a good enough solution for me.
    – someguy
    Apr 20, 2021 at 19:56

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .