# Discriminate between NaN and Inf in plots

**URL:** https://discourse.julialang.org/t/discriminate-between-nan-and-inf-in-plots/112171
**Category:** Visualization
**Tags:** question
**Created:** [March 27, 2024, 7:21am UTC](https://discourse.julialang.org/t/discriminate-between-nan-and-inf-in-plots/112171 "2024-03-27T07:21:31Z")
**Posts on this page:** 8
**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 27, 2024, 7:21am UTC](https://discourse.julialang.org/t/discriminate-between-nan-and-inf-in-plots/112171/1 "2024-03-27T07:21:31Z")

</div>

I have a plot where some values originate from devision by 0 and some originate from 0/0, I would like to add new colours to the heatmap to descriminate between them.  
Does anyone have an idea on how to do this in plots.jl?

![f_mu_pi_atar](https://global.discourse-cdn.com/julialang/original/3X/d/8/d88da3b354b00b52a00089d83bf69f0ba2eecec7.png)

---

<div class="post-metadata">

### Author: ![Sevi](https://avatars.discourse-cdn.com/v4/letter/s/c67d28/32.png) [@Sevi](https://discourse.julialang.org/u/Sevi)
#### Post date: [March 27, 2024, 8:00am UTC](https://discourse.julialang.org/t/discriminate-between-nan-and-inf-in-plots/112171/2 "2024-03-27T08:00:20Z")

</div>

I don’t know if one can specify custom colors for `Inf` and `NaN` directly, but a possible workaround would be to manually plot something in the place of the infinities/NaNs with `scatter!`.

Here’s an example (note that I transposed the data for the heatmap, otherwise the definitions of the x and y axes will be flipped for `heatmap` and `scatter!`):

```julia
using Plots

N = 50

rawData = rand(N, N)

nanPointCoordinates = rand(CartesianIndices((1:N, 1:N)), 500)
infPointCoordinates = rand(CartesianIndices((1:N, 1:N)), 500)

rawData[nanPointCoordinates] .= NaN
rawData[infPointCoordinates] .= Inf

p = heatmap(transpose(rawData); c=:viridis)

scatter!(p, Tuple.(findall(isnan, rawData)); label="NaNs", markers=:square, markerstrokewidth=0, markercolor=:red, markersize=3)
scatter!(p, Tuple.(findall(isinf, rawData)); label="Infs", markers=:square, markerstrokewidth=0, markercolor=:black, markersize=3)

```

This will produce something like this (you would have to play around with the shapes/sizes/colors of the markers of course – also how to find the right x/y coordinates in your case, I just used the indices)

![heatmap_inf_nan](https://global.discourse-cdn.com/julialang/original/3X/f/5/f58cc11478ad0aba20c70c948a449088dcc5a374.png)

---

<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 27, 2024, 8:17am UTC](https://discourse.julialang.org/t/discriminate-between-nan-and-inf-in-plots/112171/3 "2024-03-27T08:17:49Z")

</div>

This is a very creative solution, thank you very much : )

---

<div class="post-metadata">

### Author: ![joa-quim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joa-quim/32/227_2.png) [@joa-quim](https://discourse.julialang.org/u/joa-quim)
#### Post date: [March 27, 2024, 1:38pm UTC](https://discourse.julialang.org/t/discriminate-between-nan-and-inf-in-plots/112171/4 "2024-03-27T13:38:36Z")

</div>

An alternative with grids and playing with the colormap

```julia
using GMT

N = 50;
rawData = rand(N, N);
nanPointCoordinates = rand(CartesianIndices((1:N, 1:N)), 500);
infPointCoordinates = rand(CartesianIndices((1:N, 1:N)), 500);

rawData[nanPointCoordinates] .= NaN;
rawData[infPointCoordinates] .= Inf;

# Convert data into a GMT grid
G = mat2grid(Float32.(rawData));

# Make a colormap with NaNs in gray and Infs in white
C = makecpt(range=(0,1), overrule_bg=true, conf=(COLOR_FOREGROUND=:white, COLOR_NAN=:gray));

viz(G, cmap=C, colorbar=true)

```

 ![nanfg](https://global.discourse-cdn.com/julialang/original/3X/1/8/187feedcac6489b69a8ce4dadc69d43f6bba1b19.jpeg)

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [March 27, 2024, 7:53pm UTC](https://discourse.julialang.org/t/discriminate-between-nan-and-inf-in-plots/112171/5 "2024-03-27T19:53:51Z")

</div>

Fyi, it is also possible to get this using Plots’ heatmap():

 ![Plots_gr_heatmap_colorbar_NaN_Inf](https://global.discourse-cdn.com/julialang/original/3X/f/8/f8e2649a2605b3a64808dbdcc2d8ba7a52064a33.png)

> **Plots' heatmap code**
>
> ```julia
> # (continued from code above)
> using Colors, ColorSchemes, Plots; gr()
> theme(:dark)
> 
> c0, c3 = colorant"white", colorant"red" # NaN's are labelled white, Inf's red
> m = 16
> cscheme1 = palette(ColorScheme([c0; ColorSchemes.viridis.colors; c3]), m+2)
> z1, z2 = extrema(x for x in rawData if isfinite(x))
> dz = (z2 - z1)/m
> Z0 = replace(rawData, NaN => z1 - dz, Inf => z2 + dz)
> 
> heatmap(Z0, c=cscheme1, lims=(0.5, N+0.5), ratio=1, dpi=600)
> annotate!(N+8, N+0.5, text("Inf", :red, 9))
> annotate!(N+8, 1, text("NaN", :white, 9))
> 
> ```

---

<div class="post-metadata">

### Author: ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)
#### Post date: [March 27, 2024, 9:15pm UTC](https://discourse.julialang.org/t/discriminate-between-nan-and-inf-in-plots/112171/6 "2024-03-27T21:15:16Z")

</div>

> [@rafael.guerra](#):
>
> it is also possible to get this using Plots’ heatmap():

what is the syntax?

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [March 27, 2024, 9:21pm UTC](https://discourse.julialang.org/t/discriminate-between-nan-and-inf-in-plots/112171/7 "2024-03-27T21:21:42Z")

</div>

The code has been posted below the plot.

---

<div class="post-metadata">

### Author: ![Sevi](https://avatars.discourse-cdn.com/v4/letter/s/c67d28/32.png) [@Sevi](https://discourse.julialang.org/u/Sevi)
#### Post date: [March 28, 2024, 6:13pm UTC](https://discourse.julialang.org/t/discriminate-between-nan-and-inf-in-plots/112171/8 "2024-03-28T18:13:05Z")

</div>

> [@rafael.guerra](#):
>
> Fyi, it is also possible to get this using Plots’ heatmap():

This is brilliant, thanks for the pointer!
