1

I am trying to create a stacked bar plot in oder to compare two sets of data. If I add only one plot (in this example just uncomment the other plot) everything looks perfect, but if i try to activate both at the same time, curious things happen. Can sombody please help? Please excuse the poor layout in the example, in reality I read from a data file with more entries and the plot looks nicer. Working example:

\documentclass[a4paper,12pt,oneside]{report}
    \usepackage{pgfplots}

\begin{document}


\begin{tikzpicture}
\begin{axis}[xbar stacked,
symbolic y coords={DP,DPn},
bar width=7pt,
y=0.8cm]

\addplot coordinates {(10.322289,DP) (5.856952215,DP) (1.0252891,DP)};
\addplot coordinates {(10.26813236,DPn) (5.833599714,DPn) (1.035041566,DPn)};
\end{axis}
\end{tikzpicture}

\end{document}

Picture of the whole plot (the working example only includes the first entry but shows the same issue).

enter image description here enter image description here

  • Did my answer answer your question? If yes, please consider upvoting (by clicking on the arrows next to the score) it and/or marking it as the accepted answer (by clicking on the checkmark ✓). If not, please edit your question again and state what you are missing. – Stefan Pinnow Apr 24 '18 at 4:33
3

You simply use it wrong. Each \addplot command should contain only one coordinate per ytick and they get stacked by multiple of such \addplot commands. So a simple rearrangement of your coordinates to different \addplot commands solves your problem.

\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        xbar stacked,
        symbolic y coords={DP,DPn},
        ytick=data,
    ]
        \addplot coordinates {(10.322289,DP) (10.26813236,DPn)};
        \addplot coordinates {(5.856952215,DP) (5.833599714,DPn)};
        \addplot coordinates {(1.0252891,DP) (1.035041566,DPn)};
    \end{axis}
\end{tikzpicture}
\end{document}

image showing the result of above code

  • thank you this was very useful! now i adapted my data, but my problem is, that i am comparing data for each point, so i want every first bar to be red and every second bar to be blue for example. is there a possibility to mix up two stacked bar plots? or change the color for each point added? (i added a picture to explain to the original post) – Lieselottelernt Apr 25 '18 at 9:41
0

okay, i did find my own solution, based on this thread: pgfplots: Multiple (shifted) stacked plots in one diagram now i adjusted the colours and could get the plot i wanted:enter image description here

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.