From 384f4d73e19a717d3c082bc24b4470fbe7e6cdfb Mon Sep 17 00:00:00 2001 From: t-bltg Date: Wed, 23 Nov 2022 15:25:31 +0100 Subject: [PATCH] add `empty!` for plots --- src/types.jl | 5 +++++ test/test_misc.jl | 16 +++++++++++++++- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/types.jl b/src/types.jl index 0726846ca..6dc6067e2 100644 --- a/src/types.jl +++ b/src/types.jl @@ -144,12 +144,17 @@ Base.size(plt::Plot) = size(plt.layout) Base.size(plt::Plot, i::Integer) = size(plt.layout)[i] Base.ndims(plt::Plot) = 2 +# clear out series list, but retain subplots +Base.empty!(plt::Plot) = foreach(sp -> empty!(sp.series_list), plt.subplots) + # attr(plt::Plot, k::Symbol) = plt.attr[k] # attr!(plt::Plot, v, k::Symbol) = (plt.attr[k] = v) Base.getindex(sp::Subplot, i::Union{Vector{<:Integer},Integer}) = series_list(sp)[i] Base.lastindex(sp::Subplot) = length(series_list(sp)) +Base.empty!(sp::Subplot) = empty!(sp.series_list) + # ----------------------------------------------------------------------- Base.show(io::IO, sp::Subplot) = print(io, "Subplot{$(sp[:subplot_index])}") diff --git a/test/test_misc.jl b/test/test_misc.jl index 4e02b6174..323ab1c1f 100644 --- a/test/test_misc.jl +++ b/test/test_misc.jl @@ -139,7 +139,7 @@ end end end -@testset "plot" begin +@testset "tex_output_standalone" begin pl = plot(1:5) pl2 = plot(pl, tex_output_standalone = true) @test !pl[:tex_output_standalone] @@ -191,6 +191,20 @@ end @test series[:y] == y2 end +@testset "Empty Plot / Subplots" begin + pl = plot(map(_ -> plot(1:2, [1:2 2:3]), 1:2)...) + empty!(pl) + @test length(pl.subplots) == 2 + @test length(first(pl).series_list) == 0 + @test length(last(pl).series_list) == 0 + + pl = plot(map(_ -> plot(1:2, [1:2 2:3]), 1:2)...) + empty!(first(pl)) # clear out only the first subplot + @test length(pl.subplots) == 2 + @test length(first(pl).series_list) == 0 + @test length(last(pl).series_list) == 2 +end + @testset "Measures" begin @test 1Plots.mm * 0.1Plots.pct == 0.1Plots.mm @test 0.1Plots.pct * 1Plots.mm == 0.1Plots.mm