# How to properly implement Heatmap efficiently?

**URL:** https://discourse.julialang.org/t/how-to-properly-implement-heatmap-efficiently/127582
**Category:** New to Julia
**Created:** [April 1, 2025, 8:34am UTC](https://discourse.julialang.org/t/how-to-properly-implement-heatmap-efficiently/127582 "2025-04-01T08:34:14Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![matyaspayn](https://avatars.discourse-cdn.com/v4/letter/m/57b2e6/32.png) [@matyaspayn](https://discourse.julialang.org/u/matyaspayn)
#### Post date: [April 1, 2025, 8:34am UTC](https://discourse.julialang.org/t/how-to-properly-implement-heatmap-efficiently/127582/1 "2025-04-01T08:34:14Z")

</div>

I am currently trying to visualise a solution to a set of differential equations, where u is my solution, and though my code works the running of the heatmap to visualise it takes at least 200 seconds each time. It should be noted here that u is a vector of length 2^10 and m is 200,000. I understand that this is a large problem but I just don’t see why copying values across and plotting is taking so long.

begin

```
U = zeros(m,n)

for i = 1:m
    U[i, :] = u[:,i]
end

Plots.heatmap(U)

```

end

This code is not runnable but I am hoping that someone can still explain if I am doing something inefficient here. If it needs to be run, then I can copy all the code as unfortunately there is no other way to make it self contained.

Thank you for the help.

---

<div class="post-metadata">

### Author: ![matyaspayn](https://avatars.discourse-cdn.com/v4/letter/m/57b2e6/32.png) [@matyaspayn](https://discourse.julialang.org/u/matyaspayn)
#### Post date: [April 1, 2025, 8:38am UTC](https://discourse.julialang.org/t/how-to-properly-implement-heatmap-efficiently/127582/2 "2025-04-01T08:38:10Z")

</div>

I should note that I am using Pluto

---

<div class="post-metadata">

### Author: ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)
#### Post date: [April 1, 2025, 8:42am UTC](https://discourse.julialang.org/t/how-to-properly-implement-heatmap-efficiently/127582/3 "2025-04-01T08:42:38Z")

</div>

> [@matyaspayn](#):
>
> ```julia
> for i = 1:m
> U[i, :] = u[:,i]
> end
> 
> ```

Four things:

- Every `u[:, i]` is a slice, which creates an allocated vector. You can try using `view(u, :, i)`.
- Writing to `U` row-wise is inefficient, perhaps you can reorganize `U` to receive data columnwise.
- Make sure your code is in a function, and that this is not happening in global scope.
- Finally, are you just trying to create a transpose? Can you just write `Plots.heatmap(u')` instead?

---

<div class="post-metadata">

### Author: ![barucden](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/barucden/32/26154_2.png) [@barucden](https://discourse.julialang.org/u/barucden)
#### Post date: [April 1, 2025, 8:51am UTC](https://discourse.julialang.org/t/how-to-properly-implement-heatmap-efficiently/127582/4 "2025-04-01T08:51:58Z")

</div>

I agree with everything DNF said, but I would like to also point out that you are (if I am not mistaken) trying to plot a heatmap of size 200\,000 \times 1024. That’s a lot of points, and I am afraid that most of the time is spent actually plotting (and not creating `U`).

You might consider switching from plotting a heatmap to plotting an image. Makie.jl has a function for that ([`image`](https://docs.makie.org/dev/reference/plots/image)). I am not sure how to do it in `Plots`, but it seems that you’d have to convert `U` to a matrix of `RGB`/`Gray` (a type from ColorTypes.jl) and just use `plot`.

---

<div class="post-metadata">

### Author: ![slumpy](https://avatars.discourse-cdn.com/v4/letter/s/a88e4f/32.png) [@slumpy](https://discourse.julialang.org/u/slumpy)
#### Post date: [April 1, 2025, 9:24am UTC](https://discourse.julialang.org/t/how-to-properly-implement-heatmap-efficiently/127582/5 "2025-04-01T09:24:23Z")

</div>

You might also want to try outside pluto.

---

<div class="post-metadata">

### Author: ![matyaspayn](https://avatars.discourse-cdn.com/v4/letter/m/57b2e6/32.png) [@matyaspayn](https://discourse.julialang.org/u/matyaspayn)
#### Post date: [April 1, 2025, 10:01am UTC](https://discourse.julialang.org/t/how-to-properly-implement-heatmap-efficiently/127582/6 "2025-04-01T10:01:07Z")

</div>

Thank you for the help, I ended up using the simple transpose (don’t know how I missed that) and also reduced the number of points by a factor of 10. This seems to have done the job, although the time taken still varies sometimes.

I will also try the image method.

---

<div class="post-metadata">

### Author: ![matyaspayn](https://avatars.discourse-cdn.com/v4/letter/m/57b2e6/32.png) [@matyaspayn](https://discourse.julialang.org/u/matyaspayn)
#### Post date: [April 1, 2025, 10:01am UTC](https://discourse.julialang.org/t/how-to-properly-implement-heatmap-efficiently/127582/7 "2025-04-01T10:01:19Z")

</div>

What do you mean by in a function?

---

<div class="post-metadata">

### Author: ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)
#### Post date: [April 1, 2025, 10:04am UTC](https://discourse.julialang.org/t/how-to-properly-implement-heatmap-efficiently/127582/8 "2025-04-01T10:04:29Z")

</div>

If you run this code

```julia
for i = 1:m
    U[i, :] = u[:,i]
end

```

in global scope, and not inside a function, it will be slow. If it isn’t already in a function, you should write

```julia
function mytranspose!(U, u)
    for i = 1:m
        U[i, :] = u[:,i] # better to use view here
    end
    return U
end

```

and then call the function in your script.

I _strongly_ recommend reading [Performance Tips · The Julia Language](https://docs.julialang.org/en/v1/manual/performance-tips/)  
_Any_ question you have on Julia performance should be addressed by first consulting that page, before looking elsewhere.

---

<div class="post-metadata">

### Author: ![barucden](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/barucden/32/26154_2.png) [@barucden](https://discourse.julialang.org/u/barucden)
#### Post date: [April 1, 2025, 11:26am UTC](https://discourse.julialang.org/t/how-to-properly-implement-heatmap-efficiently/127582/9 "2025-04-01T11:26:30Z")

</div>

I tried the following out of curiosity:

```julia-repl
julia> using Plots

julia> const U = rand(1000, 1000);

julia> @time plot(heatmap(U)); # Second run
  0.015866 seconds (897 allocations: 15.411 MiB)

julia> @time plot(Gray.(U)); # Second run
  0.009538 seconds (456 allocations: 7.696 MiB)

```

You can see that `plot` is significantly faster than `heatmap` already for this size of `U`. I suspect that the difference will be even more substantial for bigger sizes.

Note that the results are different though:

**heatmap**

 ![heatmap](https://global.discourse-cdn.com/julialang/original/3X/0/8/087340179ef13ae1e68d89500f5fa04bb6149ce9.png)

**plot**

 ![plot](https://global.discourse-cdn.com/julialang/original/3X/e/e/ee30eaca494964bb241f39589e191d58ca7614bd.png)

So you’d have to add a colorbar etc. Moreover, `plot` rasterizes the image (which allows for faster plotting), while `heatmap` produces vector graphics.

---

<div class="post-metadata">

### Author: ![JADekker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jadekker/32/210281_2.png) [@JADekker](https://discourse.julialang.org/u/JADekker)
#### Post date: [April 1, 2025, 11:28am UTC](https://discourse.julialang.org/t/how-to-properly-implement-heatmap-efficiently/127582/10 "2025-04-01T11:28:22Z")

</div>

Some minor suggestions (that should be covered by the performance tips):

```julia
# You are overwriting everything, no reason to first place zeros
#U = zeros(m,n)
U = Matrix{eltype(u)}(undef, m, n) 

# By construction, i should be in-bounds. 
# Also, you may wish to iterate over an axes
#for i = 1:m
@inbounds for i in axes(U, 1)
    # Avoid allocating an intermediate array when slicing
    # Also, use .= for assigning values
    U[i, :] .= view(u, i)
    #U[i, :] = u[:,i]
end

```
