# Skip empty entries of sparse matrix when plotting a heatmap

**URL:** <https://discourse.julialang.org/t/skip-empty-entries-of-sparse-matrix-when-plotting-a-heatmap/135343>\
**Category:** General Usage\
**Tags:** plots\
**Created:** [January 30, 2026, 1:00pm UTC](https://discourse.julialang.org/t/skip-empty-entries-of-sparse-matrix-when-plotting-a-heatmap/135343 "2026-01-30T13:00:10Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [January 30, 2026, 1:37pm UTC](https://discourse.julialang.org/t/skip-empty-entries-of-sparse-matrix-when-plotting-a-heatmap/135343/2 "2026-01-30T13:37:21Z")

</div>

> [@lmiq](#):
>
> Can anyone see a smart way of achieving this without creating a dense matrix as an intermediate?

Is there a strong reason to avoid this? The memory usage for `heatmap` is probably going to be proportional to the size of a dense matrix anyway.

To ensure that you only avoid plotting “structural” zeros, you can use `findnz` when creating the dense matrix:

```julia-auto
X = sprand(Float64, 5, 5, 0.5)
I, J, V = findnz(X)
A = fill(NaN, size(X))
setindex!.((A,), V, I, J)

```

(here I used `NaN` instead of `missing` to avoid having to deal with `Union` types).

If the matrix is so huge that allocating a dense version is a problem, then probably you don’t want `heatmap` — you want some kind of smart downsampling algorithm, as discussed in [Plotting image data in Julia is much slower than MATLAB - #17 by stevengj](https://discourse.julialang.org/t/plotting-image-data-in-julia-is-much-slower-than-matlab/79660/17)

---

_[View the full topic](https://discourse.julialang.org/t/skip-empty-entries-of-sparse-matrix-when-plotting-a-heatmap/135343)._
