# Wind Rose 'hard' stacked barplot edges in GLMakie

**URL:** <https://discourse.julialang.org/t/wind-rose-hard-stacked-barplot-edges-in-glmakie/124277>\
**Category:** Visualization\
**Tags:** makie, glmakie\
**Created:** [December 30, 2024, 3:42pm UTC](https://discourse.julialang.org/t/wind-rose-hard-stacked-barplot-edges-in-glmakie/124277 "2024-12-30T15:42:45Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ewillcox](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ewillcox/32/22980_2.png) [@ewillcox](https://discourse.julialang.org/u/ewillcox)\
**Post date:** [December 30, 2024, 3:42pm UTC](https://discourse.julialang.org/t/wind-rose-hard-stacked-barplot-edges-in-glmakie/124277/1 "2024-12-30T15:42:45Z")

</div>

Hello,

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

 ![image](https://global.discourse-cdn.com/julialang/original/3X/f/a/fae51cd58c4bd72c90e2d3c06bf25cccf0bbc3d0.png)

I can plot a stacked [barplot](https://docs.makie.org/stable/reference/plots/barplot) into a [PolarAxis](https://docs.makie.org/v0.21/reference/blocks/polaraxis):

```julia
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):

 ![image](https://global.discourse-cdn.com/julialang/original/3X/b/d/bdcffbe9117f3c0a0a4ca71d876a9bebb4ed756d.png)

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

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![ffreyer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ffreyer/32/21569_2.png) [@ffreyer](https://discourse.julialang.org/u/ffreyer)\
**Post date:** [December 30, 2024, 11:39pm UTC](https://discourse.julialang.org/t/wind-rose-hard-stacked-barplot-edges-in-glmakie/124277/2 "2024-12-30T23:39:30Z")

</div>

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:

```julia
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

```

 ![image](https://global.discourse-cdn.com/julialang/original/3X/e/4/e4c3fe61587bcecd519173edd3f12004cbb51138.png)

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

---

<div class="post-metadata">

**Author:** ![ewillcox](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ewillcox/32/22980_2.png) [@ewillcox](https://discourse.julialang.org/u/ewillcox)\
**Post date:** [December 31, 2024, 8:53am UTC](https://discourse.julialang.org/t/wind-rose-hard-stacked-barplot-edges-in-glmakie/124277/3 "2024-12-31T08:53:12Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![ewillcox](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ewillcox/32/22980_2.png) [@ewillcox](https://discourse.julialang.org/u/ewillcox)\
**Post date:** [December 31, 2024, 10:11am UTC](https://discourse.julialang.org/t/wind-rose-hard-stacked-barplot-edges-in-glmakie/124277/4 "2024-12-31T10:11:15Z")

</div>

The result for actual data is very nice. Thanks again

 ![image](https://global.discourse-cdn.com/julialang/original/3X/4/e/4ea0e0f29e1b1fe420e9512a413372f7ddbe1bea.png)

---

<div class="post-metadata">

**Author:** ![ffreyer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ffreyer/32/21569_2.png) [@ffreyer](https://discourse.julialang.org/u/ffreyer)\
**Post date:** [December 31, 2024, 2:08pm UTC](https://discourse.julialang.org/t/wind-rose-hard-stacked-barplot-edges-in-glmakie/124277/5 "2024-12-31T14:08:17Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ewillcox](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ewillcox/32/22980_2.png) [@ewillcox](https://discourse.julialang.org/u/ewillcox)\
**Post date:** [January 1, 2025, 9:16pm UTC](https://discourse.julialang.org/t/wind-rose-hard-stacked-barplot-edges-in-glmakie/124277/6 "2025-01-01T21:16:52Z")

</div>

I created this as a feature request:

> <https://github.com/MakieOrg/Makie.jl/issues/4698>
>
> \### Feature requested
> 
> Currently, when a barplot is plotted in Polar Axis(), the… edges are straight rather than curving along the circle as would be required for the creation of a wind rose. I asked a question about how to round the edges on \[this discourse thread\](https://discourse.julialang.org/t/wind-rose-hard-stacked-barplot-edges-in-glmakie/124277/4). I was provided with an answer there (using popat!(), map(), and poly!()) to redefine the polygons that make up the plot but of course it would be very nice to have this be the default for PolarAxis() or available as an option/argument in barplot!() itself. 
> 
> \### Examples for goal, problem description and temporary solution
> 
> \#### This is an equivalent plot in PlotlyJS.jl
> !\[Example of a wind rose from PlotlyJS.jl\](https://github.com/user-attachments/assets/3ec124ea-b6e9-43bf-a07d-01479b687dd1)
> 
> \#### A plain stacked barplot based on the examples in the documentation (in GLMakie) 
> !\[Image that represents the issue of flat instead of curved stacked bar ends\](https://github.com/user-attachments/assets/070ae06a-3547-4e40-bd5f-dd72e7d30fb6)
> 
> \#### The (temporary) solution provided in the discourse thread
> !\[The solution provided in the discourse thread\](https://github.com/user-attachments/assets/e5379b58-c953-4ae0-828e-2fb1ab82ee76)
