# Polar Histogram

**URL:** <https://discourse.julialang.org/t/polar-histogram/103113>\
**Category:** Visualization\
**Tags:** makie\
**Created:** [August 23, 2023, 2:17pm UTC](https://discourse.julialang.org/t/polar-histogram/103113 "2023-08-23T14:17:57Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![VincentPoupart](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vincentpoupart/32/49479_2.png) [@VincentPoupart](https://discourse.julialang.org/u/VincentPoupart)\
**Post date:** [August 23, 2023, 2:17pm UTC](https://discourse.julialang.org/t/polar-histogram/103113/1 "2023-08-23T14:17:57Z")

</div>

Hi! I wrote a little function to plot polar histograms using PolarAxis. I wonder if it’s a good idea to add something like this to makie?

```julia
using StatsBase
using Distributions
using CairoMakie

#set of angles in radian
angles = deg2rad.(rand(Normal(45,15), 100))
#bins limits in degrees
bins = [0, 15, 30, 45, 60, 75, 90]

function polarhist(angles, bins)

    bins = deg2rad.(bins)
    h = fit(Histogram, angles, bins)
    x = collect(h.edges[1])
    y = h.weights ./ sum(h.weights)
    f = Figure()
    ax = PolarAxis(f[1, 1], title="Polar Histogram")

    for i in eachindex(y)
        if y[i] > 0
            poly!(ax, [(0, 0), (y[i], x[i]), (y[i], x[i+1])], [1, 2, 3])
        end
    end
    return f
end

polarhist(angles, bins)

```

---

<div class="post-metadata">

**Author:** ![The\_Mastermage](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/the_mastermage/32/49444_2.png) [@The\_Mastermage](https://discourse.julialang.org/u/The_Mastermage)\
**Post date:** [August 28, 2024, 3:13pm UTC](https://discourse.julialang.org/t/polar-histogram/103113/2 "2024-08-28T15:13:49Z")

</div>

I was searching for something like this and it would be realy practical if something like this existed within Makie.jl

---

<div class="post-metadata">

**Author:** ![sdanisch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sdanisch/32/1406_2.png) [@sdanisch](https://discourse.julialang.org/u/sdanisch)\
**Post date:** [August 28, 2024, 3:41pm UTC](https://discourse.julialang.org/t/polar-histogram/103113/3 "2024-08-28T15:41:13Z")

</div>

Barplot does work with PolarAxis:

```julia
    f = Figure()
    ax = PolarAxis(f[1, 1], title="Polar Histogram")
    r = 0:0.1:2pi
    barplot!(ax, r, rand(length(r)), color=r)
    f

```

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

So it should mainly be about getting the x values on the angle range.

---

<div class="post-metadata">

**Author:** ![The\_Mastermage](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/the_mastermage/32/49444_2.png) [@The\_Mastermage](https://discourse.julialang.org/u/The_Mastermage)\
**Post date:** [September 9, 2024, 2:25pm UTC](https://discourse.julialang.org/t/polar-histogram/103113/4 "2024-09-09T14:25:50Z")

</div>

Is that with Makie? Because i remember this issue existing: [PolarAxis histogram fails in CairoMakie · Issue #3175 · MakieOrg/Makie.jl · GitHub](https://github.com/MakieOrg/Makie.jl/issues/3175)

---

<div class="post-metadata">

**Author:** ![sdanisch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sdanisch/32/1406_2.png) [@sdanisch](https://discourse.julialang.org/u/sdanisch)\
**Post date:** [September 9, 2024, 2:38pm UTC](https://discourse.julialang.org/t/polar-histogram/103113/5 "2024-09-09T14:38:19Z")

</div>

Yeah I didn’t realize that this seems to be broken in cairomakie

---

<div class="post-metadata">

**Author:** ![The\_Mastermage](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/the_mastermage/32/49444_2.png) [@The\_Mastermage](https://discourse.julialang.org/u/The_Mastermage)\
**Post date:** [September 9, 2024, 3:04pm UTC](https://discourse.julialang.org/t/polar-histogram/103113/6 "2024-09-09T15:04:35Z")

</div>

But still thank you. Lets just hope they fix it in CairoMakie so i have less to suffer in Matplotlib.

---

<div class="post-metadata">

**Author:** ![VincentPoupart](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vincentpoupart/32/49479_2.png) [@VincentPoupart](https://discourse.julialang.org/u/VincentPoupart)\
**Post date:** [September 9, 2024, 4:07pm UTC](https://discourse.julialang.org/t/polar-histogram/103113/7 "2024-09-09T16:07:01Z")

</div>

Hi! I had to use poly to make it works with CairoMakie. Here is how I succeded:

```julia
using CairoMakie
using StatsBase
CairoMakie.activate!(type = "png")
angles = 360 * rand(1000)
f = Figure()
function PolarHist(angles, figure, limits, title, colour, transparancy, graphposition)
    anglesRad = deg2rad.(angles)
    binsRad = deg2rad.(collect(0:15:limits))
    h = fit(Histogram, anglesRad, binsRad)
    x = collect(h.edges[1])
    y = convert.(Float64, h.weights)
    ax = PolarAxis(figure[1, graphposition],
        title="$title",
        thetaticks=((collect(0:15:limits)) .* 2*pi ./ limits, string.(string.(collect(0:15:limits)), "°")),
        #rticks=collect(0:10:ceil(maximum(y) / 10)*10)
    )
    thetalims!(ax, 0, 2*pi )
    #rlims!(ax, 0, ceil(maximum(y) / 10) * 10)
    for i in eachindex(y)
        if y[i] > 0
            CairoMakie.poly!(ax, [(0.0, 0.0), (x[i], y[i]), (x[i+1], y[i])], [1, 2, 3], color=Makie.wong_colors()[colour], alpha=transparancy, strokewidth=2, strokecolor=:black)
        end
    end
end
PolarHist(angles, f, 360, "polar histogram", 3, 0.5, 1)
f

```

 ![PolarHistogram](https://global.discourse-cdn.com/julialang/original/3X/a/5/a5571b06c461a2965e5db6b843e0186f26017f4f.png)

---

<div class="post-metadata">

**Author:** ![The\_Mastermage](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/the_mastermage/32/49444_2.png) [@The\_Mastermage](https://discourse.julialang.org/u/The_Mastermage)\
**Post date:** [September 10, 2024, 5:08pm UTC](https://discourse.julialang.org/t/polar-histogram/103113/8 "2024-09-10T17:08:25Z")

</div>

Oh god thats amazing. I love it.
