4

I would like to use absolute coordinates instead of relative values when using a ybar stacked plot. Is this possible? I know I can just reformat my data to be relative, but it sounds like pgfplots could do that job too. I assume my real problem is that what I seek is not 'stacked', but just 'overlay'. Does that exist?

Here is an example from converting an area plot that uses absolute coordinates to a ybar stacked plot to show what I mean.

area plot

\begin{figure}[h]\centering
\begin{tikzpicture}\begin{axis}[area style,xtick=data]
\addplot[red,fill=red] coordinates { (1,8) (2,8) (3,9) (4,10) } \closedcycle;
\addplot[green,fill=green] coordinates { (1,5) (2,7) (3,8) (4,9)  } \closedcycle;
\addplot[blue,fill=blue] coordinates { (1,3) (2,5) (3,7) (4,8)  } \closedcycle;
\end{axis}\end{tikzpicture}
\caption{Original area plot ordered back to front using absolute values that I want to convert to a stacked bar plot instead.}
\end{figure}

stacked relative

\begin{figure}[h]\centering
\begin{tikzpicture}\begin{axis}[ybar stacked,xtick=data]
\addplot[blue,fill=blue] coordinates { (1,3) (2,5) (3,7) (4,8) };
\addplot[green,fill=green] coordinates { (1,2) (2,2) (3,1) (4,1) };
\addplot[red,fill=red] coordinates { (1,3) (2,1) (3,1) (4,1) };
\end{axis}\end{tikzpicture}
\caption{Stacked using base values and relative values, ordered bottom to top. This is the output I desire, but I had to modify the data to be relative.}
\end{figure}

stacked_absolute

\begin{figure}[h]\centering
\begin{tikzpicture}\begin{axis}[ybar stacked,xtick=data]
\addplot[red,fill=red] coordinates { (1,8) (2,8) (3,9) (4,10) };
\addplot[green,fill=green] coordinates { (1,5) (2,7) (3,8) (4,9)  };
\addplot[blue,fill=blue] coordinates { (1,3) (2,5) (3,7) (4,8)  };
\end{axis}\end{tikzpicture}
\caption{Stacked using original absolute data, ordered back to front.}
\end{figure}

As a new user I can't make new keywords, but I wish they were: pgfplots, stacked, ybar, absolute, relative.

  • Welcome to tex.sx! We don't want to go overboard with tags (keywords) -- e.g., absolute and relative aren't particularly helpful when looking for relevant questions. (And I'm sure that pgfplots is sufficient as a call to the experts. ;-)) – lockstep Sep 1 '11 at 16:11
2

You can overlay the bars by setting bar shift=0pt. Note that you should also always set ymin=0 explicitly to make sure the whole columns are visible.

\documentclass{article}
\usepackage{pgfplots}
\begin{document}
\begin{figure}[h]\centering
\begin{tikzpicture}\begin{axis}[
    ybar,
    bar shift=0pt,
    ymin=0,
    xtick=data]
\addplot[red,fill=red] coordinates { (1,8) (2,8) (3,9) (4,10) };
\addplot[green,fill=green] coordinates { (1,5) (2,7) (3,8) (4,9)  };
\addplot[blue,fill=blue] coordinates { (1,3) (2,5) (3,7) (4,8)  };
\end{axis}\end{tikzpicture}
\caption{Stacked using original absolute data, ordered back to front.}
\end{figure}

\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.