Makie inplace plot recipe

Is it possible to define an in-place plotting recipe using Makie? For example, the following recipe works properly.

using Makie 

@recipe(MyPlot, x, y) do scene 
    Attributes(
        plot_color = :red,
    )
end 

function AbstractPlotting.plot!(myplot::MyPlot) 
    data = rand(10) 
    lines!(myplot, data, color=myplot.plot_color)
    scatter!(myplot, data, color=myplot.plot_color) 
    myplot
end

fig = Figure() 
myplot(fig[1, 1], rand(10), rand(10), plot_color=:black)

The problem is that when I execute the last statement again, like,

myplot(fig[1, 1], rand(10), rand(10), plot_color=:green)

I get the following error

ERROR: AssertionError: isempty(contents(fp.gp, exact = true))
Stacktrace:
 [1] plot(::Type{Combined{myplot, ArgType} where ArgType}, ::AbstractPlotting.FigurePosition, ::Vector{Float64}, ::Vararg{Vector{Float64}, N} where N; axis::NamedTuple{(), Tuple{}}, kwargs::Base.Iterators.Pairs{Symbol, Symbol, Tuple{Symbol}, NamedTuple{(:plot_color,), Tuple{Symbol}}})
   @ AbstractPlotting ~/.julia/packages/AbstractPlotting/50gu0/src/figureplotting.jl:44
 [2] myplot(::AbstractPlotting.FigurePosition, ::Vararg{Any, N} where N; attributes::Base.Iterators.Pairs{Symbol, Symbol, Tuple{Symbol}, NamedTuple{(:plot_color,), Tuple{Symbol}}})
   @ Main ~/.julia/packages/AbstractPlotting/50gu0/src/recipes.jl:12
 [3] top-level scope
   @ REPL[43]:1
 [4] eval
   @ ./boot.jl:360 [inlined]

Is there a possible solution for this?

Ok. If the first argument is Axis instead of Figure, multiple executions work without errors.

fig = Figure() 
ax = fig[1, 1] = Axis(fig)
myplot(ax, rand(10), rand(10), plot_color=:black)
myplot(ax, rand(10), rand(10), plot_color=:green)

Once an axis is constructed and inserted into fig, the first ax argument to myplot can be dropped. As far as I can see, once an axis is inserted into fig, fig.current_axis is not nothing anymore. So the following is also possible,

fig = Figure() 
ax = fig[1, 1] = Axis(fig)
myplot(rand(10), rand(10), plot_color=:black)
myplot(rand(10), rand(10), plot_color=:green)

The behavior of the different plotting function signatures is explained here http://makie.juliaplots.org/stable/plot_method_signatures.html

@jules Yes, figured that out late :grinning: Thank you.

Great :slight_smile: I thought maybe you had pieced everything together yourself :wink:

After a couple of trial and error. :smiley: