Skip to content

Comments

Add possibility to collect legend entries from multiple axes#5500

Open
jusack wants to merge 5 commits intoMakieOrg:masterfrom
jusack:JA/legend-multiple-axes
Open

Add possibility to collect legend entries from multiple axes#5500
jusack wants to merge 5 commits intoMakieOrg:masterfrom
jusack:JA/legend-multiple-axes

Conversation

@jusack
Copy link

@jusack jusack commented Jan 19, 2026

Description

I added a convenient way to gather legend entries from multiple axes, i.e.

using GLMakie

fig = Figure()
ax1 = Axis(fig[1,1])
ax2 = Axis(fig[1,2])
lines!(ax1, randn(10), label="Trace 1")
lines!(ax1, randn(10), label="Trace 2")
lines!(ax1, randn(10), label="Trace 3")
lines!(ax2, randn(10), label="Trace 1")
lines!(ax2, randn(10), label="Trace 2")
Legend(fig[1,3], [ax1, ax2], merge=true)

will produce a joint legend for both axes.
img1

The main reason why this is relevant for me is that the interactivity is then linked as well:
img2

Type of change

  • New feature (non-breaking change which adds functionality)

Checklist

  • Added an entry in CHANGELOG.md (for new features and breaking changes)
  • Added or changed relevant sections in the documentation
  • Added unit tests for new algorithms, conversion methods, etc.
  • Added reference image tests for new plotting functions, recipes, visual options, etc.

@github-project-automation github-project-automation bot moved this to Work in progress in PR review Jan 19, 2026
Copy link
Member

@jkrumbiegel jkrumbiegel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One of the tests is really enough, given the super simple mechanism not much is gained by piping many different plot types through. Rather, it could be tested once that the legend actually has the two expected entries.

@jusack
Copy link
Author

jusack commented Jan 23, 2026

Thank you for the comment and looking at the PR. I have adjusted the test. However I am not sure if I the indexing into all the layers is the best way to get to the legend content. Is there an easier way to achieve what I was trying?

@jusack jusack requested a review from jkrumbiegel January 23, 2026 14:48
If `unique` is `true`, all plot objects with the same plot type and label will be reduced to one occurrence.
"""
function Legend(fig_or_scene, axis::Union{Axis, Axis3, Scene, LScene}, title = nothing; merge = false, unique = false, kwargs...)
function Legend(fig_or_scene, axis::Union{Axis, Axis3, Scene, LScene, AbstractVector{<:Union{Axis, Axis3, Scene, LScene}}}, title = nothing; merge = false, unique = false, kwargs...)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Docstring should be updated here

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't really related to the pr, but shouldn't the types here be Union{AbstractAxis, Scene} so this can work with other axis types more easily? Like GeoMakie.GeoAxis or PolarAxis? (And maybe we should also have get_plots(ax::AbstractAxis) = ax.scene?)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds sensible

If `merge` is `true`, all plot objects with the same label will be layered on top of each other into one legend entry.
If `unique` is `true`, all plot objects with the same plot type and label will be reduced to one occurrence.

To create a joint legend for multiple axes it is also possible to pass a `Vector` of axis objects.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be good to add a simple visual example here, maybe one that uses merge too

@jkrumbiegel
Copy link
Member

two more comments got stuck in the pipeline, I've submitted them again now. The indexing in the test is fine, using internals internally is ok :)

@StefanPofahl
Copy link

StefanPofahl commented Jan 30, 2026

I would like share my view:
My opinion about the functionality is: It is very important to add such a feature to the function Legend()!
Thanks a lot Julius for your work on this!
My proposal is:
What about an automatism: if there is a vector of axis handles join the legends of the axes?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Work in progress

Development

Successfully merging this pull request may close these issues.

4 participants