Below is a simple example of a stacked bar chart from the pgfplots manual.
In my case, the y axis represent percentages, and the percentages in all columns add up to 100 (so like in this example, all bars have the same height). However, the absolute number of items in each group differs. For example, I have 200 samples for tool1 but 500 for tool2. I would like to modify the bar width as a function of the total number of samples in each group, so that the bars for larger groups are wider.
I am aware of bar width
. However, the \addplot
macro defines all coordinates for, e.g., the blue parts, so setting bar width
on the first \addplot
changes the widths of all the blue bar parts. What I want instead is a way to set the width of an entire column.
Is there a way to do this? Ideally, the width is computed automatically, but it would be fine if I would have to set the width for each column manually.
\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{width=7cm,compat=1.18}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
ybar stacked,
symbolic x coords={tool1,tool2,tool3,tool4,tool5,tool6,tool7},
xtick=data,
x tick label style={rotate=45,anchor=east}]
\addplot+ [ybar] coordinates {(tool1,0) (tool2,2) (tool3,2) (tool4,3) (tool5,0) (tool6,2) (tool7,0)};
\addplot+ [ybar] coordinates {(tool1,0) (tool2,0) (tool3,0) (tool4,3) (tool5,1) (tool6,1) (tool7,0)};
\addplot+ [ybar] coordinates {(tool1,6) (tool2,6) (tool3,8) (tool4,2) (tool5,6) (tool6,5) (tool7,6)};
\addplot+ [ybar] coordinates {(tool1,4) (tool2,2) (tool3,0) (tool4,2) (tool5,3) (tool6,2) (tool7,4)};
\end{axis}
\end{tikzpicture}
\end{document}