Makie - single bar label for stacked bar

I have a stacked bar plot, and can add a label to each block in the stack with bar_labels=:y. But this is noisy, and I’d like to only add a single label to the top of the stack, showing the total stacked value. How do I do that?

(here’s an example, borrowed from Bar Label Alignment · Issue #3160 · MakieOrg/Makie.jl · GitHub)


tbl = (x = [1, 1, 1, 2, 2, 2, 3, 3, 3],
       height = 0.1:0.1:0.9,
       grp = [1, 2, 3, 1, 2, 3, 1, 2, 3],
       grp1 = [1, 2, 2, 1, 1, 2, 1, 1, 2],
       grp2 = [1, 1, 2, 1, 2, 1, 1, 2, 1]
       )

barplot(
        tbl.x, tbl.height,
        stack = tbl.grp,
        color = tbl.grp,
        bar_labels = :y,
        axis = (xticks = (1:3, ["left", "middle", "right"]),),
    )

I’d like it to show only the labels at the top of each bar, ie “0.6”, “1.5” and “2.4”

thanks!

There’s no inbuilt convenience function to do that, but you can pass a vector of labels to bar_labels and you can make all that you want to hide ""

Perfect!

using CairoMakie, Makie

tbl = (x = [1, 1, 1, 2, 2, 2, 3, 3, 3],
       height = 0.1:0.1:0.9,
       grp = [1, 2, 3, 1, 2, 3, 1, 2, 3],
       )

barplot(
        tbl.x, tbl.height,
        stack = tbl.grp,
        color = tbl.grp,
        bar_labels = [g==3 ? "$h" : "" for (g,h) in zip(tbl.grp, tbl.height)],
        axis = (xticks = (1:3, ["left", "middle", "right"]),),
    )