2

I'm creating a stacked ybar plot from some data, which causes the input coordinates to be not sorted. This causes a gap between some of the bars. Is this a bug? Shouldn't the stacked ybar plot know all the heights? If not, is there a simple way to fix this? Surely i fix this in my program (i do some data processing beforehand), but i feel there should be an easy solution.

Here is a sample document:

\documentclass{scrartcl}

\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}
    \begin{axis}
        [
            ybar stacked,
            ymin = 0,
            enlarge x limits
        ]
        \addplot coordinates {
            (1, 1)
            (2, 2)
            (3, 3)
        };
        \addplot coordinates {
            (1, 1)
            (3, 3) % 'wrong' order
            (2, 2)
        };
    \end{axis}
\end{tikzpicture}

\end{document}

This produces:Wrong result

What i want is the following, which is produced by changing the 'wrongly' ordered lines in the second \addplot command. Wanted Result

3

This is not a real answer, but a long comment ...


In the PGFPlots manual (v1.15) section 4.5.9 on page 94 at the end of the description of the key stack plots you can find the following text:

The current implementation for stack plots does not interpolate missing coordinates. That means stacking will fail if the plots have different grids.

Together with the slightly modified example you have given which you can find below, it should be pretty much self-explanatory how the current implementation is working. Thus I think you don't have another choice than sorting your entries first before stating the coordinates.

But you mentioned something about "your program" so I think you create your data externally. Then I would consider writing the data to an external file and use this data by \addplot table. (I don't present an example here because you can find plenty of them on this site.)

% used PGFPlots v1.15
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
    % used to center the `nodes near coords' in stacked plots
    \pgfplotsset{compat=1.9}
\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        ybar stacked,
        ymin=0,
        enlarge x limits,
        nodes near coords,
        point meta=\coordindex,
    ]
        \addplot coordinates {
            (1, 1)
            (5, 2) % 'wrong' order
            (3, 3)
        };
        \addplot coordinates {
            (1, 1)
            (4, 3) % 'wrong' order
            (2, 2)
        };
    \end{axis}
\end{tikzpicture}
\end{document}

image showing the result of above code

| improve this answer | |
  • I see, thank you for your example. You are right, i am generating the data myself, as well as the whole .tex file. So i will just stick to sorting the data :) – pschulz Sep 11 '17 at 11:44

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.