I have solved an ODE in Julia describing the motion of a particle, and I have saved the coordinates and respective time in an array. I would like to create an animated gif image of a plot with the particle along the solved trajectory, but to do that (the only way that I have come up) is to plot the position of the particle using scatter
, and erase the previous position of the particle every moment. However I only know about scatter!
which will add more particles to the plot rather than showing the change of particle position. So how can I erase the previous plot every iteration, or are there more clever ways to do this? And what about if I want to mark down the trajectory of the particle in earlier moments using plots?
Asked
Active
Viewed 1,286 times
4

Sato
- 1,013
- 1
- 12
- 27
-
Do you need to use Plots.jl? Makie.jl is definitely better for this, since it will allow you to change the position of the scatter marker. There is a (perhaps overcomplicated) example here: https://simondanisch.github.io/ReferenceImages/gallery/differentialequations_path_animation/index.html – Anshul Singhvi Sep 25 '19 at 17:30
-
I had a similar question here: https://stackoverflow.com/questions/57829961/julia-plotting-delete-and-modify-existing-lines , switched to Makie as recommended and removing objects works just fine. – mattu Sep 26 '19 at 02:36
1 Answers
6
Erasing previous data is not possible with Plots.jl. The previous plot can be erased by using plot
or scatter
commands instead of plot!
and scatter!
. Here are some examples how animations can be created using the @gif
macro (http://docs.juliaplots.org/latest/animations/)
Create some dummy data:
using Plots
t = range(0, 4π, length = 100)
r = range(1, 0, length = 100)
x = cos.(t) .* r
y = sin.(t) .* r
Plot only the last current point in each step:
@gif for i in eachindex(x)
scatter((x[i], y[i]), lims = (-1, 1), label = "")
end
Plot all previous steps with a marker at the current position:
@gif for i in eachindex(x)
plot(x[1:i], y[1:i], lims = (-1, 1), label = "")
scatter!((x[i], y[i]), color = 1, label = "")
end
The same as above with decreasing alpha for older steps (only showing the newest 10 steps):
@gif for i in eachindex(x)
plot(x[1:i], y[1:i], alpha = max.((1:i) .+ 10 .- i, 0) / 10, lims = (-1, 1), label = "")
scatter!((x[i], y[i]), color = 1, label = "")
end

Daniel Schwabeneder
- 206
- 1
- 2