I would like to insert a centered title in figure containing a pie chart and a colorbar with CairoMakie.
I successfully included my colorbar at the right of the pie (see fig 2)
I tried to use the Figure()
function to position my differents object and inserting a main title centered at the top of the pie chart and the colorbar. My title and my color bar appears but my pie chart remain invisible
(see fig 3) .
using CairoMakie
using ColorSchemes
numberofpeople=Dict(
"A"=>135000000
,"B"=>90000
,"C"=>12500000
,"D"=>18000000
)
datas = collect(values(numberofpeople))
labels = collect(keys(numberofpeople))
colors =[ get(ColorSchemes.Spectral_9,i) for i in (0:1/(length(datas)-1):1) ]
fa = Figure()
fig, ax, plt =Makie.pie(datas,
color =colors ,
label = labels,
radius = 4,
strokecolor = :black,
strokewidth = 2,
axis = (autolimitaspect = 1,),
)
save("fig1.png", fig)
cb = Colorbar(fig[1,2],
colormap = cgrad(reverse(colors), categorical = true),
width=70,
limits = (0, 11),
minorticksvisible=false,
ticksvisible=false
)
save("fig2.png", fig)
lab=Label(fa[1, 1:2], "My title",
font = "Nimbus Sans Bold",fontsize = 42,
padding = (0, 0, 0, 0))
cb = Colorbar(fa[2,2],
colormap = cgrad(reverse(colors), categorical = true),
width=70,
limits = (0, 11),
minorticksvisible=false,
ticksvisible=false
)
fa[2,1]=ax
save("fig3.png", fa)
I think I don't insert my pie chart correctly but I don't understand why.
How could I correctly insert my pie chart in the figure ?
Is there another to do what I want ?
To answer your first question, you presently cannot copy an Axis object from one Figure to another, per this linked discourse question:
You can't currently copy plots or axes from figure to figure. In the future we might make this possible.
So you have to use the Figure where you initially built the pie chart (or rebuild the pie chart in the new Figure):
fig, ax, plt = Makie.pie(
datas,
color = colors,
label = labels,
radius = 4,
strokecolor = :black,
strokewidth = 2,
axis = (
autolimitaspect = 1,
)
)
Colorbar(
fig[1, 2],
colormap = cgrad(reverse(colors), categorical = true),
width = 70,
limits = (0, 11),
minorticksvisible = false,
ticksvisible = false,
)
Label(
fig[begin-1, 1:2],
"My title",
font = "Nimbus Sans Bold",
fontsize = 42,
padding = (0, 0, 0, 0),
)
Note that in this example I use fig[begin-1, 1:2]
to specify a grid layout row above the current top row. This adds a row to the grid layout at the top. If I had specified fig[1, 1:2]
instead, the Label would have been written over the pie chart and the Colorbar, which is likely not what you want:
To answer your second question, it might be easier to specify title
, titlefont
, and titlesize
as properties of the axis
when calling Makie.pie
, like so:
fig, ax, plt = Makie.pie(
datas,
color = colors,
label = labels,
radius = 4,
strokecolor = :black,
strokewidth = 2,
axis = (
autolimitaspect = 1,
title = "My title",
titlefont = "Nimbus Sans Bold",
titlesize = 42,
)
)
Colorbar(
fig[1, 2],
colormap = cgrad(reverse(colors), categorical = true),
width = 70,
limits = (0, 11),
minorticksvisible = false,
ticksvisible = false,
)
This does not create a new cell in the grid layout: it simply draws a title in the margin space above the Axis. However, note that the title in this example is only centered over the pie chart Axis and not the space including both the pie chart and the Colorbar.