From 854757e2d1127222eecbefc58dcd44d65f8b0933 Mon Sep 17 00:00:00 2001 From: Iain Skett Date: Mon, 9 Jun 2025 21:32:46 +0000 Subject: [PATCH 1/2] skip annotations for plot title subplot --- PlotsBase/src/Plots.jl | 2 ++ PlotsBase/test/test_components.jl | 40 +++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/PlotsBase/src/Plots.jl b/PlotsBase/src/Plots.jl index cf440823c..2065b4d44 100644 --- a/PlotsBase/src/Plots.jl +++ b/PlotsBase/src/Plots.jl @@ -247,6 +247,8 @@ function _update_subplot_attrs( # grab those args which apply to this subplot for k ∈ keys(_subplot_defaults) + # We don't apply annotations to the plot-title sub-plot + k != :annotations || get(plt.attr, :plot_titleindex, 0) != subplot_index || continue PlotsBase.slice_arg!(plotattributes_in, sp.attr, k, subplot_index, remove_pair) end diff --git a/PlotsBase/test/test_components.jl b/PlotsBase/test/test_components.jl index 740ea053e..5d60609db 100644 --- a/PlotsBase/test/test_components.jl +++ b/PlotsBase/test/test_components.jl @@ -187,6 +187,46 @@ end annotate!(pl, loc, string(loc)) end end + + let p = scatter([4], [4], plot_title = "x", xlims = (0, 10), ylims = (0, 10)) + for sp ∈ p.subplots + @test sp[:annotations] == [] + end + annotate!(4, 4, "4") + + for (i, sp) ∈ enumerate(p.subplots) + if i == p.attr[:plot_titleindex] + @test sp[:annotations] == [] + else + @test length(sp[:annotations]) == 1 + @test sp[:annotations][1][1] == 4 + @test sp[:annotations][1][2] == 4 + @test sp[:annotations][1][3].str == "4" + end + end + end + + let p = plot( + scatter([4], [4], xlims = (0, 10), ylims = (0, 10)), + scatter([4], [4], xlims = (0, 10), ylims = (0, 10)), + plot_title = "x", + ) + for sp ∈ p.subplots + @test sp[:annotations] == [] + end + annotate!(4, 4, "4") + + for (i, sp) ∈ enumerate(p.subplots) + if i == p.attr[:plot_titleindex] + @test sp[:annotations] == [] + else + @test length(sp[:annotations]) == 1 + @test sp[:annotations][1][1] == 4 + @test sp[:annotations][1][2] == 4 + @test sp[:annotations][1][3].str == "4" + end + end + end end @testset "Fonts" begin From 388cf17c984daf61fff850fe0ac3b342fcf9aa9c Mon Sep 17 00:00:00 2001 From: Iain Skett Date: Tue, 10 Jun 2025 08:29:01 +0000 Subject: [PATCH 2/2] switch predicate to an if block and add additional test to verify consistent behaviour of annotations --- PlotsBase/src/Plots.jl | 4 +++- PlotsBase/test/test_components.jl | 20 ++++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/PlotsBase/src/Plots.jl b/PlotsBase/src/Plots.jl index 2065b4d44..0441616e3 100644 --- a/PlotsBase/src/Plots.jl +++ b/PlotsBase/src/Plots.jl @@ -248,7 +248,9 @@ function _update_subplot_attrs( # grab those args which apply to this subplot for k ∈ keys(_subplot_defaults) # We don't apply annotations to the plot-title sub-plot - k != :annotations || get(plt.attr, :plot_titleindex, 0) != subplot_index || continue + if k == :annotations && get(plt.attr, :plot_titleindex, 0) == subplot_index + continue + end PlotsBase.slice_arg!(plotattributes_in, sp.attr, k, subplot_index, remove_pair) end diff --git a/PlotsBase/test/test_components.jl b/PlotsBase/test/test_components.jl index 5d60609db..02d26d57f 100644 --- a/PlotsBase/test/test_components.jl +++ b/PlotsBase/test/test_components.jl @@ -206,6 +206,26 @@ end end end + let p = scatter( + [4], + [4], + plot_title = "x", + xlims = (0, 10), + ylims = (0, 10), + annotations = (4, 4, "4"), + ) + for (i, sp) ∈ enumerate(p.subplots) + if i == p.attr[:plot_titleindex] + @test sp[:annotations] == [] + else + @test length(sp[:annotations]) == 1 + @test sp[:annotations][1][1] == 4 + @test sp[:annotations][1][2] == 4 + @test sp[:annotations][1][3].str == "4" + end + end + end + let p = plot( scatter([4], [4], xlims = (0, 10), ylims = (0, 10)), scatter([4], [4], xlims = (0, 10), ylims = (0, 10)),