在 Julia 中擦除以前的数据/图(Plots.jl,GR 后端)

Sat*_*ato 4 plot animation gif ode julia

我已经在 J​​ulia 中解决了描述粒子运动的 ODE,并将坐标和各自的时间保存在一个数组中。我想用粒子沿着求解的轨迹创建一个绘图的动画 gif 图像,但要做到这一点(我提出的唯一方法)是使用 绘制粒子的位置scatter,并擦除 的先前位置每时每刻的粒子。但是我只知道scatter!哪个会向绘图中添加更多粒子,而不是显示粒子位置的变化。那么我怎样才能在每次迭代时擦除以前的图,或者有更聪明的方法来做到这一点?如果我想使用绘图标记更早时刻粒子的轨迹怎么办?

小智 5

使用 Plots.jl 无法擦除以前的数据。可以使用plotscatter命令代替plot!and来擦除之前的绘图scatter!。以下是一些如何使用@gif宏创建动画的示例( http://docs.juliaplots.org/latest/animations/ )

创建一些虚拟数据:

using Plots

t = range(0, 4?, length = 100)
r = range(1, 0, length = 100)

x = cos.(t) .* r
y = sin.(t) .* r
Run Code Online (Sandbox Code Playgroud)

在每一步中只绘制最后一个当前点:

@gif for i in eachindex(x)
    scatter((x[i], y[i]), lims = (-1, 1), label = "")
end
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

在当前位置用标记绘制所有先前的步骤:

@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
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

与上面相同,降低旧步骤的 alpha(仅显示最新的 10 个步骤):

@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
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明