# GLMakie: masking values when plotting images

**URL:** <https://discourse.julialang.org/t/glmakie-masking-values-when-plotting-images/66508>\
**Category:** Visualization\
**Tags:** question, plotting\
**Created:** [August 16, 2021, 7:25pm UTC](https://discourse.julialang.org/t/glmakie-masking-values-when-plotting-images/66508 "2021-08-16T19:25:57Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![mariusd](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mariusd/32/23231_2.png) [@mariusd](https://discourse.julialang.org/u/mariusd)\
**Post date:** [August 16, 2021, 7:25pm UTC](https://discourse.julialang.org/t/glmakie-masking-values-when-plotting-images/66508/1 "2021-08-16T19:25:57Z")

</div>

I’m using GLMakie to display 2 images.  
GLMakie.image(GLMakie.rotr90(image\_data))  
GLMakie.image!(GLMakie.rotr90(some\_extra\_data), colormap=(“viridis”, 0.5))

The second image have a transparency of 0.5. See here: [GLMakie: how to plot two images with transparency?](https://discourse.julialang.org/t/glmakie-how-to-plot-two-images-with-transparency/66492)  
some\_extra\_data contains good values and also invalid values. Invalid values are equal to zero.

What I want to achieve is to mask the invalid values in some\_extra\_data such that they are not displayed at all. Only the valid values shall be displayed. In python this can be achieved via matplotlib and numpy.ma.masked\_where.  
So, how to do this masking with GLMakie?

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)\
**Post date:** [August 16, 2021, 8:48pm UTC](https://discourse.julialang.org/t/glmakie-masking-values-when-plotting-images/66508/2 "2021-08-16T20:48:39Z")

</div>

NaN values are transparent by default, so you can replace the invalid numbers with NaNs

---

<div class="post-metadata">

**Author:** ![mariusd](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mariusd/32/23231_2.png) [@mariusd](https://discourse.julialang.org/u/mariusd)\
**Post date:** [August 17, 2021, 4:52am UTC](https://discourse.julialang.org/t/glmakie-masking-values-when-plotting-images/66508/3 "2021-08-17T04:52:07Z")

</div>

Replacing with NaN doesn’t work, I have tried the following:

some\_with\_nan = [e == 0 ? NaN : e for e in some\_extra\_data]  
GLMakie.image(GLMakie.rotr90(image\_data))  
GLMakie.image!(GLMakie.rotr90(some\_with\_nan ), colormap=(“viridis”, 0.5))

---

<div class="post-metadata">

**Author:** ![mariusd](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mariusd/32/23231_2.png) [@mariusd](https://discourse.julialang.org/u/mariusd)\
**Post date:** [August 18, 2021, 1:07pm UTC](https://discourse.julialang.org/t/glmakie-masking-values-when-plotting-images/66508/4 "2021-08-18T13:07:38Z")

</div>

I have tried to use heatmap for “some\_with\_nan”. And there indeed NaN values are transparent when I only display the heatmap. But the problem is that I can no longer display heatmap “some\_with\_nan” over “image\_data”.

---

<div class="post-metadata">

**Author:** ![mariusd](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mariusd/32/23231_2.png) [@mariusd](https://discourse.julialang.org/u/mariusd)\
**Post date:** [August 18, 2021, 1:08pm UTC](https://discourse.julialang.org/t/glmakie-masking-values-when-plotting-images/66508/5 "2021-08-18T13:08:43Z")

</div>

It seems to me that nan\_color option in GLMakie.image is not implemented yet. I have opened an issue: [https://github.com/JuliaPlots/Makie.jl/issues/1230](https://github.com/JuliaPlots/Makie.jl/issues/1230)

---

<div class="post-metadata">

**Author:** ![mariusd](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mariusd/32/23231_2.png) [@mariusd](https://discourse.julialang.org/u/mariusd)\
**Post date:** [August 18, 2021, 2:23pm UTC](https://discourse.julialang.org/t/glmakie-masking-values-when-plotting-images/66508/6 "2021-08-18T14:23:57Z")

</div>

I have managed to overlap “image” and “heatmap” by setting a transparency value to the “image” as well.  
image(rotr90(image\_data), colormap=(:gray1, 0.5))  
image!(rotr90(some\_with\_nan ), colormap=(:viridis, 0.5))

This is how the output looks like, currently. To be improved.

 ![image](https://global.discourse-cdn.com/julialang/original/3X/e/3/e369204f13fdea8ea104e1a4afc07acd9afe7711.jpeg)

---

<div class="post-metadata">

**Author:** ![mariusd](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mariusd/32/23231_2.png) [@mariusd](https://discourse.julialang.org/u/mariusd)\
**Post date:** [August 18, 2021, 3:06pm UTC](https://discourse.julialang.org/t/glmakie-masking-values-when-plotting-images/66508/7 "2021-08-18T15:06:55Z")

</div>

With some extra tweaks, I have reached the desired output. Instead of GLMakie.image I have used GLMakie.heatmap also for the image data, in order to get transparency for the image as well, see bellow.

```julia
some_extra_data_nan = [e == 0 ? NaN : e for e in some_extra_data]

img_data2 = img_data/256
img_data2[some_extra_data .!= 0] .= NaN

img_data3 = imr_wide.img_data/256.0
img_data3[some_extra_data .== 0] .= NaN

# where extra data is invalid, show the image, no transparency
heatmap(rotr90(img_data2), colormap=:gray1)
# where the extra data is valid, show the image with 0.5 transparency
heatmap!(rotr90(img_data3), colormap=(:gray1, 0.5)) 
# show the extra data
heatmap!(rotr90(some_extra_data_nan), colormap=(:viridis, 0.5)) 

```

Here is the output:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/9/8/983934f62c37b38007d6670408beb0b19f71b7fc.jpeg)
