# Makie inplace plot recipe

**URL:** https://discourse.julialang.org/t/makie-inplace-plot-recipe/54857
**Category:** Visualization
**Tags:** plotting, makie, recipe
**Created:** [February 8, 2021, 6:00pm UTC](https://discourse.julialang.org/t/makie-inplace-plot-recipe/54857 "2021-02-08T18:00:06Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![zekeriya.sari](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zekeriya.sari/32/13695_2.png) [@zekeriya.sari](https://discourse.julialang.org/u/zekeriya.sari)
#### Post date: [February 8, 2021, 6:00pm UTC](https://discourse.julialang.org/t/makie-inplace-plot-recipe/54857/1 "2021-02-08T18:00:06Z")

</div>

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

```julia
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,

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

```

I get the following error

```julia
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?

---

<div class="post-metadata">

### Author: ![zekeriya.sari](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zekeriya.sari/32/13695_2.png) [@zekeriya.sari](https://discourse.julialang.org/u/zekeriya.sari)
#### Post date: [February 8, 2021, 6:08pm UTC](https://discourse.julialang.org/t/makie-inplace-plot-recipe/54857/2 "2021-02-08T18:08:30Z")

</div>

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

```julia
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)

```

---

<div class="post-metadata">

### Author: ![zekeriya.sari](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zekeriya.sari/32/13695_2.png) [@zekeriya.sari](https://discourse.julialang.org/u/zekeriya.sari)
#### Post date: [February 8, 2021, 6:34pm UTC](https://discourse.julialang.org/t/makie-inplace-plot-recipe/54857/3 "2021-02-08T18:34:14Z")

</div>

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,

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

```

---

<div class="post-metadata">

### Author: ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)
#### Post date: [February 9, 2021, 8:24am UTC](https://discourse.julialang.org/t/makie-inplace-plot-recipe/54857/4 "2021-02-09T08:24:09Z")

</div>

The behavior of the different plotting function signatures is explained here [http://makie.juliaplots.org/stable/plot\_method\_signatures.html](http://makie.juliaplots.org/stable/plot_method_signatures.html)

---

<div class="post-metadata">

### Author: ![zekeriya.sari](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zekeriya.sari/32/13695_2.png) [@zekeriya.sari](https://discourse.julialang.org/u/zekeriya.sari)
#### Post date: [February 9, 2021, 8:25am UTC](https://discourse.julialang.org/t/makie-inplace-plot-recipe/54857/5 "2021-02-09T08:25:56Z")

</div>

@jules Yes, figured that out late 😀 Thank you.

---

<div class="post-metadata">

### Author: ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)
#### Post date: [February 9, 2021, 8:32am UTC](https://discourse.julialang.org/t/makie-inplace-plot-recipe/54857/6 "2021-02-09T08:32:41Z")

</div>

Great 🙂 I thought maybe you had pieced everything together yourself 😉

---

<div class="post-metadata">

### Author: ![zekeriya.sari](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zekeriya.sari/32/13695_2.png) [@zekeriya.sari](https://discourse.julialang.org/u/zekeriya.sari)
#### Post date: [February 9, 2021, 8:34am UTC](https://discourse.julialang.org/t/makie-inplace-plot-recipe/54857/7 "2021-02-09T08:34:29Z")

</div>

After a couple of trial and error. 😃
