# Distinct color for missing values in colorbar

**URL:** <https://discourse.julialang.org/t/distinct-color-for-missing-values-in-colorbar/71927>\
**Category:** Visualization\
**Tags:** plots, missing-values\
**Created:** [November 23, 2021, 3:46am UTC](https://discourse.julialang.org/t/distinct-color-for-missing-values-in-colorbar/71927 "2021-11-23T03:46:02Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![adigitoleo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/adigitoleo/32/27478_2.png) [@adigitoleo](https://discourse.julialang.org/u/adigitoleo)\
**Post date:** [November 23, 2021, 3:46am UTC](https://discourse.julialang.org/t/distinct-color-for-missing-values-in-colorbar/71927/1 "2021-11-23T03:46:02Z")

</div>

I am wondering how to handle `missing` values in a colorbar.

For my specific use case, I want to create a scatter plot where the data points are colored according to a third vector of values which may contain `missing`. Before opening an issue, I am first checking if there is already a way to achieve this, that I have, for lack of better word, missed.

From what I can see, the only way I could do it right now is to loop over `x`, `y` and `marker_z` simultaneously, and exclude values from all arrays for indices that satisfy `ismissing(marker_z[i])`. This seems a bit silly, since up to the visualisation stage, `missing` values were propagated through the processing without any issue.

There is one solution that I don’t think is curently possible. First, I relpace missing values with a number I know to be outside the valid cmap range, say:

```julia
cmap = replace(x -> ismissing(x) ? 0 : x, cmap_vector)

```

Then, I would need to combine two colorbars: at the bottom there is a single color for say (0, 10) which is a small range for which the colormap value is meaningless, and above that I have a gradient map as usual.

Alternatively, perhaps this requires a new keyword argument to `cgrad`, something like `missingcolor`? Then a small box could be drawn below the colorbar filled with `missingcolor` and with an appropriate label… Maybe it’s too complicated.

I’m not sure what I think about this, and I wonder if anyone else has some ideas.

Leon

---

<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:** [November 24, 2021, 12:40pm UTC](https://discourse.julialang.org/t/distinct-color-for-missing-values-in-colorbar/71927/2 "2021-11-24T12:40:10Z")

</div>

For `Plots.jl` you could use some workaround like here below:

```julia
using Plots; gr()
Z = Vector{Union{Float64, Missing}}(undef, 11)
X = rand(11); Y = rand(11); Z .= -5:5
Z[rand(1:10,3)] .= missing

using Colors, ColorSchemes
c1 = colorant"red"
c2 = colorant"green"
c0 = colorant"white" # missings will be labelled white
n = 10
cscheme1 = ColorScheme([c0; range(c1, stop=c2, length=n)])
z1, z2 = extrema(skipmissing(Z))
dz = (z2 - z1)/n
Z2 = replace(Z, missing => z1 - dz)
ϵ = 1e-3 # fudge factor
Plots.scatter(X,Y, marker_z=Z2, ms=6, msw=0.3, label=false, c=palette(cscheme1,n+1), clims=(z1-(1+ϵ)*dz,z2+ϵ*dz))

```

 ![Plots_gr_scatter_missing_NaN_color](https://global.discourse-cdn.com/julialang/original/3X/3/5/35a44a8c262c282bb493ceeb767003ae3dc2ecb6.png)

Please double-check the details.

---

<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:** [November 24, 2021, 2:53pm UTC](https://discourse.julialang.org/t/distinct-color-for-missing-values-in-colorbar/71927/3 "2021-11-24T14:53:04Z")

</div>

It is easier to plot first the non-missing and then the missing, but then the missings’ color does not make it to the color scale as in workaround above:

```julia
using Plots; gr()
Z = Vector{Union{Float64, Missing}}(undef, 11)
X = rand(11); Y = rand(11); Z .= -5:5
Z[rand(1:11,3)] .= missing
ix = ismissing.(Z)
Plots.scatter(X[.!ix],Y[.!ix],marker_z=Z[.!ix], label=false)
Plots.scatter!(X[ix],Y[ix], mc="white", label=false)

```

---

<div class="post-metadata">

**Author:** ![adigitoleo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/adigitoleo/32/27478_2.png) [@adigitoleo](https://discourse.julialang.org/u/adigitoleo)\
**Post date:** [November 24, 2021, 11:36pm UTC](https://discourse.julialang.org/t/distinct-color-for-missing-values-in-colorbar/71927/4 "2021-11-24T23:36:38Z")

</div>

Thanks! These are both good options, I didn’t realise I could use logical indexing like that for the second case, pretty cool. I’ll mark the first option as a solution since it adresses the colorbar as well.

---

<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:** [November 24, 2021, 11:46pm UTC](https://discourse.julialang.org/t/distinct-color-for-missing-values-in-colorbar/71927/5 "2021-11-24T23:46:01Z")

</div>

Just in case, some scatter plots may look better using the `theme(:dark)`:

 ![Plots_gr_scatter_missing_NaN_color](https://global.discourse-cdn.com/julialang/original/3X/a/3/a3234a0ac8f3499f5cb70cdae8b8b3e22600363d.png)
