Wind Rose 'hard' stacked barplot edges in GLMakie

Hello,

I am aiming to draw a wind rose similar to the below (from PlotlyJS.jl) in Makie.

I can plot a stacked barplot into a PolarAxis:

using GLMakie;
using ColorSchemes;

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

f = Figure(width=200, height=200);

ax = PolarAxis(f[1, 1],
    rticks=1:0.1:1,
    );

barplot!(ax,
    tbl.cat, 
    tbl.height,
    stack = tbl.grp1,
    color = tbl.grp2,
    colormap=:lightrainbow,
);

f

This results in the following (in GLMakie):

Is there any way to get the edges to be convex rather than straight? It looks a bit peculiar.

Thanks in advance!

There isn’t a simple way to get that yet. Somewhere between the barplot and the Polar transform applying, the Rect2f’s barplot generates need to get interpolated. You can hack Makie to do that like this:

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

f = Figure(width=200, height=200);

ax = PolarAxis(f[1, 1],
    rticks=1:0.1:1,
    );

p = barplot!(ax,
    tbl.cat, 
    tbl.height,
    stack = tbl.grp1,
    color = tbl.grp2,
    colormap=:lightrainbow,
);
pp = popat!(p.plots, findfirst(x -> x isa Poly, p.plots))
polys = map(pp.converted[1]) do rects
    map(rects) do rect
        N_steps = 100
        mini = minimum(rect); maxi = maximum(rect)
        ps = Point2f[mini, Point2f(mini[1], maxi[2]), maxi, Point2f(maxi[1], mini[2]), mini]
        ps = map(range(0, 4, length = N_steps)) do f
            ps[1] * max(0, 1-f) + 
            ps[2] * max(0, 1 - abs(f-1)) + 
            ps[3] * max(0, 1 - abs(f-2)) + 
            ps[4] * max(0, 1 - abs(f-3)) + 
            ps[5] * max(0, 1 - abs(f-4))
        end
        Makie.Polygon(ps)
    end
end
poly!(p, pp.attributes, polys)

f

This will fail if you display before the poly!() call. Also worth an issue I think

4 Likes

This is great! thanks for your detailed reply. I will have a look to see whether I can get these polygons to work for my data.

When you suggest an ‘issue’, do you mean to open one (in github?) for the failure of poly! if display has been previously called or are you referring to the entire wind rose?

The result for actual data is very nice. Thanks again

1 Like

I mean a github issue for the entire wind rose, i.e. that barplot doesn’t curve in a PolarAxis.

The display failure is expected. It happens because Makie expects a plot to be done (in terms of child plots) when added to a scene. It just happens to work in that case, because window initialization has to look through all the plots.

I created this as a feature request: