# 3D plot animation examples¶

• Gen Kuroki
• 2021-08-07～

## Generate test data of simple arrays¶

The test data X, Y, and Z are 1-dimensional arrays (vectors) of length $10^4+1$.

In [1]:
# The following code is copied from https://diffeq.sciml.ai/stable/basics/plot/#Example

using DifferentialEquations, Plots
function lorenz(du,u,p,t)
du[1] = p[1]*(u[2]-u[1])
du[2] = u[1]*(p[2]-u[3]) - u[2]
du[3] = u[1]*u[2] - p[3]*u[3]
end

u0 = [1., 5., 10.]
tspan = (0., 100.)
p = (10.0,28.0,8/3)
prob = ODEProblem(lorenz, u0, tspan,p)
sol = solve(prob);

In [2]:
# Create data of simple arrays for test plotting

t = range(sol.prob.tspan...; length=10^4+1)
X, Y, Z = ((t -> sol(t)[i]).(t) for i in 1:3)
xlim, ylim, zlim = extrema.((X, Y, Z));


## 3D plotting with gr() backend¶

In [3]:
gr(fmt = :png)
plot(X, Y, Z; label="", lw=0.5)

Out[3]:
In [4]:
gr(fmt = :png)
anim = @animate for i in 1:100:length(X)
@views plot(X[1:i], Y[1:i], Z[1:i]; label="", lw=0.5)
plot!(; xlim, ylim, zlim)
end
gif(anim, "lorenz1.gif")

┌ Info: Saved animation to
│   fn = D:\OneDrive\public\0015\lorenz1.gif
└ @ Plots D:\.julia\packages\Plots\Awg62\src\animation.jl:104

Out[4]:
In [5]:
gr(fmt = :png)
x, y, z = X, Y, Z
A = plot(x, y, z; label="", lw=0.5)
B = plot(x, y; label="", lw=0.2, title="x, y", titlefontsize=8)
C = plot(x, z; label="", lw=0.2, title="x, z", titlefontsize=8)
D = plot(y, z; label="", lw=0.2, title="y, z", titlefontsize=8)
layout = @layout [
a{0.7h}
[b c d]
]
plot(A, B, C, D; layout, size=(640, 640))

Out[5]:
In [6]:
gr(fmt = :png)
anim = @animate for i in 1:100:length(X)
@views x, y, z = X[1:i], Y[1:i], Z[1:i]
A = plot(x, y, z; label="", lw=0.5, xlim, ylim, zlim)
B = plot(x, y; label="", lw=0.2, title="x, y", titlefontsize=8, xlim=xlim, ylim=ylim)
C = plot(x, z; label="", lw=0.2, title="x, y", titlefontsize=8, xlim=xlim, ylim=zlim)
D = plot(y, z; label="", lw=0.2, title="x, y", titlefontsize=8, xlim=ylim, ylim=zlim)
layout = @layout [
a{0.7h}
[b c d]
]
plot(A, B, C, D; layout, size=(640, 640))
end
gif(anim, "lorenz2.gif")

┌ Info: Saved animation to
│   fn = D:\OneDrive\public\0015\lorenz2.gif
└ @ Plots D:\.julia\packages\Plots\Awg62\src\animation.jl:104

Out[6]: