How to combine the legend of multiple axes on same figure into one in Makie.jl?

I have a few axes on the same figure such that each of them share some common curves. Is there a way I can add a single legend for all of them on the figure such that the curve (following the same label and style) get merged as well?

Yes, have a look in the Makie source code where axislegend is defined, there is some logic to collect all the legend elements for one Axis. You’d have to do the step manually where you collect the plot objects from all axes into one vector (they are in ax.scene.plots) and then let the existing code do the merging / deduplication.

@jules Thanks a lot! It worked with the code you mentioned. For anyone else wondering this is how:

plots_in_fig = AbstractPlot[]
labels_in_fig = AbstractString[]
for ax in axes
    pl, lb = Makie.get_labeled_plots(ax, merge=false, unique=false)
    append!(plots_in_fig, pl)
    append!(labels_in_fig, lb)
end

ulabels = Base.unique(labels_in_fig)
mergedplots = [[lp for (i, lp) in enumerate(plots_in_fig) if labels_in_fig[i] == ul]
        for ul in ulabels]

Legend(fig[:, 2], mergedplots, ulabels)
1 Like