# Makie surface and NaN values

**URL:** <https://discourse.julialang.org/t/makie-surface-and-nan-values/65165>\
**Category:** Visualization\
**Tags:** makie\
**Created:** [July 23, 2021, 10:37am UTC](https://discourse.julialang.org/t/makie-surface-and-nan-values/65165 "2021-07-23T10:37:51Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![vettert](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vettert/32/30599_2.png) [@vettert](https://discourse.julialang.org/u/vettert)\
**Post date:** [July 23, 2021, 10:37am UTC](https://discourse.julialang.org/t/makie-surface-and-nan-values/65165/1 "2021-07-23T10:37:51Z")

</div>

I am running in some unexpected behavior with GLMakie, surface plots and matrices that contain NaN as a value.

My minimum working example below produces the following plots of three different 4 x 4 matrices with different elements set to NaN:

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

1. What causes the darker shading of cells adjacent to a NaN value?
2. The right most example is, frankly, somewhat weird to me (why is the surface dropping to a low value and becomes transparent like this?)

The behavior that I would like to have is that all tiles that have a NaN value as one of its vertices are simply not displayed. Is this doable with some tinkering of attributes of the surface?

Here the MWE code reproducing the above figures:

```julia
using GLMakie

x = 0.0:3.0
y = 0.0:3.0
Z1 = reshape([100*i+50*j for i in x for j in y], (length(x), length(y)))
Z2 = copy(Z1)
Z3 = copy(Z1)

Z2[1,1] = NaN
Z3[1:2, 1:2] .= NaN

f = Figure(resolution = (800, 800))
ax1 = Axis3(f[1, 1], aspect = (1, 1, 1))
ax2 = Axis3(f[1, 2], aspect = (1, 1, 1))
ax3 = Axis3(f[1, 3], aspect = (1, 1, 1))

surface!(ax1,x,y,Z1)
surface!(ax2,x,y,Z2)
surface!(ax3,x,y,Z3)

```

---

<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:** [July 23, 2021, 11:01am UTC](https://discourse.julialang.org/t/makie-surface-and-nan-values/65165/2 "2021-07-23T11:01:47Z")

</div>

Just for reference, linking a related issue [here](https://discourse.julialang.org/t/makie-adjusting-axis-scale-limits-in-3d/29231/6), for which did not get an answer.

---

<div class="post-metadata">

**Author:** ![vettert](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vettert/32/30599_2.png) [@vettert](https://discourse.julialang.org/u/vettert)\
**Post date:** [July 23, 2021, 11:28am UTC](https://discourse.julialang.org/t/makie-surface-and-nan-values/65165/3 "2021-07-23T11:28:35Z")

</div>

The behavior in your post is actually what I want. Smooth edges would be nice, I agree with you on that, but I would actually settle for what you already had!

The problem is, however, that your code produces a completely different looking image on the most current Makie version. It now shows similar issues as are shown above, but since your z-values are negative, the surface doesn’t go down like in my right hand side image, but up (towards 0).

edit: Included an image for reference.

 ![image](https://global.discourse-cdn.com/julialang/original/3X/9/d/9d7b7fc21cf9c93e4d8ccf2fbedbd6dd43da96ad.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:** [July 23, 2021, 12:34pm UTC](https://discourse.julialang.org/t/makie-surface-and-nan-values/65165/4 "2021-07-23T12:34:37Z")

</div>

Empirical tip for the latest Makie version: _replace NaN by Inf_

 ![Makie_surface_NaN_replaced_by_Inf](https://global.discourse-cdn.com/julialang/original/3X/e/9/e9074a6a00092343ae64d7403d28aad7e4e99f40.png)

_ **PS:** but result is still not shaved though…_

---

<div class="post-metadata">

**Author:** ![ffreyer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ffreyer/32/21569_2.png) [@ffreyer](https://discourse.julialang.org/u/ffreyer)\
**Post date:** [July 23, 2021, 12:40pm UTC](https://discourse.julialang.org/t/makie-surface-and-nan-values/65165/5 "2021-07-23T12:40:11Z")

</div>

The shader computes normals using the surrounding grid positions. If those have weird values the normals will have weird values and the lighting calculation will do weird things. Setting `shading = false` should get rid of the dark rectangles (or alternatively setting `diffuse=Vec3f0(0), specular = Vec3f0(0)`).

---

<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:** [July 23, 2021, 12:43pm UTC](https://discourse.julialang.org/t/makie-surface-and-nan-values/65165/6 "2021-07-23T12:43:17Z")

</div>

@ffreyer, fyi none of your solutions worked for me. The result is still as displayed by @vettert for the truncated paraboloid example.

---

<div class="post-metadata">

**Author:** ![ffreyer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ffreyer/32/21569_2.png) [@ffreyer](https://discourse.julialang.org/u/ffreyer)\
**Post date:** [July 23, 2021, 1:02pm UTC](https://discourse.julialang.org/t/makie-surface-and-nan-values/65165/7 "2021-07-23T13:02:56Z")

</div>

```julia
using GLMakie

x = 0.0:3.0
y = 0.0:3.0
Z1 = reshape([100*i+50*j for i in x for j in y], (length(x), length(y)))
Z2 = copy(Z1)
Z3 = copy(Z1)

Z2[1,1] = NaN
Z3[1:2, 1:2] .= NaN

f = Figure(resolution = (800, 800))
ax1 = Axis3(f[1, 1], aspect = (1, 1, 1))
ax2 = Axis3(f[1, 2], aspect = (1, 1, 1))
ax3 = Axis3(f[1, 3], aspect = (1, 1, 1))

surface!(ax1,x,y,Z1,shading=false)
surface!(ax2,x,y,Z2,shading=false)
surface!(ax3,x,y,Z3,shading=false)

```

This does get rid of the darker rectangles for me. Changing `shading` requires you to close the window beforehand though.

 ![Screenshot from 2021-07-23 14-57-36](https://global.discourse-cdn.com/julialang/original/3X/6/8/685145598ee5c84233ee5730a54fc8de97aa5c10.png)

---

<div class="post-metadata">

**Author:** ![ffreyer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ffreyer/32/21569_2.png) [@ffreyer](https://discourse.julialang.org/u/ffreyer)\
**Post date:** [July 23, 2021, 1:24pm UTC](https://discourse.julialang.org/t/makie-surface-and-nan-values/65165/8 "2021-07-23T13:24:12Z")

</div>

The reason for the surface to drop like it does is that the [shader](https://github.com/JuliaPlots/Makie.jl/blob/abf38b6d4e08fa8e7922a00c75e323ba6c179e72/GLMakie/assets/shader/surface.vert#L105) converts nan values to 0. You could work around that by passing the modified z-values as colors, i.e.

```julia
surface!(ax3,x,y,Z1,shading=false, color = Z3)

```

 ![Screenshot from 2021-07-23 15-17-51](https://global.discourse-cdn.com/julialang/original/3X/a/7/a7b71fc2c77b8db2c388278096c88221b2d668db.png)

I guess for a smooth cut we’d need a shader discarding pixels that are outside some value range or more generally a shape

---

<div class="post-metadata">

**Author:** ![vettert](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vettert/32/30599_2.png) [@vettert](https://discourse.julialang.org/u/vettert)\
**Post date:** [July 23, 2021, 1:29pm UTC](https://discourse.julialang.org/t/makie-surface-and-nan-values/65165/9 "2021-07-23T13:29:12Z")

</div>

Thanks to both of you @ffreyer and @rafael.guerra. Here is the full code that gets close to what I wanted.

```julia
using GLMakie

x = 0.0:3.0
y = 0.0:3.0
Z1 = reshape([100*i+50*j for i in x for j in y], (length(x), length(y)))
Z2 = copy(Z1)
Z3 = copy(Z1)
Z4 = copy(Z1)
Z5 = copy(Z1)

Z2[1,1] = NaN
Z3[1:2, 1:2] .= NaN
Z4[1,1] = Inf
Z5[1:2, 1:2] .= Inf

f = Figure(resolution = (800, 800))
ax1 = Axis3(f[1, 1], aspect = (1, 1, 1))
ax2 = Axis3(f[1, 2], aspect = (1, 1, 1))
ax3 = Axis3(f[1, 3], aspect = (1, 1, 1))
ax4 = Axis3(f[2, 2], aspect = (1, 1, 1))
ax5 = Axis3(f[2, 3], aspect = (1, 1, 1))

s1 = surface!(ax1,x,y,Z1)
s2 = surface!(ax2,x,y,Z2)
s3 = surface!(ax3,x,y,Z3)
s4 = surface!(ax4,x,y,Z4, shading = false)
s5 = surface!(ax5,x,y,Z5, shading = false)

display(f)

```

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

The smooth cut thing is of course nice to have, but in my use example, I am fine with just making the grid finer and minimizing how well one sees the rugged edges (the visualization is not the computationally expensive part of the process 🙂 )

---

<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:** [July 23, 2021, 1:29pm UTC](https://discourse.julialang.org/t/makie-surface-and-nan-values/65165/10 "2021-07-23T13:29:22Z")

</div>

![Makie_NaN_replaced_by_Inf](https://global.discourse-cdn.com/julialang/original/3X/0/6/06ee5631cd80de4b04ebc69eced44245972159e6.png)

NB: using `Inf` workaround as indicated further above.
