# Julia plots are very slow in every run

I am relatively new to Julia. I find plotting very slow; though I found some questions and responses about slow plotting for the first time due to compilation, in my case it is slow every time. Here is what I am doing:

``````@gif for n ∈ 1:nt
surface(x,y,un[:,:,n], ylims=(0,2), xlims=(0,2), zlims=(1,2))
end
``````

It took 11 minutes 15.3 seconds! The size of `un` is (161, 161, 100).
Is it normal? Or what should I do to make it faster? I am using Julia version 1.6.7 on Ubuntu 20.04 on VS Code. I tried to run the same code using `julia script.jl` command. It also takes about the same time.

For me , the following code launched in vscode (after precompilation) runs in a few seconds:

``````using Plots
nx,ny,nt=161,161,100
un = randn(nx,ny,nt)
x = 1:nx
y = 1:ny
@gif for n ∈ 1:nt
print(n)
surface(x,y,un[:,:,n], ylims=(0,2), xlims=(0,2), zlims=(1,2))
end
``````

I did a first run to precompile using `nx,ny,nt=10,10,10` to fasten things.

This is clearly not normal times you are observing. Even the compilation time should not be that long.

1 Like

That definitely sounds excessive - your code doesn’t run, but taking the example from the Plots.jl docs I get:

``````julia> @time @gif for i in range(0, stop = 2π, length = n)
f(x, y) = sin(x + 10sin(i)) + cos(y)

# create a plot with 3 subplots and a custom layout
l = @layout [a{0.7w} b; c{0.2h}]
p = plot(x, y, f, st = [:surface, :contourf], layout = l)

# induce a slight oscillating camera angle sweep, in degrees (azimuth, altitude)
plot!(p, camera = (10 * (1 + cos(i)), 40))

fixed_x = zeros(40)
z = map(f, fixed_x, y)
plot!(p, fixed_x, y, z, line = (:black, 5, 0.2))
vline!(p, , line = (:black, 5))

# add to and show the tracked values over time
global zs = vcat(zs, z')
plot!(p, zs, alpha = 0.2, palette = cgrad(:blues).colors)
end
[ Info: Saved animation to ...\Temp\jl_S5GrOzKpwz.gif
21.477568 seconds (3.73 M allocations: 281.195 MiB, 0.19% gc time, 0.19% compilation time)
``````

here `n = 100`, so there are as many frames as you’re using. Of course for all we know you might have overloaded `getindex` for whatever type `un` is and `un[:, :, n]` actually performs some really complicated calculation, but that’s impossible to tell without an MWE.

`un` is just a 3d array. I have used a plot for a simple 1d array and it just plots in a reasonable time. So, it might be due to the reason you suspect. But I did not overload `getindex`. How can I avoid `un[:, :, n]`?
You can find the complete notebook on GitHub.

I was using `pyplot()` and after I removed it, the run time decreased largely. The runtime is now 24 seconds as `nt=300` and I precompiled this snippet with `nt=2` before. Is `pyplot()` the cause for longer runtime?

The problem is gone ( runtime is 24 seconds now) as I removed `pyplot()` from `using Plots; pyplot()` command.

1 Like