Skip to content

Commit 379bb69

Browse files
Fix legend for plotlist with multiple plots (#4546)
* Fix `legend` for plotlist with multiple plots * Add a test * Update CHANGELOG.md --------- Co-authored-by: SimonDanisch <sdanisch@protonmail.com>
1 parent 70e5d2c commit 379bb69

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## [Unreleased]
44

55
- Added `subsup` and `left_subsup` functions that offer stacked sub- and superscripts for `rich` text which means this style can be used with arbitrary fonts and is not limited to fonts supported by MathTeXEngine.jl [#4489](https://github.com/MakieOrg/Makie.jl/pull/4489).
6+
- Expand PlotList plots to expose their child plots to the legend interface, allowing `axislegend`show plots within PlotSpecs as individual entries. [#4546](https://github.com/MakieOrg/Makie.jl/pull/4546)
67
- Implement S.Colorbar(plotspec) [#4520](https://github.com/MakieOrg/Makie.jl/pull/4520).
78

89
## [0.21.15] - 2024-10-25

src/makielayout/blocks/legend.jl

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -661,7 +661,8 @@ end
661661

662662
function get_labeled_plots(ax; merge::Bool, unique::Bool)
663663
lplots = filter(get_plots(ax)) do plot
664-
haskey(plot.attributes, :label)
664+
haskey(plot.attributes, :label) ||
665+
plot isa PlotList && any(x -> haskey(x.attributes, :label), plot.plots)
665666
end
666667
labels = map(lplots) do l
667668
l.label[]
@@ -713,6 +714,11 @@ function get_labeled_plots(ax; merge::Bool, unique::Bool)
713714
end
714715

715716
get_plots(p::AbstractPlot) = [p]
717+
# NOTE: this is important, since we know that `get_plots` is only ever called on the toplevel,
718+
# we can assume that any plotlist on the toplevel should be decomposed into individual plots.
719+
# However, if the user passes a label argument with a legend override, what do we do?
720+
get_plots(p::PlotList) = haskey(p.attributes, :label) && p.attributes[:label] isa Pair ? [p] : p.plots
721+
716722
get_plots(ax::Union{Axis, Axis3}) = get_plots(ax.scene)
717723
get_plots(lscene::LScene) = get_plots(lscene.scene)
718724
function get_plots(scene::Scene)

test/specapi.jl

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,3 +170,20 @@ end
170170
@test isempty(f.content)
171171
@test isempty(f.layout.content)
172172
end
173+
174+
@testset "Legend construction" begin
175+
f, ax, pl = plotlist([S.Scatter(1:4, 1:4; marker = :circle, label="A"), S.Scatter(1:6, 1:6; marker = :rect, label="B")])
176+
leg = axislegend(ax)
177+
# Test that the legend has two scatter plots
178+
@test count(x -> x isa Makie.Scatter, leg.scene.plots) == 2
179+
180+
# Test that the scatter plots have the correct markers
181+
# This is too internal and fragile, so we won't actually test this
182+
# @test leg.scene.plots[2].marker[] == :circle
183+
# @test leg.scene.plots[3].marker[] == :rect
184+
185+
# Test that the legend has the correct labels.
186+
# Again, I consider this too fragile to work with!
187+
# @test contents(contents(leg.grid)[1])[2].text[] == "A"
188+
# @test contents(contents(leg.grid)[2])[4].text[] == "B"
189+
end

0 commit comments

Comments
 (0)