GLMakie: Legend for figures with two y-axis

Hi!
I spent some time in the net but a failed to find a suitable solution.
In the forum I found the helpful topic:
https://discourse.julialang.org/t/solved-plotting-2-plots-on-same-figure-with-dual-axis/124623

But the question is still not solved:
How to specify a legend if a figure contains two y-axis.
Example:
Given are: Xvec, VectorDistValues, VectorCurrent, VectorPrivious

    fig = Figure()
    ax1 = Axis(fig[1, 1], yticklabelcolor = :blue, xlabel = "n Iterations / -", ylabel = "Cycle Counter / -")
    ax2 = Axis(fig[1, 1], yticklabelcolor = :red, yaxisposition = :right, ylabel = "Distance / -")
    
    hln1 = lines!(ax1, Xvec, VectorDistValues, label= "Distance / -")
    hln2 = lines!(ax2, Xvec, VectorCurrent,  color = :red, label= "Current")
    hln3 = lines!(ax2, Xvec, VectorPrivious, color = :orange, label= "Privious")  

I know that the following works fine:

    Legend(fig[1, 2],
       [hln1, hln2, hln3],
       ["Distance / -", "Current", "Privious"],)

But I wonder if I can use the line-labels already specified in the line-definitions.
Something similar like the following (which works only for figures whith one y-axis):

    Legend(fig[1, 2], ax1)

Any ideas?

You want Add possibility to collect legend entries from multiple axes by jusack · Pull Request #5500 · MakieOrg/Makie.jl · GitHub

2 Likes

Cool! :slight_smile:
Thanks! - Is it already merged into main?

No, green means it’s still open

@jules
Proposal: What about an automatism: if there is a vector of axis handles join the legends of the axes?

Better to comment on the PR if you have specific suggestions about the implementation

@jules
I was informed that something have been accepted and that code have been merged.
Thanks for your initiative!
What are now the next steps? Currently I do not yet see a change in the functionality of the Legend() function.

The PR is merged into the main branch of the Makie.jl repo (see the changelog), but is not released yet. It looks like the changes from the PR #5551 will be released in Makie 0.24.10. In the meantime I think you can use the unreleased changes by doing

using Pkg
Pkg.add(url="https://github.com/MakieOrg/Makie.jl", rev="master")
1 Like

Better is:
]add ComputePipeline#master Makie#master GLMakie#master
Since otherwise you may get some weird compat issues. ComputePipeline/Makie and the backends are developped in lockstep, and just adding Makie#master basically disables that :wink:

2 Likes