# Plotting slow

**URL:** https://discourse.julialang.org/t/plotting-slow/111083
**Category:** Performance
**Created:** [March 3, 2024, 8:07am UTC](https://discourse.julialang.org/t/plotting-slow/111083 "2024-03-03T08:07:42Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Fourier](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fourier/32/38176_2.png) [@Fourier](https://discourse.julialang.org/u/Fourier)
#### Post date: [March 3, 2024, 8:07am UTC](https://discourse.julialang.org/t/plotting-slow/111083/1 "2024-03-03T08:07:42Z")

</div>

We are currently working on a simulation and the bottleneck is plotting.  
We are plotting a grid of dimension 1000 x 2000 and saving it.

```julia
  
T_c_hm = heatmap(x, y, transpose(T_c_renh), size = (1000, 400), clim=(0, 0.3))
savefig("run/T_c_$it_text.png")
T_s_hm = heatmap(x, y, transpose(T_s_renh), size = (1000, 400), clim=(0, 1.0))
savefig("run/T_s_$it_text.png")
u_hm = heatmap(x, y, transpose(v_renh), size = (1000, 400), clim=(0,0.3))
savefig("run/u_$it_text.png")

```

We tried using `gr()` with no avail.

---

<div class="post-metadata">

### Author: ![gdalle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdalle/32/27854_2.png) [@gdalle](https://discourse.julialang.org/u/gdalle)
#### Post date: [March 3, 2024, 8:08am UTC](https://discourse.julialang.org/t/plotting-slow/111083/2 "2024-03-03T08:08:36Z")

</div>

Maybe try Makie.jl?

[https://docs.makie.org/stable/reference/plots/heatmap/index.html](https://docs.makie.org/stable/reference/plots/heatmap/index.html)

---

<div class="post-metadata">

### Author: ![jheinen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jheinen/32/2787_2.png) [@jheinen](https://discourse.julialang.org/u/jheinen)
#### Post date: [March 3, 2024, 8:58am UTC](https://discourse.julialang.org/t/plotting-slow/111083/3 "2024-03-03T08:58:31Z")

</div>

What would you expect?

```julia
using GR
@time heatmap(randn(1000,2000))
  0.197676 seconds (8.00 M allocations: 160.242 MiB, 24.41% gc time)

```

Do you have a complete MWE?

---

<div class="post-metadata">

### Author: ![Fourier](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fourier/32/38176_2.png) [@Fourier](https://discourse.julialang.org/u/Fourier)
#### Post date: [March 3, 2024, 9:08am UTC](https://discourse.julialang.org/t/plotting-slow/111083/5 "2024-03-03T09:08:26Z")

</div>

Its a bit frustrating when you have an 80 Terraflop GPU that does all the computation in 20 ms and then taking 200 ms per plot, so I guess we are going to implement it our own. Is there a fast png library that wont bottleneck again?

---

<div class="post-metadata">

### Author: ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)
#### Post date: [March 3, 2024, 9:20am UTC](https://discourse.julialang.org/t/plotting-slow/111083/6 "2024-03-03T09:20:47Z")

</div>

Perhaps you could save this as an image rather than trying to plot it as a heatmap?

---

<div class="post-metadata">

### Author: ![jar1](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jar1](https://discourse.julialang.org/u/jar1)
#### Post date: [March 3, 2024, 9:29am UTC](https://discourse.julialang.org/t/plotting-slow/111083/7 "2024-03-03T09:29:01Z")

</div>

If you’re interested in GPU plotting, I would at least consider if [GLMakie](https://docs.makie.org/stable/explanations/backends/glmakie/index.html) meets your needs.

```julia
using GLMakie
julia> @time let
       f = Figure(size=(2000,1000))
       ax = Axis(f[1, 1])
       
       centers_x = 1:5
       centers_y = 6:10
       data = reshape(rand(25), 5, 5)
       
       heatmap!(ax, centers_x, centers_y, data)
       save("/tmp/my.png", f)
       end
  0.025841 seconds (96.64 k allocations: 8.738 MiB)

```

---

<div class="post-metadata">

### Author: ![Fourier](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fourier/32/38176_2.png) [@Fourier](https://discourse.julialang.org/u/Fourier)
#### Post date: [March 4, 2024, 12:57pm UTC](https://discourse.julialang.org/t/plotting-slow/111083/8 "2024-03-04T12:57:43Z")

</div>

Very nice, thanks a lot. The solution we had at the end was using ParralelStencil

```julia
@parallel_indices (i, j) function heatmap_para!(image, array, min_val, max_val, table)
    x = (array[i, j] - min_val) / (max_val - min_val)

    for k in 1:3
        col = 0.0
 
        for l in 1:6
            col *= x
            col += table[k, 7-l]
        end

        image[j, i, k] = clamp(col, 0, 1)
    end

    return nothing
end

```
