I am trying to make a bar chart for categorical data, where each category has a score and the number of items in a category is represented by the width of the bar. Based on Bar chart from CSV file with adjustable bar width I tried to add the category labels on the x axis with xticklabels from table
:
\documentclass[border=5mm]{standalone}
\usepackage{filecontents}
\usepackage{pgfplots, pgfplotstable}
\begin{filecontents}{data.csv}
name height width
foo 1 884
bar 2 5768
baz 3 835
qux 7 2661
quux 10 492
corge 11 1349
clyde 11 0
\end{filecontents}
\begin{document}
\begin{tikzpicture}
\begin{axis}[xticklabels from table={data.csv}{name},
typeset ticklabels with strut
]
\addplot [ybar interval, fill=yellow] table [
create on use/xaccum/.style={
create col/expr=\pgfmathaccuma+\prevrow{width}
},
x=xaccum, y=height
] {data.csv};
\end{axis}
\end{tikzpicture}
\end{document}
This solution however does not align the xticklabels to the bars:
I was thinking about adding a extra description
nodes or extra x tick
, or using an extra loop after plotting to add nodes based on the new 'xaccum' column, but I'm not sure how to implement something like that.
Bonus question: how to remove the 10e4?
Other solutions (with or without pgfplots) are also welcome. In principle I can precompute or modify the data if needed (in LaTeX or otherwise). An example would be to (automatically) add zero-height unlabeled rows before and after narrow bars to prevent label overlap.
xtick=data
to theaxis
options to "align" thexticklabels
(at the beginning of the bar, but then the labels will overlap)