Makie.jl Series Plot Color Cycling with Observable Legends

My goal is to plot a series of data and have their legends be observable so I can change them later. This works already using series! using the labels keyword argument set to a vector of observables. However, when I add another series plot on top of an existing one, the colors seem to repeat themselves (they do not cycle).

Consider the following example which illustrates this:

using GLMakie

fig = Figure()
ax = Axis(fig[1, 1])

# Labels I want to change
lab1 = Observable("label 1")
lab2 = Observable("label 2")
lab3 = Observable("label 3")
lab4 = Observable("label 4")

# Arbitrary data to plot
X = [1, 2, 3]
Y1 = [1 2 3; 4 5 6] # First set of Y data
Y2 = [2 5 7; 3 1 9] # Second set of Y data

# Plot Y1 using series plot
series!(ax, X, Y1, labels=[lab1, lab2])

# Plot Y2 on top of current axis
series!(ax, X, Y2, labels=[lab4, lab4])

Here I can successfully change lab1, lab2, lab3 and lab4 to different strings and the legend updates appropriately. The problem is the colors of the lines repeat themselves.

Below is an alternative approach which uses the lines! function instead of series! to get proper color cycling:

# Plot Y1 using line plot
foreach((y, lab) -> lines!(ax, X, y, label=lab), eachrow(Y1), [lab1, lab2])

# Plot Y2 on top of current axis
foreach((y, lab) -> lines!(ax, X, y, label=lab), eachrow(Y2), [lab3, lab4])

Here the colors work better, however, I can no longer change the labels.

Does anyone have an idea what the best approach would be for me to use? I did read this discussion:

But I was wondering if there was a more elegant solutions than this? Any help is appreciated, thanks.

1 Like