We need Pyplot.using3D()
. See https://github.com/JuliaPy/PyPlot.jl#3d-plotting
using Base64
displayfile(mime, file) = open(file) do f
base64 = base64encode(f)
display("text/html", """<img src="data:$mime;base64,$base64">""")
end
using PyPlot: PyPlot, plt
PyPlot.using3D()
using PyCall
anim = pyimport("matplotlib.animation")
using Random: seed!
function makegif(giffile, rwalk, plot_rwalk)
L = size(rwalk, 2)
fig = plt.figure()
frame(t) = plot_rwalk(fig, rwalk, t)
ts = [fill(1,10); 1:10:L; fill(L,10)]
myanim = anim.FuncAnimation(fig, frame, frames=ts, interval=100)
myanim.save(giffile, writer="imagemagick")
plt.clf()
end
makegif (generic function with 1 method)
function plot_rwalk(fig, rwalk, t)
xmin, xmax = extrema(rwalk[1,:])
ymin, ymax = extrema(rwalk[2,:])
zmin, zmax = extrema(rwalk[3,:])
fig.clf()
ax = fig.add_subplot(111, projection="3d")
ax.set_xlabel("x")
ax.set_ylabel("y")
ax.set_zlabel("z")
ax.set_xlim(xmin, xmax)
ax.set_ylim(ymin, ymax)
ax.set_zlim(zmin, zmax)
ax.plot3D(rwalk[1,1:t], rwalk[2,1:t], rwalk[3,1:t], lw=0.5)
end
seed!(2019)
rwalk = cumsum(randn(3, 2000); dims=2)
fig = plt.figure()
plot_rwalk(fig, rwalk, size(rwalk,2))
1-element Array{PyObject,1}: PyObject <mpl_toolkits.mplot3d.art3d.Line3D object at 0x0000000044BA6CC0>
giffile = "PyPlot_plot_rwalk.gif"
@time makegif(giffile, rwalk, plot_rwalk)
displayfile("image/gif", giffile)