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

**URL:** <https://discourse.julialang.org/t/how-to-add-a-legend-to-a-figure-with-only-a-fig-ax-tuple-in-hand/119795>\
**Category:** Visualization\
**Tags:** question, makie\
**Created:** [September 24, 2024, 7:16am UTC](https://discourse.julialang.org/t/how-to-add-a-legend-to-a-figure-with-only-a-fig-ax-tuple-in-hand/119795 "2024-09-24T07:16:30Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![WuSiren](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wusiren/32/42529_2.png) [@WuSiren](https://discourse.julialang.org/u/WuSiren)\
**Post date:** [September 24, 2024, 7:16am UTC](https://discourse.julialang.org/t/how-to-add-a-legend-to-a-figure-with-only-a-fig-ax-tuple-in-hand/119795/1 "2024-09-24T07:16:30Z")

</div>

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
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}}}}

```

---

<div class="post-metadata">

**Author:** ![fedoroff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fedoroff/32/53209_2.png) [@fedoroff](https://discourse.julialang.org/u/fedoroff)\
**Post date:** [September 24, 2024, 7:48am UTC](https://discourse.julialang.org/t/how-to-add-a-legend-to-a-figure-with-only-a-fig-ax-tuple-in-hand/119795/2 "2024-09-24T07:48:59Z")

</div>

Something like `axislegend(ax)`?

---

<div class="post-metadata">

**Author:** ![WuSiren](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wusiren/32/42529_2.png) [@WuSiren](https://discourse.julialang.org/u/WuSiren)\
**Post date:** [September 24, 2024, 7:52am UTC](https://discourse.julialang.org/t/how-to-add-a-legend-to-a-figure-with-only-a-fig-ax-tuple-in-hand/119795/3 "2024-09-24T07:52:27Z")

</div>

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

```julia
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`?

---

<div class="post-metadata">

**Author:** ![fedoroff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fedoroff/32/53209_2.png) [@fedoroff](https://discourse.julialang.org/u/fedoroff)\
**Post date:** [September 24, 2024, 8:01am UTC](https://discourse.julialang.org/t/how-to-add-a-legend-to-a-figure-with-only-a-fig-ax-tuple-in-hand/119795/4 "2024-09-24T08:01:57Z")

</div>

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](https://docs.makie.org/v0.21/reference/blocks/legend#Creating-A-Legend-From-Elements)

---

<div class="post-metadata">

**Author:** ![WuSiren](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wusiren/32/42529_2.png) [@WuSiren](https://discourse.julialang.org/u/WuSiren)\
**Post date:** [September 24, 2024, 8:14am UTC](https://discourse.julialang.org/t/how-to-add-a-legend-to-a-figure-with-only-a-fig-ax-tuple-in-hand/119795/5 "2024-09-24T08:14:27Z")

</div>

How can I get each axis from `ax`?

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)\
**Post date:** [September 24, 2024, 8:47am UTC](https://discourse.julialang.org/t/how-to-add-a-legend-to-a-figure-with-only-a-fig-ax-tuple-in-hand/119795/6 "2024-09-24T08:47:52Z")

</div>

`ax.scene.plots`

---

<div class="post-metadata">

**Author:** ![WuSiren](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wusiren/32/42529_2.png) [@WuSiren](https://discourse.julialang.org/u/WuSiren)\
**Post date:** [September 24, 2024, 8:51am UTC](https://discourse.julialang.org/t/how-to-add-a-legend-to-a-figure-with-only-a-fig-ax-tuple-in-hand/119795/7 "2024-09-24T08:51:27Z")

</div>

Ah! Thanks!
