Labels in pie charts in Makie

Hi there!

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

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!

what about doing:

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

2 Likes

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

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

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
1 Like

Would such an annotation be good enough?

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

@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:

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
1 Like

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

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