[Tex/LaTex] Bar chart from CSV file with adjustable bar width

bar chartcsvplot

I have a CSV file with approximately 1500 records that I would like to plot using something like a bar chart, but I don't know where to start at all.

Each record contains a width and a height, which needs to be translated into the width and height of the bars. The bars then need to be arranged side by side without space in between.

The total accumulative width is about 50k units, and the maximum height is about 205k. The y-axis (height) should have a logarithmic scale, since most height values are <10k. I hope to scale the whole thing to about 1 x 0.75 * a page's available width.

I also would like a scale on the x- and y-axes that is readably large when scaled.

CSV example:

height,width
1,884
2,5768
3,835
7,2661
10,492
11,1349
...

Best Answer

You can use the create on use mechanism to generate a new column on the fly that contains the cumulative widths of the bars, and use that together with the ybar interval plot style to generate the plot:

\documentclass[border=5mm]{standalone}
\usepackage{filecontents}
\usepackage{pgfplots, pgfplotstable}

\begin{filecontents}{data.csv}
height,width
1,884
2,5768
3,835
7,2661
10,492
11,1349
11,0
\end{filecontents}

\begin{document}
\begin{tikzpicture}
\begin{axis}
\addplot [ybar interval, fill=yellow] table [
        col sep=comma,
        create on use/xaccum/.style={
            create col/expr=\pgfmathaccuma+\prevrow{width}
        },
        x=xaccum, y=height
    ] {data.csv};
\end{axis}
\end{tikzpicture}
\end{document}