2

I have a template that globally sets the ybar option. However, I would like to use ybar stacked. When combining both, the output bars are shifted to the right as in the example below. How can I reset the effect of ybar without removing it from the global options?

The problem is very similar to this post, but it is the other way around.

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{ybar} % global option that can not be removed
\begin{document}

\begin{tikzpicture}
    \begin{axis}[ybar stacked]
        \addplot coordinates {(0,1) (1,1) (2,3) (3,2) (4,1.5)};
        \addplot coordinates {(0,1) (1,1) (2,3) (3,2) (4,1.5)};
        \addplot coordinates {(0,1) (1,1) (2,3) (3,2) (4,1.5)};
    \end{axis}
\end{tikzpicture}

\end{document}

ybar mixed with ybar stacked

  • 2
    Add bar shift=0pt to the axis options: \begin{axis}[ybar stacked,bar shift=0pt] – esdd Nov 22 '17 at 10:31
  • Sounds like a bad plan to globally set ybar, why can't you remove it? – Torbjørn T. Nov 22 '17 at 10:40
  • @TorbjørnT. Because it is a plot template that was originally only meant to be used for conventional bar plots. Now it is in use by dozens of plots and either I have to touch all of them or add a bar shift=0pt to one of them, as @esdd suggested, which is working very good for my application. – koalo Nov 22 '17 at 10:47
2

Add bar shift=0pt to the axis options:

\begin{axis}[ybar stacked,bar shift=0pt]

enter image description here

Additional remark: Set a compat immediatelly after loading pgfplots.

Code:

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.15}% <- add this after loading pgfplots, current version is 1.15
\pgfplotsset{ybar} % global option that can not be removed
\begin{document}
\begin{tikzpicture}
    \begin{axis}[ybar stacked,
      bar shift=0pt% <- added
    ]
        \addplot coordinates {(0,1) (1,1) (2,3) (3,2) (4,1.5)};
        \addplot coordinates {(0,1) (1,1) (2,3) (3,2) (4,1.5)};
        \addplot coordinates {(0,1) (1,1) (2,3) (3,2) (4,1.5)};
    \end{axis}
\end{tikzpicture}
\end{document}

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.