I did some experiments, and I found two things that could dramatically speed up the plotting process.
First, rather than redrawing the plots using surface!() and heatmap!(), I simply replaced their :z series. This is shown by comparing the first to the third and second to the fourth functions in the code example.
Second, GR.jl; setcharheight is extremely slow. This is likely due to the ccall(), which means it may be OS dependent. By setting xticks and yticks to false, significant speedups were achieved. This is shown by comparing the first to second and third to fourth functions in the example.
using Plots
function mcve(n,A,B)
l = @layout [ a b ; c]
p1 = surface(1:100,1:100,A[1,:,:],clims=(0,1),legend=false)
p2 = heatmap(A[1,:,:],clims=(0,1),aspect_ratio=1,legend=false)
p3 = heatmap(B[1,:,:],aspect_ratio=1)
p = plot(p1,p2,p3,layout = l)
anim = @animate for i=1:n
surface!(p[1],1:100,1:100,A[i,:,:])
heatmap!(p[2],A[i,:,:])
heatmap!(p[3],B[i,:,:])
end
gif(anim,"example1.gif")
end
function mcve4(n,A,B)
l = @layout [ a b ; c]
p1 = surface(1:100,1:100,A[1,:,:],clims=(0,1),legend=false,xticks=false,yticks=false)
p2 = heatmap(A[1,:,:],clims=(0,1),aspect_ratio=1,legend=false,xticks=false,yticks=false)
p3 = heatmap(B[1,:,:],aspect_ratio=1,xticks=false,yticks=false)
p = plot(p1,p2,p3,layout = l)
anim = @animate for i=1:n
surface!(p[1],1:100,1:100,A[i,:,:],xticks=false,yticks=false)
heatmap!(p[2],A[i,:,:],xticks=false,yticks=false)
heatmap!(p[3],B[i,:,:],xticks=false,yticks=false)
end
gif(anim,"example4.gif")
end
function mcve2(n,A,B)
l = @layout [ a b ; c]
p1 = surface(1:100,1:100,A[1,:,:],clims=(0,1),legend=false,xticks =false,yticks= false)
p2 = heatmap(A[1,:,:],clims=(0,1),aspect_ratio=1,legend=false,xticks = false,yticks= false)
p3 = heatmap(B[1,:,:],aspect_ratio=1,xticks = false,yticks= false)
p = plot(p1,p2,p3,layout = l)
anim = @animate for i=1:n
p[1][1][:z] = A[i,:,:]
p[2][1][:z] = A[i,:,:]
p[3][1][:z] = B[i,:,:]
end
gif(anim,"example2.gif")
end
function mcve3(n,A,B)
l = @layout [ a b ; c]
p1 = surface(1:100,1:100,A[1,:,:],clims=(0,1),legend=false)
p2 = heatmap(A[1,:,:],clims=(0,1),aspect_ratio=1,legend=false)
p3 = heatmap(B[1,:,:],aspect_ratio=1)
p = plot(p1,p2,p3,layout = l)
anim = @animate for i=1:n
p[1][1][:z] = A[i,:,:]
p[2][1][:z] = A[i,:,:]
p[3][1][:z] = B[i,:,:]
end
gif(anim,"example3.gif")
end
A = rand(1,100,100)
B = rand(1,100,100)
mcve(1,A,B)
mcve2(1,A,B)
mcve3(1,A,B)
mcve4(1,A,B)
A = rand(10,100,100)
B = rand(10,100,100)
println("Replot,ticks on")
@time mcve(10,A,B)
println("Replot,ticks off")
@time mcve4(10,A,B)
println(":z replace, ticks on")
@time mcve3(10,A,B)
println(":z replace, ticks off")
@time mcve2(10,A,B)
which results in
Replot,ticks on
19.347849 seconds (12.78 M allocations: 399.848 MiB, 0.30% gc time)
Replot,ticks off
6.227432 seconds (8.71 M allocations: 298.890 MiB, 0.88% gc time)
:z replace, ticks on
8.572728 seconds (5.43 M allocations: 149.359 MiB, 0.24% gc time)
:z replace, ticks off
1.805316 seconds (1.36 M allocations: 48.450 MiB, 0.40% gc time)