How to add a legend to a figure with only a `(fig, ax)` tuple in hand?

I only have a (fig, ax) tuple of a figure in hand. Now I want to add a legend for this figure. How can I do?

julia> fig, ax
(Scene (600px, 450px):
  0 Plots
  1 Child Scene:
    └ Scene (600px, 450px), Axis (5 plots))

julia> ax
Axis with 5 plots:
 ┣━ Plot{Makie.contourf, Tuple{LinRange{Float64, Int64}, LinRange{Float64, Int64}, Matrix{Float32}}}
 ┣━ Scatter{Tuple{Vector{Point{2, Float64}}}}
 ┣━ Plot{Makie.contour, Tuple{LinRange{Float64, Int64}, LinRange{Float64, Int64}, Matrix{Float32}}}
 ┣━ Scatter{Tuple{Vector{Point{2, Float64}}}}
 ┗━ Scatter{Tuple{Vector{Point{2, Float64}}}}

Something like axislegend(ax)?

Thank your for your response. But the key issue is there are no plots with labels in the given axis:

julia> axislegend(ax)
ERROR: There are no plots with labels in the given axis that can be put in the legend. Supply labels to plotting functions like `plot(args...; label = "My label")`

I wonder if I can add labels to each axis through ax?

You can pass a keyword argument to your plots:
scatter(x, y; label="my label")
or pass the labels as keyword arguments to the legend function:
https://docs.makie.org/v0.21/reference/blocks/legend#Creating-A-Legend-From-Elements

How can I get each axis from ax?

ax.scene.plots

1 Like

Ah! Thanks!