# Makie: how to remove a legend from an axis

**URL:** <https://discourse.julialang.org/t/makie-how-to-remove-a-legend-from-an-axis/65876>\
**Category:** Visualization\
**Tags:** makie\
**Created:** [August 5, 2021, 12:11pm UTC](https://discourse.julialang.org/t/makie-how-to-remove-a-legend-from-an-axis/65876 "2021-08-05T12:11:43Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![HenriDeh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrideh/32/8316_2.png) [@HenriDeh](https://discourse.julialang.org/u/HenriDeh)\
**Post date:** [August 5, 2021, 12:11pm UTC](https://discourse.julialang.org/t/makie-how-to-remove-a-legend-from-an-axis/65876/1 "2021-08-05T12:11:43Z")

</div>

Hi, I’m making a dashboard and I’m using a lot of mutating functions to make an interactive plot that changes its content based on a menu selection. When I change the plot, I remove the former one from the axis using `empty!(ax)`. The problem is that the legend is not linked to the axis but to the `GridPosition` of the axis. But I can’t find it in the fields of the `GridPosition` object.  
Here’s a MWE

```julia
using GLMakie
p = lines(rand(10), label = "foo")
function add_a_legend!(p) = axislegend(p.axis); return nothing end
add_a_legend!(p)
empty!(p.axis)

```

The legend is still there. But because I don’t have the legend variable in scope, I can’t use `delete!` directly. And I also can’t find it when I dig the `p.figure[1,1]` object.

I can change the function so that it returns the legend but I find this really cumbersome/impractical to have to return the Legend all the way up the callstack, and thus change all the functions. Is there a way to do this that I am not aware of ?

It would be nice if something like `empty!(p.figure[1,1, Inner()])` worked.

---

<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:** [August 5, 2021, 12:57pm UTC](https://discourse.julialang.org/t/makie-how-to-remove-a-legend-from-an-axis/65876/2 "2021-08-05T12:57:34Z")

</div>

p.figure.content should have it

---

<div class="post-metadata">

**Author:** ![HenriDeh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrideh/32/8316_2.png) [@HenriDeh](https://discourse.julialang.org/u/HenriDeh)\
**Post date:** [August 5, 2021, 1:06pm UTC](https://discourse.julialang.org/t/makie-how-to-remove-a-legend-from-an-axis/65876/3 "2021-08-05T13:06:15Z")

</div>

Thank you

---

<div class="post-metadata">

**Author:** ![HenriDeh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrideh/32/8316_2.png) [@HenriDeh](https://discourse.julialang.org/u/HenriDeh)\
**Post date:** [August 5, 2021, 1:18pm UTC](https://discourse.julialang.org/t/makie-how-to-remove-a-legend-from-an-axis/65876/4 "2021-08-05T13:18:32Z")

</div>

Although,  
if I have two axes in the figure, each with a legend, how do I know which legend is on the axis I am emptying ?

```julia
using GLMakie
f = Figure()
ax1 = f[1,1] = Axis(f)
ax2 = f[1,2] = Axis(f)
p1 = lines!(ax1, rand(10), label = "foo")
p2 = lines!(ax2, rand(10), label = "bar")
function add_a_legend!(ax) axislegend(ax); return nothing end
add_a_legend!(ax1)
add_a_legend!(ax2)
empty!(ax1)
[delete!(leg) for leg in f.content if leg isa Legend]

```

will remove the Legend from both axis, how do I identify which is where in the figure ?

---

<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:** [August 5, 2021, 1:53pm UTC](https://discourse.julialang.org/t/makie-how-to-remove-a-legend-from-an-axis/65876/5 "2021-08-05T13:53:27Z")

</div>

I don’t think there’s an easy way to check that currently. The link to the axis exists only through the ax.scene.px\_area observable. You could theoretically check which axis has the scene whose px\_area observable is referenced in the legend’s suggestedbbox observable

---

<div class="post-metadata">

**Author:** ![HenriDeh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrideh/32/8316_2.png) [@HenriDeh](https://discourse.julialang.org/u/HenriDeh)\
**Post date:** [August 5, 2021, 2:00pm UTC](https://discourse.julialang.org/t/makie-how-to-remove-a-legend-from-an-axis/65876/6 "2021-08-05T14:00:44Z")

</div>

I though I had found a workaround using SubLayouts:

```julia
using GLMakie
f = Figure()
gl1 = f[1,1] = GridLayout()
gl2 = f[1,2] = GridLayout()
ax1 = gl1[1,1] = Axis(f)
ax2 = gl2[1,1] = Axis(f)
p1 = lines!(ax1, rand(10), label = "foo")
p2 = lines!(ax2, rand(10), label = "bar")
function add_a_legend!(ax) axislegend(ax); return nothing end
add_a_legend!(ax1)
add_a_legend!(ax2)
empty!(ax1)
for leg in contents(gl1) 
    if leg isa Legend
        delete!(leg)
    end
end

```

but `contents` only contains the Axis, not the legend. And I found that this is because I use `axislegend()`. Doing it directly with the `Legend` constructor works:

```julia
f = Figure()
gl1 = f[1,1] = GridLayout()
gl2 = f[1,2] = GridLayout()
ax1 = gl1[1,1] = Axis(f)
ax2 = gl2[1,1] = Axis(f)
p1 = lines!(ax1, rand(10), label = "foo")
p2 = lines!(ax2, rand(10), label = "bar")
Legend(gl1[1,1], [p1], ["foo"], tellwidth = false, tellhight = false)
contents(gl1) # contains the legend
for leg in contents(gl1) 
    if leg isa Legend
        delete!(leg)
    end
end # correctly deletes the left legend

```

Do you want me to open an issue on this ?

---

<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:** [August 5, 2021, 4:24pm UTC](https://discourse.julialang.org/t/makie-how-to-remove-a-legend-from-an-axis/65876/7 "2021-08-05T16:24:14Z")

</div>

Sure go ahead, I’m not sure in what other way I should link legend and axis, because legends in makie don’t have a tight one to one correspondence with Axes

---

<div class="post-metadata">

**Author:** ![barnett567](https://avatars.discourse-cdn.com/v4/letter/b/e0b2c6/32.png) [@barnett567](https://discourse.julialang.org/u/barnett567)\
**Post date:** [October 14, 2023, 6:28pm UTC](https://discourse.julialang.org/t/makie-how-to-remove-a-legend-from-an-axis/65876/8 "2023-10-14T18:28:22Z")

</div>

I know this is 2-year-old post, but I found an easier way is simply to store the `Legend` object for deletion later:

```julia
f=Figure()
ax = Axis(f[1,1])
lines!(ax, rand(10),label="foo")
leg = axislegend()
# do other stuff here
delete!(leg) # does what you want
empty!(ax) # clears the lines

```

Hope this helps (I am new to GLMakie…)
