在 Julia 中擦除之前的 data/plots(Plots.jl,GR 后端)
Erasing previous data/plots in Julia (Plots.jl, GR backend)
我在 Julia 中求解了描述粒子运动的 ODE,并将坐标和各自的时间保存在数组中。我想创建一个动画 gif 图像,其中粒子沿着已求解的轨迹绘制,但要做到这一点(我想出的唯一方法)是使用 scatter
绘制粒子的位置,并且每时每刻擦除粒子的先前位置。但是我只知道 scatter!
会在图中添加更多粒子而不是显示粒子位置的变化。那么我怎样才能每次迭代都删除以前的情节,或者有更聪明的方法来做到这一点?如果我想使用绘图标记早期粒子的轨迹怎么办?
无法使用 Plots.jl 删除以前的数据。可以使用 plot
或 scatter
命令而不是 plot!
和 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
仅绘制每一步中的最后一个当前点:
@gif for i in eachindex(x)
scatter((x[i], y[i]), lims = (-1, 1), label = "")
end
在当前位置用标记绘制所有先前的步骤:
@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
与上面相同,降低旧步骤的 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
我在 Julia 中求解了描述粒子运动的 ODE,并将坐标和各自的时间保存在数组中。我想创建一个动画 gif 图像,其中粒子沿着已求解的轨迹绘制,但要做到这一点(我想出的唯一方法)是使用 scatter
绘制粒子的位置,并且每时每刻擦除粒子的先前位置。但是我只知道 scatter!
会在图中添加更多粒子而不是显示粒子位置的变化。那么我怎样才能每次迭代都删除以前的情节,或者有更聪明的方法来做到这一点?如果我想使用绘图标记早期粒子的轨迹怎么办?
无法使用 Plots.jl 删除以前的数据。可以使用 plot
或 scatter
命令而不是 plot!
和 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
仅绘制每一步中的最后一个当前点:
@gif for i in eachindex(x)
scatter((x[i], y[i]), lims = (-1, 1), label = "")
end
在当前位置用标记绘制所有先前的步骤:
@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
与上面相同,降低旧步骤的 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