1

I've been trying to create a stacked and grouped bar chart using the package pgfplots but I am struggling with it.

Here is a minimal working example:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{pgfplots}
\pgfplotsset{
    compat=1.18
}
\begin{document}
\pgfplotstableread{
100 0.33 0.3 0.14 0.77
200 0.57 1.26 0.16 1.99
300 0.85 2.25 0.15 3.25
400 1.21 3.36 0.17 4.74
500 1.74 4.06 0.17 5.97
600 2.19 4.73 0.18 7.09
700 2.64 5.69 0.18 8.51
800 3.38 6.59 0.18 10.15
900 3.65 8.32 0.19 12.15
1000 3.97 9.86 0.19 14.01
}\dataset
\begin{figure}
\begin{tikzpicture}
\begin{axis}[
ybar stacked,
width=\textwidth,
bar width = 8pt,
ylabel= time in seconds,
xlabel near ticks,
xlabel=graph size,
ymin = 0,
enlarge x limits=0.05,
xtick=data,
xticklabels = {
100,
200,
300,
400,
500,
600,
700,
800,
900,
1000
},
xticklabel style={yshift=-6ex}, % height of xticklabel
major x tick style = {/pgfplots/major tick length=0pt,},
minor x tick num = 1,
minor tick length=2ex,
scatter/position=absolute,
node near coords style={
    % ... to provide axis coordinates at `ymin' for the nodes
    at={(axis cs:\pgfkeysvalueof{/data point/x},\pgfkeysvalueof{/pgfplots/ymin})},
    % then also the `anchor' has to be adapted ...
    anchor=east,
    % ... because we rotate the labels which would overlap otherwise
    rotate=90,
},
]
% first stacked bar
\addplot[ybar, fill=green, nodes near coords=abc]
table [x index=0, y index=1]\dataset;
\addplot[ybar, fill=red]
table [x index=0, y index=2]\dataset;
\addplot[ybar, fill=blue]
table [x index=0, y index=2]\dataset;
% second stacked bar
% this should be starting at y=0 
\addplot[ybar, fill=green, bar shift=12px, nodes near coords=def]
table [x index=0, y index=1]\dataset;
\addplot[ybar, fill=red, bar shift=12px]
table [x index=0, y index=2]\dataset;
\addplot[ybar, fill=blue, bar shift=12px]
table [x index=0, y index=2]\dataset;
\end{axis}
\end{tikzpicture}
    \caption{Caption}
\end{figure}
\end{document}

second bar does not start at the bottom

0 Answers0