# Combine a "plot" object with a "scatter" object in an animation

**URL:** <https://discourse.julialang.org/t/combine-a-plot-object-with-a-scatter-object-in-an-animation/42140>\
**Category:** General Usage\
**Tags:** plotting, gaston\
**Created:** [June 26, 2020, 11:36pm UTC](https://discourse.julialang.org/t/combine-a-plot-object-with-a-scatter-object-in-an-animation/42140 "2020-06-26T23:36:49Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gus\_Hart](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gus_hart/32/6987_2.png) [@Gus\_Hart](https://discourse.julialang.org/u/Gus_Hart)\
**Post date:** [June 26, 2020, 11:36pm UTC](https://discourse.julialang.org/t/combine-a-plot-object-with-a-scatter-object-in-an-animation/42140/1 "2020-06-26T23:36:49Z")

</div>

I’m creating an animation by combining multiple `scatter` plots (using `Animation()` and `frame()`). It all works fine. But I would like my scatter plots to be overlaid on a `plot` that shows the trajectories I expect the particles to follow. That plot could be done before the loop that creates the frames.

But I don’t know how to overlay a `scatter` on top of a `plot`. I don’t think `scatter!` is the way to go because each frame needs to be a new `scatter` plot. That is, I don’t want the particle positions to accumulate, but to be updated in each frame.

Here is a MWE. The particle will follow a parabolic trajectory. I want to plot that parabola underneath the particle.

```julia
using Plots
x = [-1. -1. -1.] #Starting positions
δt = 0.01
anim = Animation()
tEnd = π; t = 0;
vb = [-1,+1];

while t < tEnd
    x[3] = 2x[2] - x[1] -x[2]*δt^2 #Verlet step
    # Make plot for anim fram
    scatter([x[2]] ,[V(x[2])],xlim=vb,ylim=vb,legend=:none
      ,markersize=3,frame=:box,dpi=150,aspectratio=1,markerstrokecolor="blue",
      markercolor="blue")
    # update position vector
    x[2],x[1] = x[3],x[2]
    t += δt # advance simulation time
    frame(anim)
end

```

---

<div class="post-metadata">

**Author:** ![mbaz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbaz/32/17295_2.png) [@mbaz](https://discourse.julialang.org/u/mbaz)\
**Post date:** [June 27, 2020, 2:01am UTC](https://discourse.julialang.org/t/combine-a-plot-object-with-a-scatter-object-in-an-animation/42140/2 "2020-06-27T02:01:32Z")

</div>

I don’t know how to do that using `Plots`, but here’s an example using Gaston and gnuplot. I couldn’t reproduce your code exactly because I don’t know what `V(x)` is, but my example should be easy to adapt.

```julia
using Gaston
x = -5:0.1:5;
y = sin.(π*x);
F = plot(x, y, lc = :black); # we need one first plot
for (idx, xx) in enumerate(x)
    # plot "trajectory"
    P = plot(x, y, lc = :black, handle = 2);
    # plot "particle"
    plot!([xx], [y[idx]], w = :points,
          marker = "fcircle", markersize = 3,
          lc = :blue, handle = 2);
    push!(F, P) # add new frame to first plot
end
# save as gif
save(term = "gif",
     saveopts = "animate size 600,400 delay 1",
     output = "anim.gif", handle = 1)

```

![anim](https://global.discourse-cdn.com/julialang/original/3X/1/7/17401ee27f049d4c14322adf284fbeb876655464.gif)

---

<div class="post-metadata">

**Author:** ![mtsch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mtsch/32/2072_2.png) [@mtsch](https://discourse.julialang.org/u/mtsch)\
**Post date:** [June 27, 2020, 3:45am UTC](https://discourse.julialang.org/t/combine-a-plot-object-with-a-scatter-object-in-an-animation/42140/3 "2020-06-27T03:45:38Z")

</div>

I can’t test this right now, but try `deepcopy`ing the trajectory plot for each frame and then use `scatter!` on the copy.

---

<div class="post-metadata">

**Author:** ![Gus\_Hart](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gus_hart/32/6987_2.png) [@Gus\_Hart](https://discourse.julialang.org/u/Gus_Hart)\
**Post date:** [June 27, 2020, 2:22pm UTC](https://discourse.julialang.org/t/combine-a-plot-object-with-a-scatter-object-in-an-animation/42140/4 "2020-06-27T14:22:26Z")

</div>

The really simple solution is just to plot the trajectory fresh in each loop. Thanks @mbaz.

```julia
using Plots
function V(x)
    return x^2
end    
tx = -1:0.02:+1
x = [-1. -1. -1.] #Starting positions
δt = 0.05
anim = Animation()
tEnd = 2π; t = 0; 
vb = [-1,+1];

 while t < tEnd
   x[3] = 2x[2] - x[1] -x[2]*δt^2
   plot(tx,V.(tx),legend=:none)
   scatter!([x[2]] ,[V(x[2])],xlim=vb,ylim=vb,legend=:none,ticks=:false
       ,markersize=3,frame=:box,dpi=150,aspectratio=1,markerstrokecolor="blue",
       markercolor="blue")
   x[2],x[1] = x[3],x[2]
   t += δt
   frame(anim)
end
gif(anim)

```
