0

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:

enter image description here

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.

  • add xtick=data to the axis options to "align" the xticklabels (at the beginning of the bar, but then the labels will overlap) – Stefan Pinnow Apr 6 '18 at 11:11
  • @StefanPinnow that is already much better than what I had, thanks! In the end I made the graph with gnuplot, which allows to explicitly set the x position, width and label of each bar (and also a global label offset). – Marijn Apr 6 '18 at 12:19
1

This is not a perfect solution, but hopefully a good start if you want to keep the bar width showing "something" that is also repeated as a node.

For details please have a look at the comments in the code.

% PGFPlots v1.15
    \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}
\documentclass[border=5pt]{standalone}
\usepackage{filecontents}
\usepackage{pgfplots, pgfplotstable}
\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        xticklabels from table={data.csv}{name},
        typeset ticklabels with strut,
        % to align the `xticklabels` properly
        % (although they overlap now)
        xtick=data,
        % to remove the "10^4"
        scaled ticks=false,
        % to add nodes with the `width' value
        nodes near coords,
        point meta=explicit,
    ]
        \addplot [ybar interval, fill=yellow] table [
            create on use/xaccum/.style={
                create col/expr=\pgfmathaccuma+\prevrow{width}
            },
            x=xaccum,
            y=height,
            meta=width,     % <-- also added to display the `width' value
        ] {data.csv};
    \end{axis}
\end{tikzpicture}
\end{document}

image showing the result of above code

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.