# Labels in pie charts in Makie

**URL:** <https://discourse.julialang.org/t/labels-in-pie-charts-in-makie/94088>\
**Category:** Visualization\
**Tags:** makie\
**Created:** [February 5, 2023, 11:56am UTC](https://discourse.julialang.org/t/labels-in-pie-charts-in-makie/94088 "2023-02-05T11:56:28Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![mpirke](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mpirke/32/44837_2.png) [@mpirke](https://discourse.julialang.org/u/mpirke)\
**Post date:** [February 5, 2023, 11:56am UTC](https://discourse.julialang.org/t/labels-in-pie-charts-in-makie/94088/1 "2023-02-05T11:56:28Z")

</div>

Hi there!

I was wondering how to put labels on a pie chart in Makie. Like in the example shown on the page.

```julia
using CairoMakie

data = [36, 12, 68, 5, 42, 27]
colors = [:yellow, :orange, :red, :blue, :purple, :green]

f, ax, plt = pie(data,
                 color = colors,
                 radius = 4,
                 inner_radius = 2,
                 strokecolor = :white,
                 strokewidth = 5,
                 axis = (autolimitaspect = 1, )
                )

f

```

How could I for example use to colors as a label, all the things I tried did not work out…

Thanks!

---

<div class="post-metadata">

**Author:** ![lazarusA](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lazarusa/32/6571_2.png) [@lazarusA](https://discourse.julialang.org/u/lazarusA)\
**Post date:** [February 5, 2023, 4:11pm UTC](https://discourse.julialang.org/t/labels-in-pie-charts-in-makie/94088/2 "2023-02-05T16:11:02Z")

</div>

what about doing:

```julia
using GLMakie
data = [36, 12, 68, 5, 42, 27]
colors = [:yellow, :orange, :red, :blue, :purple, :green]
nc = length(colors)
nδ = 1/nc

f, ax, plt = pie(data,
                 color = colors,
                 radius = 4,
                 inner_radius = 2,
                 strokecolor = :white,
                 strokewidth = 5,
                 axis = (autolimitaspect = 1, )
                )
cbar = Colorbar(f[1,2], colormap = cgrad(colors, categorical = true))
cbar.ticks = (range(0+nδ/2, 1-nδ/2, nc), string.(colors))
f

```

 ![Screenshot 2023-02-05 at 17.10.37](https://global.discourse-cdn.com/julialang/original/3X/f/5/f5d681fcf96145b84583807a3e4243a2d1ab2e7a.png)

---

<div class="post-metadata">

**Author:** ![mpirke](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mpirke/32/44837_2.png) [@mpirke](https://discourse.julialang.org/u/mpirke)\
**Post date:** [February 5, 2023, 5:47pm UTC](https://discourse.julialang.org/t/labels-in-pie-charts-in-makie/94088/3 "2023-02-05T17:47:36Z")

</div>

Yes good idea, this works! Thanks!  
But still it would be better if one could label die pieces immediately.

---

<div class="post-metadata">

**Author:** ![malefors](https://avatars.discourse-cdn.com/v4/letter/m/a3d4f5/32.png) [@malefors](https://discourse.julialang.org/u/malefors)\
**Post date:** [March 8, 2023, 11:00am UTC](https://discourse.julialang.org/t/labels-in-pie-charts-in-makie/94088/4 "2023-03-08T11:00:13Z")

</div>

I also struggled with this. I ended up creating a dummy scatter plot object that matched the data points for the pie chart.

```julia
let 
    data = [36, 12, 68, 5, 42, 27]
    colors = [:yellow, :orange, :red, :blue, :purple, :green]

    f, ax, plt = pie(data,
                    color = colors,
                    radius = 4,
                    inner_radius = 2,
                    strokecolor = :white,
                    strokewidth = 5,
                    axis = (autolimitaspect = 1, )
                    )

    for x in 1:length(data)
        scatter!(ax, zeros(10), zeros(10), color = colors[x], label=string(colors[x]))
    end
    scatter!(ax, zeros(10), zeros(10), color = :white)
    axislegend()
    f
end

```

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [March 8, 2023, 2:25pm UTC](https://discourse.julialang.org/t/labels-in-pie-charts-in-makie/94088/5 "2023-03-08T14:25:08Z")

</div>

Would such an annotation be good enough?

 ![Makie_pie_chart_annotated](https://global.discourse-cdn.com/julialang/original/3X/6/3/631518bb9bcfc0bb304162df356d11e5c9945eab.png)

---

<div class="post-metadata">

**Author:** ![fabiangans](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fabiangans/32/2624_2.png) [@fabiangans](https://discourse.julialang.org/u/fabiangans)\
**Post date:** [April 5, 2023, 1:09pm UTC](https://discourse.julialang.org/t/labels-in-pie-charts-in-makie/94088/6 "2023-04-05T13:09:06Z")

</div>

Hi @rafael.guerra would you mind to share the code you used for your plot?

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [April 5, 2023, 2:13pm UTC](https://discourse.julialang.org/t/labels-in-pie-charts-in-makie/94088/7 "2023-04-05T14:13:44Z")

</div>

@fabiangans, this was some time ago, but I remember to have tested a number of things, as it seemed difficult to tame the result when we enlarge the Makie window (_it is easier not to turn the labels over for better reading_).

As requested, here is basically what I got when turning the labels over:

```julia
using Makie, GLMakie

data = [36, 12, 68, 5, 42, 27]
colors = [:yellow, :orange, :red, :blue, :purple, :green]
labels = ["label$i" for i in 1:length(data)]
r0, r1 = 2, 4

fig, ax, plt = Makie.pie(data, color=colors, radius=r1, inner_radius=r0,
        strokecolor=:white, strokewidth=5, axis=(autolimitaspect = 1, )
)

rot(θ) = 90<θ<270 ? θ*π/180 - π : θ*π/180
radius(θ, r) = 90<θ<270 ? 1.25*r : 1.05*r # adjust function of label sizes

θ = (cumsum(data) - data/2) .* 360/sum(data)
scθ = sincosd.(θ)

for (li, θi, sci) in zip(labels, θ, scθ)
    ri = radius(θi, r1)
    text!(ax, li, position=ri.*(sci[2], sci[1]), rotation=rot(θi), fontsize=16, space=:data)
end

Makie.ylims!(ax, -5.5, 5.5)
hidedecorations!(ax) # hides ticks, grid and lables
hidespines!(ax) # hide the frame

```

---

<div class="post-metadata">

**Author:** ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)\
**Post date:** [May 23, 2023, 7:07pm UTC](https://discourse.julialang.org/t/labels-in-pie-charts-in-makie/94088/8 "2023-05-23T19:07:46Z")

</div>

I just got hit by this. The pie plot code in Makie needs some love… For now I’m using a hand-made legend:

```julia
data = [36, 12, 68, 5, 42, 27]
colors = Makie.wong_colors()[1:length(data)]
labels = ["label$i" for i in 1:length(data)]

fig, ax, plt = pie(data, color=colors, strokecolor=:transparent, axis=(; aspect=DataAspect()))
hidedecorations!(ax)
hidespines!(ax)
Legend(fig[1,2], [PolyElement(color=c) for c in colors], labels, framevisible=false)
fig

```

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

---

<div class="post-metadata">

**Author:** ![JonathanLilly](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jonathanlilly/32/49070_2.png) [@JonathanLilly](https://discourse.julialang.org/u/JonathanLilly)\
**Post date:** [January 27, 2025, 5:07pm UTC](https://discourse.julialang.org/t/labels-in-pie-charts-in-makie/94088/9 "2025-01-27T17:07:28Z")

</div>

Thanks for this. Easy to customize and worked like a charm.
