# Speed up heatmap for large (2500x2500) matrices?

**URL:** https://discourse.julialang.org/t/speed-up-heatmap-for-large-2500x2500-matrices/52310
**Category:** New to Julia
**Tags:** visualization
**Created:** [December 24, 2020, 1:09am UTC](https://discourse.julialang.org/t/speed-up-heatmap-for-large-2500x2500-matrices/52310 "2020-12-24T01:09:43Z")
**Posts on this page:** 14
**Page:** 1

<div class="post-metadata">

### Author: ![vshesh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vshesh/32/20362_2.png) [@vshesh](https://discourse.julialang.org/u/vshesh)
#### Post date: [December 24, 2020, 1:09am UTC](https://discourse.julialang.org/t/speed-up-heatmap-for-large-2500x2500-matrices/52310/1 "2020-12-24T01:09:43Z")

</div>

Hi,

Trying to plot a heatmap of a distance matrix that is 2500x2500 give or take.  
This takes almost a minute to complete using Plots.jl and GR as the backend, and it breaks pluto/plotlyjs when i try it interactively.  
In mathematica i can do a matrixplot of a 2000x2000 matrix in less than a second … seems like in julia doing the computation is fast, showing it is slow and in mma doing the computation is slow and showing it is fast ☹

i’m figuring i got something wrong - i read some stuff on the viz category; couldn’t find anything that talked about very large matrixplots.

You can get the behavior I’m talking about by simply doing:

```julia
> using StatsPlots
> @time heatmap(randn((2500,2500)))
  0.139569 seconds (11.03 k allocations: 61.574 MiB)

```

Except that time is a lie because I counted 48 seconds before the window with GKSTerm showed me anything and the cursor for julia prompt came back.

System - Mac OS X 10.13, julia 1.5

---

<div class="post-metadata">

### Author: ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)
#### Post date: [December 24, 2020, 1:48am UTC](https://discourse.julialang.org/t/speed-up-heatmap-for-large-2500x2500-matrices/52310/2 "2020-12-24T01:48:47Z")

</div>

What happens if you run the command twice?

---

<div class="post-metadata">

### Author: ![vshesh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vshesh/32/20362_2.png) [@vshesh](https://discourse.julialang.org/u/vshesh)
#### Post date: [December 24, 2020, 1:54am UTC](https://discourse.julialang.org/t/speed-up-heatmap-for-large-2500x2500-matrices/52310/3 "2020-12-24T01:54:51Z")

</div>

I’ve been running this heatmap computation over and over again in the terminal with different matrices and getting similar run times each time.  
my workflow is

1. change distance function
2. compute distance matrix
3. plot matrix

i just reran the command to make sure before writing this and got a similar (45s) time to see the matrix plot. Again, i’m probably doing something dumb, just not clear what.

---

<div class="post-metadata">

### Author: ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)
#### Post date: [December 24, 2020, 3:51am UTC](https://discourse.julialang.org/t/speed-up-heatmap-for-large-2500x2500-matrices/52310/4 "2020-12-24T03:51:31Z")

</div>

looks like using GR directly is much faster:

```julia
julia> using GR

julia> a = rand(2500,2500);

julia> @time display(heatmap(a))
nothing
  3.725224 seconds (39.82 M allocations: 1.523 GiB, 8.06% gc time)

julia> @time display(heatmap(a))
nothing
  1.102791 seconds (31.25 M allocations: 1.094 GiB, 6.55% gc time)

```

---

<div class="post-metadata">

### Author: ![vshesh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vshesh/32/20362_2.png) [@vshesh](https://discourse.julialang.org/u/vshesh)
#### Post date: [December 24, 2020, 5:55am UTC](https://discourse.julialang.org/t/speed-up-heatmap-for-large-2500x2500-matrices/52310/5 "2020-12-24T05:55:49Z")

</div>

thank you - i think using GR directly is it - or at least it’s fast enough for good iterative workflow  
the great news is that this works in pluto directly too (i guess b/c it’s rendering a raster image instead of trying to draw 40,000,000 rectangles in svg and there’s no interactivity?) so i can go back to working iteratively

---

<div class="post-metadata">

### Author: ![purplishrock](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/purplishrock/32/13451_2.png) [@purplishrock](https://discourse.julialang.org/u/purplishrock)
#### Post date: [December 24, 2020, 6:11am UTC](https://discourse.julialang.org/t/speed-up-heatmap-for-large-2500x2500-matrices/52310/6 "2020-12-24T06:11:29Z")

</div>

I was curious what PyPlot would do with this, so I tried it.

```julia
julia> a=rand(2500,2500)
julia> @time imshow(a, cmap="cool", interpolation=:nearest)
  0.051976 seconds (69 allocations: 2.734 KiB)
PyObject <matplotlib.image.AxesImage object at 0x7f2cbd9808b0>

```

Interesting…

---

<div class="post-metadata">

### Author: ![aramirezreyes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aramirezreyes/32/42573_2.png) [@aramirezreyes](https://discourse.julialang.org/u/aramirezreyes)
#### Post date: [December 24, 2020, 7:27am UTC](https://discourse.julialang.org/t/speed-up-heatmap-for-large-2500x2500-matrices/52310/7 "2020-12-24T07:27:58Z")

</div>

For the pluto case, i don’t know if it works the same way as jupyter, but you could try doing

```julia
using Plots
default(fmt=:png)

```

In jupyter it breaks because it is drawing an svg that is extremely heavy, using png will avoid that problem (I don’t think it will speed up the REPL, though).

[Reference](https://github.com/JuliaPlots/Plots.jl/issues/2111)

---

<div class="post-metadata">

### Author: ![LaurentPlagne](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/laurentplagne/32/10103_2.png) [@LaurentPlagne](https://discourse.julialang.org/u/LaurentPlagne)
#### Post date: [December 24, 2020, 1:33pm UTC](https://discourse.julialang.org/t/speed-up-heatmap-for-large-2500x2500-matrices/52310/8 "2020-12-24T13:33:29Z")

</div>

GLMakie is much faster

---

<div class="post-metadata">

### Author: ![aramirezreyes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aramirezreyes/32/42573_2.png) [@aramirezreyes](https://discourse.julialang.org/u/aramirezreyes)
#### Post date: [December 25, 2020, 3:34am UTC](https://discourse.julialang.org/t/speed-up-heatmap-for-large-2500x2500-matrices/52310/9 "2020-12-25T03:34:11Z")

</div>

I think that is true, but it is still worth it to try and fix it in Plots. If matplotlib can do it in 1/20th of the time Plots takes using the cpu, there may be something wrong on our side.

---

<div class="post-metadata">

### Author: ![vshesh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vshesh/32/20362_2.png) [@vshesh](https://discourse.julialang.org/u/vshesh)
#### Post date: [December 25, 2020, 3:41am UTC](https://discourse.julialang.org/t/speed-up-heatmap-for-large-2500x2500-matrices/52310/10 "2020-12-25T03:41:40Z")

</div>

Is there a way to mark multiple solutions?  
the default(fmt=:png) seems to have sped things up in pluto as well although not as much as using GR directly. I didn’t want to install another distribution of python on my laptop so staying away from matplotlib/python for now, although I do appreciate the speed.  
How do you explain GR allocating 1 GB of memory vs matplotlib allocating only 2KB!? that seems like an enormous difference for the same task.

It would be nice if (Stats)Plots could have this be fast too so I can take advantage of the nice documentation and interface… as a new julian GR’s direct documentation is hard to understand.

I think the best solution I can see right now for fast matrix plot with useable API without installing conda python is GRUtils… in case that helps anyone else in my position.

---

<div class="post-metadata">

### Author: ![LaurentPlagne](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/laurentplagne/32/10103_2.png) [@LaurentPlagne](https://discourse.julialang.org/u/LaurentPlagne)
#### Post date: [December 25, 2020, 5:29pm UTC](https://discourse.julialang.org/t/speed-up-heatmap-for-large-2500x2500-matrices/52310/11 "2020-12-25T17:29:27Z")

</div>

FWIW, plotting a 2500x2500 Float64 heatmap takes around 0.05s with GLMakie.jl (on the second call).

---

<div class="post-metadata">

### Author: ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)
#### Post date: [December 25, 2020, 8:08pm UTC](https://discourse.julialang.org/t/speed-up-heatmap-for-large-2500x2500-matrices/52310/12 "2020-12-25T20:08:53Z")

</div>

Regardless of the backend, try to set `gr(format=:png)` to avoid vector graphics in the notebook (the default is `:svg`). I do this trick all the time in my tutorials and lectures because some students have old machines.

---

<div class="post-metadata">

### Author: ![vshesh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vshesh/32/20362_2.png) [@vshesh](https://discourse.julialang.org/u/vshesh)
#### Post date: [December 25, 2020, 8:32pm UTC](https://discourse.julialang.org/t/speed-up-heatmap-for-large-2500x2500-matrices/52310/13 "2020-12-25T20:32:42Z")

</div>

ok, can confirm that GLMakie is indeed really fast, and using png in the notebook is the way to go  
thank you all for the help!

---

<div class="post-metadata">

### Author: ![vshesh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vshesh/32/20362_2.png) [@vshesh](https://discourse.julialang.org/u/vshesh)
#### Post date: [December 26, 2020, 9:10pm UTC](https://discourse.julialang.org/t/speed-up-heatmap-for-large-2500x2500-matrices/52310/14 "2020-12-26T21:10:04Z")

</div>

GLMakie hangs in pluto though, this has been running for 5m:

```julia
> GLMakie.heatmap(randn(100, 100))

```

it takes \<1s on the terminal, somehow pluto takes a long time to render it.  
am i supposed to use WebGLMakie for pluto?
