# Julia plots are very slow in every run

**URL:** https://discourse.julialang.org/t/julia-plots-are-very-slow-in-every-run/89863
**Category:** Visualization
**Tags:** question, plotting, performance
**Created:** [November 7, 2022, 9:08am UTC](https://discourse.julialang.org/t/julia-plots-are-very-slow-in-every-run/89863 "2022-11-07T09:08:45Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Fuad\_Hasibul\_Hasan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fuad_hasibul_hasan/32/29764_2.png) [@Fuad\_Hasibul\_Hasan](https://discourse.julialang.org/u/Fuad_Hasibul_Hasan)
#### Post date: [November 7, 2022, 9:08am UTC](https://discourse.julialang.org/t/julia-plots-are-very-slow-in-every-run/89863/1 "2022-11-07T09:08:45Z")

</div>

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:

```julia
@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.

---

<div class="post-metadata">

### Author: ![lrnv](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lrnv/32/19373_2.png) [@lrnv](https://discourse.julialang.org/u/lrnv)
#### Post date: [November 7, 2022, 9:20am UTC](https://discourse.julialang.org/t/julia-plots-are-very-slow-in-every-run/89863/2 "2022-11-07T09:20:44Z")

</div>

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

```julia
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.

---

<div class="post-metadata">

### Author: ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)
#### Post date: [November 7, 2022, 9:20am UTC](https://discourse.julialang.org/t/julia-plots-are-very-slow-in-every-run/89863/3 "2022-11-07T09:20:53Z")

</div>

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

```julia
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[1], camera = (10 * (1 + cos(i)), 40))

                         # add a tracking line
                         fixed_x = zeros(40)
                         z = map(f, fixed_x, y)
                         plot!(p[1], fixed_x, y, z, line = (:black, 5, 0.2))
                         vline!(p[2], [0], line = (:black, 5))

                         # add to and show the tracked values over time
                         global zs = vcat(zs, z')
                         plot!(p[3], 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.

---

<div class="post-metadata">

### Author: ![Fuad\_Hasibul\_Hasan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fuad_hasibul_hasan/32/29764_2.png) [@Fuad\_Hasibul\_Hasan](https://discourse.julialang.org/u/Fuad_Hasibul_Hasan)
#### Post date: [November 7, 2022, 1:46pm UTC](https://discourse.julialang.org/t/julia-plots-are-very-slow-in-every-run/89863/4 "2022-11-07T13:46:15Z")

</div>

`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](https://github.com/Fuad-HH/CFDJulia/blob/d587c69f2ea0f1f51af70871cbea43893311bc7f/step5.ipynb).

---

<div class="post-metadata">

### Author: ![Fuad\_Hasibul\_Hasan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fuad_hasibul_hasan/32/29764_2.png) [@Fuad\_Hasibul\_Hasan](https://discourse.julialang.org/u/Fuad_Hasibul_Hasan)
#### Post date: [November 7, 2022, 2:12pm UTC](https://discourse.julialang.org/t/julia-plots-are-very-slow-in-every-run/89863/5 "2022-11-07T14:12:49Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![Fuad\_Hasibul\_Hasan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fuad_hasibul_hasan/32/29764_2.png) [@Fuad\_Hasibul\_Hasan](https://discourse.julialang.org/u/Fuad_Hasibul_Hasan)
#### Post date: [November 7, 2022, 2:24pm UTC](https://discourse.julialang.org/t/julia-plots-are-very-slow-in-every-run/89863/6 "2022-11-07T14:24:54Z")

</div>

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