Makie : Update plot legend through Observables


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!

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

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.

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 and try to see if you can work your way forward from that. In your case you’d probably want to create MarkerElements with certain colors.

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

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” ?

Maybe @ffreyer knows

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:

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.

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

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

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

1 Like