diff --git a/docs/src/makielayout/axis.md b/docs/src/makielayout/axis.md index 7149b840c..9254f3ebb 100755 --- a/docs/src/makielayout/axis.md +++ b/docs/src/makielayout/axis.md @@ -36,6 +36,30 @@ scatobject = scatter!(0:0.5:10, cos, color = :orange) f ``` +## Deleting plots + +You can delete a plot object directly via `delete!(ax, plotobj)`. +You can also remove all plots with `empty!(ax)`. + +```@example +using CairoMakie +CairoMakie.activate!() # hide +AbstractPlotting.inline!(true) # hide + +f = Figure(resolution = (1200, 500)) + +axs = [Axis(f[1, i]) for i in 1:3] + +scatters = map(axs) do ax + [scatter!(ax, 0:0.1:10, x -> sin(x) + i) for i in 1:3] +end + +delete!(axs[2], scatters[2][2]) +empty!(axs[3]) + +f +``` + ## Setting Axis limits and reversing axes @@ -596,4 +620,5 @@ scene ```@eval using GLMakie GLMakie.activate!() -``` \ No newline at end of file +``` + diff --git a/src/makielayout/layoutables/axis.jl b/src/makielayout/layoutables/axis.jl index f3922cbce..1ecff304e 100644 --- a/src/makielayout/layoutables/axis.jl +++ b/src/makielayout/layoutables/axis.jl @@ -989,4 +989,16 @@ end function limits!(args...) limits!(current_axis(), args...) -end \ No newline at end of file +end + +function Base.delete!(ax::Axis, plot::AbstractPlot) + delete!(ax.scene, plot) + ax +end + +function Base.empty!(ax::Axis) + for plot in copy(ax.scene.plots) + delete!(ax, plot) + end + ax +end diff --git a/src/makielayout/layoutables/scene.jl b/src/makielayout/layoutables/scene.jl index b88d77057..4ee31d50e 100644 --- a/src/makielayout/layoutables/scene.jl +++ b/src/makielayout/layoutables/scene.jl @@ -43,3 +43,8 @@ function layoutable(::Type{LScene}, fig_or_scene; bbox = nothing, scenekw = Name ls end + +function Base.delete!(ax::LScene, plot::AbstractPlot) + delete!(ax.scene, plot) + ax +end diff --git a/test/unit_tests/makielayout.jl b/test/unit_tests/makielayout.jl index 3f6af84c8..1abe53d13 100644 --- a/test/unit_tests/makielayout.jl +++ b/test/unit_tests/makielayout.jl @@ -16,4 +16,18 @@ tb = layout[end + 1, :] = Textbox(scene) is = layout[end + 1, :] = IntervalSlider(scene) @test true +end + +@testset "deleting from axis" begin + f = Figure() + ax = Axis(f[1, 1]) + sc = scatter!(ax, randn(100, 2)) + li = lines!(ax, randn(100, 2)) + hm = heatmap!(ax, randn(20, 20)) + @test length(ax.scene.plots) == 3 + delete!(ax, sc) + @test length(ax.scene.plots) == 2 + @test sc ∉ ax.scene.plots + empty!(ax) + @test isempty(ax.scene.plots) end \ No newline at end of file