# How can I add to a plot without mutating it in Plots.jl?

**URL:** <https://discourse.julialang.org/t/how-can-i-add-to-a-plot-without-mutating-it-in-plots-jl/63911>\
**Category:** New to Julia\
**Tags:** plotting, plots\
**Created:** [July 1, 2021, 7:29pm UTC](https://discourse.julialang.org/t/how-can-i-add-to-a-plot-without-mutating-it-in-plots-jl/63911 "2021-07-01T19:29:52Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Albert\_Zevelev](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/albert_zevelev/32/11844_2.png) [@Albert\_Zevelev](https://discourse.julialang.org/u/Albert_Zevelev)\
**Post date:** [July 1, 2021, 7:29pm UTC](https://discourse.julialang.org/t/how-can-i-add-to-a-plot-without-mutating-it-in-plots-jl/63911/1 "2021-07-01T19:29:52Z")

</div>

I would like to add to an existing plot `p0` without mutating it.  
Currently I have to repeat code

```julia
using Plots;
xg = 0.01:0.01:10.0;

p0 = plot(xg, sin, legend=:topleft)
p0 = plot!(p0, xg, cos)
p1 = plot!(p0, xg, log)

p0 = plot(xg, sin, legend=:topleft)
p0 = plot!(p0, xg, cos)
p2 = plot!(p0, xg, sqrt)

```

I want:

```julia
using Plots;
xg = 0.01:0.01:10.0;

p0 = plot(xg, sin, legend=:topleft)
plot!(p0, xg, cos)

p1 = plot(p0, xg, log)
p2 = plot(p0, xg, sqrt)

```

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [July 1, 2021, 7:45pm UTC](https://discourse.julialang.org/t/how-can-i-add-to-a-plot-without-mutating-it-in-plots-jl/63911/2 "2021-07-01T19:45:44Z")

</div>

Just add more `plot` commands

```julia
       using Plots;
       xg = 0.01:0.01:10.0;
       
       p0 = plot(xg, sin, legend=:topleft)
       plot!(p0, xg, cos)
       
       p1 = plot(p0, plot(xg, log))
       p2 = plot(p0, plot(xg, sqrt))

```

---

<div class="post-metadata">

**Author:** ![Albert\_Zevelev](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/albert_zevelev/32/11844_2.png) [@Albert\_Zevelev](https://discourse.julialang.org/u/Albert_Zevelev)\
**Post date:** [July 1, 2021, 7:53pm UTC](https://discourse.julialang.org/t/how-can-i-add-to-a-plot-without-mutating-it-in-plots-jl/63911/3 "2021-07-01T19:53:08Z")

</div>

@pdeffebach that creates a subplot.  
I want to add a curve to `p0` without mutating it.

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [July 1, 2021, 7:57pm UTC](https://discourse.julialang.org/t/how-can-i-add-to-a-plot-without-mutating-it-in-plots-jl/63911/4 "2021-07-01T19:57:58Z")

</div>

Oh sorry, I see.

Only thing I could think of was `deepcopy`. Maybe that’s wrong in some way, though.

```julia
julia> begin 
       using Plots;
       xg = 0.01:0.01:10.0;
       
       p0 = plot(xg, sin, legend=:topleft)
       plot!(p0, xg, cos)
       
       p1 = plot!(deepcopy(p0), xg, log)
       end

```

---

<div class="post-metadata">

**Author:** ![Albert\_Zevelev](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/albert_zevelev/32/11844_2.png) [@Albert\_Zevelev](https://discourse.julialang.org/u/Albert_Zevelev)\
**Post date:** [August 28, 2021, 4:52am UTC](https://discourse.julialang.org/t/how-can-i-add-to-a-plot-without-mutating-it-in-plots-jl/63911/5 "2021-08-28T04:52:51Z")

</div>

Issue solved by PR:  
[https://github.com/JuliaPlots/Plots.jl/issues/3593](https://github.com/JuliaPlots/Plots.jl/issues/3593)

---

<div class="post-metadata">

**Author:** ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)\
**Post date:** [August 28, 2021, 10:45am UTC](https://discourse.julialang.org/t/how-can-i-add-to-a-plot-without-mutating-it-in-plots-jl/63911/6 "2021-08-28T10:45:34Z")

</div>

Can I ask why it is nice to do it this way compared with just mutating it?

Out of curiosity.

Kind regards

---

<div class="post-metadata">

**Author:** ![Wen-Wei\_Tseng](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wen-wei_tseng/32/28839_2.png) [@Wen-Wei\_Tseng](https://discourse.julialang.org/u/Wen-Wei_Tseng)\
**Post date:** [August 28, 2021, 11:14am UTC](https://discourse.julialang.org/t/how-can-i-add-to-a-plot-without-mutating-it-in-plots-jl/63911/7 "2021-08-28T11:14:36Z")

</div>

The one use case immediately came to my mind is creating a bunch of plots with the same “background”. (As in the OP) One could wrap the background plot generation into a function but recalculation might be more expensive than just copying the plot and using it later.

Edit: typo
