Makie.jl - How can I have a label on top of an image plot?

Hi,
I am trying to label some plots containing images, but somehow I fail to have a label on top of an image. Here is a minimal working example :

using GLMakie, FileIO
img = load(assetpath("cow.png"))
f = Figure()
image(f[1, 1], img, axis = (title = "Default",))

Box(f[1,1], halign=:left, valign=:top, tellwidth=false, tellheight=false, color=:white, width=35, height=30)
Label(f[1,1], "(a)", padding=(5,5,0,0), halign=:left, valign=:top, tellwidth=false, tellheight=false, textsize=20)
f

This gives the following result :

I saw in the documentation that for CairoMakie it’s recommended to play with translate!, but you can’t translate! an Label, so I’m a bit stuck here.

Thanks for your help !

You can translate! plots and scenes, but not layout elements. To make the Box and the Label visible you probably need something like

ax, p = image(fig[1, 1], ...)
translate!(p, 0, 0, -1)

If you want more control over your label you could try making it manually.

mesh!(fig.scene, Rect2f(200, 200, 50, 20))
lines!(fig.scene, Rect2f(200, 200, 50, 20))
p = text!(fig.scene, "(a)", position = Point2f(205, 205), align = (:left, :bottom))

fig.scene is in pixel units which is probably more convenient than axis. boundingbox(p) might also be helpful.

Hi, sorry for the late answer.

I tried again today, and after some messing around, here is what i got :

using GLMakie, FileIO

img = load(assetpath("cow.png"))
fig = Figure()
image(fig[1, 1], img, axis = (title = "Default",))
image(fig[1, 2], img, axis = (title = "Default",))
image(fig[1, 3], img, axis = (title = "Default",))
image(fig[2, 1], img, axis = (title = "Default",))
image(fig[2, 2], img, axis = (title = "Default",))
image(fig[2, 3], img, axis = (title = "Default",))

labels = [
          "a" "b" "c" 
          "d" "e" "f"
         ]
nb_rows,nb_cols = size(fig.layout)
square_size = 25
for i ∈ 1:nb_rows, j ∈ 1:nb_cols
  px_area = content(fig.layout[i,j]).scene.px_area[]

  x,y = px_area.origin
  _,h = px_area.widths

  b = poly!(fig.scene, 
            Point2f[
                    (x, y+h), 
                    (x+square_size, y+h), 
                    (x+square_size, y+h-square_size), 
                    (x, y+h-square_size)
                   ], 
            color = :white, 
            strokecolor = :black, 
            strokewidth = 1
           )
  t = text!(
            fig.scene, 
            labels[i,j], position = Point2f(x+12, y+h-25+12),
            align = (:center, :center), textsize=20,
           )
  translate!(b, 0, 0, 100)
  translate!(t, 0, 0, 100)
end

save("testfigure.png", fig)

And the result :

thanks a lot for the help @ffreyer !