# Makie : Update plot legend through Observables

**URL:** <https://discourse.julialang.org/t/makie-update-plot-legend-through-observables/86786>\
**Category:** Visualization\
**Tags:** question, plotting, glmakie\
**Created:** [September 5, 2022, 8:44am UTC](https://discourse.julialang.org/t/makie-update-plot-legend-through-observables/86786 "2022-09-05T08:44:48Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![dgs](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dgs/32/7015_2.png) [@dgs](https://discourse.julialang.org/u/dgs)\
**Post date:** [September 5, 2022, 8:44am UTC](https://discourse.julialang.org/t/makie-update-plot-legend-through-observables/86786/1 "2022-09-05T08:44:49Z")

</div>

Hello,

I am trying to a basic app with GLMakie with some user input.  
The (scatter) plot nicely updates with a slider which basically selects which dots to plots but I can’t seem to find a way to update the legend (through either axislegend or Legend) on the fly.

Is there a way to do this? Would you have an example?

Thanks a lot!

---

<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 5, 2022, 12:01pm UTC](https://discourse.julialang.org/t/makie-update-plot-legend-through-observables/86786/2 "2022-09-05T12:01:35Z")

</div>

In what way are you trying to update the legend? Labels? New entries?

---

<div class="post-metadata">

**Author:** ![dgs](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dgs/32/7015_2.png) [@dgs](https://discourse.julialang.org/u/dgs)\
**Post date:** [September 5, 2022, 2:09pm UTC](https://discourse.julialang.org/t/makie-update-plot-legend-through-observables/86786/3 "2022-09-05T14:09:10Z")

</div>

The plotted vertices have different statuses (ie. “data set 1”, “data set 2” ) which i would like to add as label to the graph.  
Depending on the slider value, the statuses and labels to display would be different.  
There will be cases where there are no vertices to plot (depending on user’s input) and/or new entries, yes.  
I do understand how to add the legend but failed to update it… and could not find in the documentation how to proceed.

---

<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 5, 2022, 2:44pm UTC](https://discourse.julialang.org/t/makie-update-plot-legend-through-observables/86786/4 "2022-09-05T14:44:22Z")

</div>

It’s not documented how to update the legend because that part of the public API hasn’t been fleshed out, yet. It’s possible though to update it, but it requires some manual work. The legend has a `.entrygroups` observable and updating this updates the legend. You can push groups to this vector or entries to existing groups and then `notify` the observable. However, you need to create the `LegendEntry` objects for that yourself, and that’s where the public interface is murky at best currently. You can start looking at the methods here [Makie.jl/legend.jl at master · JuliaPlots/Makie.jl · GitHub](https://github.com/JuliaPlots/Makie.jl/blob/master/src/makielayout/blocks/legend.jl#L312-L330) and try to see if you can work your way forward from that. In your case you’d probably want to create `MarkerElement`s with certain colors.

---

<div class="post-metadata">

**Author:** ![dgs](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dgs/32/7015_2.png) [@dgs](https://discourse.julialang.org/u/dgs)\
**Post date:** [September 5, 2022, 2:53pm UTC](https://discourse.julialang.org/t/makie-update-plot-legend-through-observables/86786/5 "2022-09-05T14:53:18Z")

</div>

Many thanks for your input on this. I will see if I can work my way through it.

---

<div class="post-metadata">

**Author:** ![dgs](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dgs/32/7015_2.png) [@dgs](https://discourse.julialang.org/u/dgs)\
**Post date:** [September 5, 2022, 3:43pm UTC](https://discourse.julialang.org/t/makie-update-plot-legend-through-observables/86786/6 "2022-09-05T15:43:09Z")

</div>

Actually, would you know if there is an easy way to overload the DataInspector for scatter plots in order to display not just the “x: xvalue\n:yvalue” but something like “x: xvalue\n:yvalue\nstatus:mylabel” ?

---

<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 6, 2022, 2:01pm UTC](https://discourse.julialang.org/t/makie-update-plot-legend-through-observables/86786/7 "2022-09-06T14:01:32Z")

</div>

Maybe @ffreyer knows

---

<div class="post-metadata">

**Author:** ![contradict](https://avatars.discourse-cdn.com/v4/letter/c/ac91a4/32.png) [@contradict](https://discourse.julialang.org/u/contradict)\
**Post date:** [September 6, 2022, 3:35pm UTC](https://discourse.julialang.org/t/makie-update-plot-legend-through-observables/86786/8 "2022-09-06T15:35:50Z")

</div>

I wasn’t able to find an official way to change the DataInspector tool tip, but assuming `status` can be computed from the data-space position, you can redefine the formatting method `position2string`. This will probably break in the future since it is fiddling with internal functions, but it does work now. I used this to show a date:

```julia
Makie.position2string(p::StaticVector{2}) = "$(unix2datetime(p[1]+EpochOffset))\n"*@sprintf("y: %0.6f", p[2])

```

`EpochOffset` is a constant value subtracted during plotting, very large X values over short ranges cause rendering problems.

---

<div class="post-metadata">

**Author:** ![dgs](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dgs/32/7015_2.png) [@dgs](https://discourse.julialang.org/u/dgs)\
**Post date:** [September 8, 2022, 12:04pm UTC](https://discourse.julialang.org/t/makie-update-plot-legend-through-observables/86786/9 "2022-09-08T12:04:05Z")

</div>

Great! I believe this solution would work for me.  
Thanks both for your input on this!

---

<div class="post-metadata">

**Author:** ![ericphanson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ericphanson/32/215186_2.png) [@ericphanson](https://discourse.julialang.org/u/ericphanson)\
**Post date:** [January 11, 2023, 2:51pm UTC](https://discourse.julialang.org/t/makie-update-plot-legend-through-observables/86786/10 "2023-01-11T14:51:59Z")

</div>

Just curious, is this still the case or has there been an update here? (updating plot legend via observable labels)

---

<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:** [January 11, 2023, 3:26pm UTC](https://discourse.julialang.org/t/makie-update-plot-legend-through-observables/86786/11 "2023-01-11T15:26:17Z")

</div>

No update yet, I have some issues I want to address for a refactor

---

<div class="post-metadata">

**Author:** ![csoto3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/csoto3/32/219254_2.png) [@csoto3](https://discourse.julialang.org/u/csoto3)\
**Post date:** [January 14, 2025, 11:32pm UTC](https://discourse.julialang.org/t/makie-update-plot-legend-through-observables/86786/12 "2025-01-14T23:32:13Z")

</div>

Echoing @ericphanson … any updates for manipulating legend entries via observables? I’m trying to build a scatter plot animation where each entry has a value that changes over time. I’d like to display these values in the legend and update them as the time loop steps forward.
