5

When one has more then one barplot in one pgfplots axis like in the following example then the red plot at the top of the code ends up as the bottom bar of the plot. I do find this confusing -- particularly as I have to make a legend in a list at the end and have to reverse the order there.

Is there any way to get the red bars to the top without changing the order in the code?

\documentclass{article}
\usepackage{pgfplots}


\begin{document}
\begin{tikzpicture}
\begin{axis}[xbar,]
\addplot[fill=red]
coordinates {(1,1)};
\label{red}

\addplot[fill=blue]
coordinates
{(1,1)}; \label{blue}
\end{axis}
\end{tikzpicture}

\begin{itemize}
\item[\ref{blue}] blub
\item[\ref{red}] red
\end{itemize}

\end{document}

enter image description here

0

1 Answer 1

3

You can switch the order by calling xbar=-2pt, bar width=-10pt (the negative of the default values for the gap between bars and the width of the bars):

\documentclass{article}
\usepackage{pgfplots}


\begin{document}
\begin{tikzpicture}
\begin{axis}[xbar=-2pt, bar width=-10pt]
\addplot[fill=red]
coordinates {(1,1)};
\label{red}

\addplot[fill=blue]
coordinates
{(1,1)}; \label{blue}
\end{axis}
\end{tikzpicture}

\begin{itemize}
\item[\ref{blue}] blub
\item[\ref{red}] red
\end{itemize}

\end{document}

Alternatively, you could use

xbar,
/pgf/bar shift={%
        % total width = n*w + (n-1)*skip
        % -> subtract half for centering
        0.5*(\numplotsofactualtype*\pgfplotbarwidth + (\numplotsofactualtype-1)*(2pt))  - 
        % the '0.5*w' is for centering
        (.5+\plotnumofactualtype)*\pgfplotbarwidth - \plotnumofactualtype*(2pt)}

which is the negative of the function used for offsetting the bars.

1
  • So "shift" was the keyword, bot reverse ;-). In my real case, I have various plots with varying numbers of plots. I also have a "bar distance" of 0pt, so I will use your second sugestions and replace the 2pt with 0pt. Feb 13, 2016 at 20:47

You must log in to answer this question.

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