# How to set the colors of NaN with grdview?

**URL:** <https://forum.generic-mapping-tools.org/t/how-to-set-the-colors-of-nan-with-grdview/4608>\
**Category:** Q&A\
**Created:** [January 16, 2024, 1:44pm UTC](https://forum.generic-mapping-tools.org/t/how-to-set-the-colors-of-nan-with-grdview/4608 "2024-01-16T13:44:57Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Esteban82](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/esteban82/32/54_2.png) [@Esteban82](https://forum.generic-mapping-tools.org/u/Esteban82)\
**Post date:** [January 16, 2024, 1:44pm UTC](https://forum.generic-mapping-tools.org/t/how-to-set-the-colors-of-nan-with-grdview/4608/1 "2024-01-16T13:44:57Z")

</div>

I think that is not possible to set the color of the NaN values with grdview.

I set the NaN to red. When I plot my grid with `grdimage` (top image) I got red but I can’t with `grdview`. Am I missing something? Is this a bug?

 ![q](https://canada1.discourse-cdn.com/flex047/uploads/gmt/original/2X/0/02c1a2fd3d0415650ede2c184dd74518548581d2.jpeg)

## Full script

```
DOMAIN_volc=-R-61.7713244711/-61.5554875289/15.9359105289/16.1517474711
gmt grdcut @earth_relief_01s ${DOMAIN_volc} -Gisland.nc
gmt grdclip island.nc -Gtopo_island.nc -Sb0/NaN -Sr0/NAN
max_alt=$(gmt grdinfo topo_island.nc -Mf | grep "v_max" | awk '{print $5}')

gmt begin q png
gmt makecpt -Coleron -T0/${max_alt} -M --COLOR_NAN=red
gmt grdview topo_island.nc -C -JM10c -JZ1c -Qc -I+d -Bafg -p135/20
gmt grdimage topo_island.nc -C -Yh
gmt end

```

---

<div class="post-metadata">

**Author:** ![yvonnefroehlich](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/yvonnefroehlich/32/5064_2.png) [@yvonnefroehlich](https://forum.generic-mapping-tools.org/u/yvonnefroehlich)\
**Post date:** [January 16, 2024, 9:07pm UTC](https://forum.generic-mapping-tools.org/t/how-to-set-the-colors-of-nan-with-grdview/4608/2 "2024-01-16T21:07:26Z")

</div>

Hello @Esteban82,

just one spontaneous idea: For `grdview`, the elevation is not only represented by the color-coding but also along the vertical or z-axis. So, there is the question at which position of the z-axis NaN values should be plotted, as there is no NaN value on the z-axis. Probably GMT does not know where to plot the NaN part of the data (vertically) and simply skips it.

---

<div class="post-metadata">

**Author:** ![yvonnefroehlich](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/yvonnefroehlich/32/5064_2.png) [@yvonnefroehlich](https://forum.generic-mapping-tools.org/u/yvonnefroehlich)\
**Post date:** [January 16, 2024, 9:16pm UTC](https://forum.generic-mapping-tools.org/t/how-to-set-the-colors-of-nan-with-grdview/4608/3 "2024-01-16T21:16:06Z")

</div>

For this case, maybe using the background can serve as a work around:  
(_Edit 2024/01/17_: Correction corresponding to the post by @Esteban82 below)

```auto
DOMAIN_volc=-R-61.7713244711/-61.5554875289/15.9359105289/16.1517474711
gmt grdcut @earth_relief_01s ${DOMAIN_volc} -Gisland.nc
gmt grdclip island.nc -Gtopo_island.nc -Sb0/-1 -Sr0/-1

gmt begin q png
gmt makecpt -Coleron -T0/2000 -M --COLOR_BACKGROUND=red
gmt grdview topo_island.nc -C -JM10c -JZ1c -Qc -I+d -Bafg -p135/20
gmt grdimage topo_island.nc -C -Yh
gmt end show

```

_Output figure_:

 ![q](https://canada1.discourse-cdn.com/flex047/uploads/gmt/original/2X/9/96edd649542d51fa1162b103cdeb0875cc240ef6.jpeg)

---

<div class="post-metadata">

**Author:** ![Esteban82](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/esteban82/32/54_2.png) [@Esteban82](https://forum.generic-mapping-tools.org/u/Esteban82)\
**Post date:** [January 16, 2024, 9:29pm UTC](https://forum.generic-mapping-tools.org/t/how-to-set-the-colors-of-nan-with-grdview/4608/4 "2024-01-16T21:29:20Z")

</div>

Sure, yes, that must be it. So I have no choice but to apply your solution.

> [@yvonnefroehlich](#):
>
> `gmt grdclip island.nc -Gtopo_island.nc`

BTW, is this command complete? Or do you add something like `-Sb0/-0.1`?

---

<div class="post-metadata">

**Author:** ![yvonnefroehlich](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/yvonnefroehlich/32/5064_2.png) [@yvonnefroehlich](https://forum.generic-mapping-tools.org/u/yvonnefroehlich)\
**Post date:** [January 16, 2024, 9:43pm UTC](https://forum.generic-mapping-tools.org/t/how-to-set-the-colors-of-nan-with-grdview/4608/5 "2024-01-16T21:43:53Z")

</div>

In principle, it should be working. But if you also want 0 to be in red, then you need something like `-Sb0/-0.1`, as the colormap starts at zero (`T0/2000`).

---

<div class="post-metadata">

**Author:** ![Esteban82](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/esteban82/32/54_2.png) [@Esteban82](https://forum.generic-mapping-tools.org/u/Esteban82)\
**Post date:** [January 16, 2024, 10:01pm UTC](https://forum.generic-mapping-tools.org/t/how-to-set-the-colors-of-nan-with-grdview/4608/6 "2024-01-16T22:01:05Z")

</div>

I think that you must add something like `-Sb0/-0.01 -Sr0/-0.01`

Otherwise I got this messagge:

`grdclip [ERROR]: Option -S: Must specify at least one of -Sa, -Sb, -Si, -Sr`

---

<div class="post-metadata">

**Author:** ![yvonnefroehlich](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/yvonnefroehlich/32/5064_2.png) [@yvonnefroehlich](https://forum.generic-mapping-tools.org/u/yvonnefroehlich)\
**Post date:** [January 17, 2024, 7:10am UTC](https://forum.generic-mapping-tools.org/t/how-to-set-the-colors-of-nan-with-grdview/4608/7 "2024-01-17T07:10:11Z")

</div>

Ah, yes your are right! Must have missed this part somehow when copying the code 🙁 (locally I have `gmt grdclip island.nc -Gtopo_island.nc -Sb0/-1 -Sr0/-1`).  
Updated my post above to avoid further confusions.

---

<div class="post-metadata">

**Author:** ![pwessel](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/pwessel/32/30_2.png) [@pwessel](https://forum.generic-mapping-tools.org/u/pwessel)\
**Post date:** [January 17, 2024, 9:45am UTC](https://forum.generic-mapping-tools.org/t/how-to-set-the-colors-of-nan-with-grdview/4608/8 "2024-01-17T09:45:46Z")

</div>

The issue of using NaN colors in **grdview** is probably something that could be improved and revisited. Right now, I get read NaNs only with **-Qs** (surface) and only without **-p**. This is due to various if-test checks. For **-Q** we rasterize the grid to an image and set the image pixels to whatever color (but not the NaN color).

I suspect we will need a modifier or option to insist that we use the NaN color, whatever it is, on the NaN pixels or NaN nodes or NaN patches. Perhaps the simplest syntax might be a new modifier **+n** to **-Q** which says "paint any NaN node or pixel with the NaN color in the CPT (requires **-C** )?

---

<div class="post-metadata">

**Author:** ![Esteban82](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/esteban82/32/54_2.png) [@Esteban82](https://forum.generic-mapping-tools.org/u/Esteban82)\
**Post date:** [January 17, 2024, 12:47pm UTC](https://forum.generic-mapping-tools.org/t/how-to-set-the-colors-of-nan-with-grdview/4608/9 "2024-01-17T12:47:15Z")

</div>

I think that this is the best way to do it.

1. Create two grids: one with NaN and another with 0 for the bathymetry.
2. I made a 2D map with the NaN grid.
3. Finally, I drape the map from step 2 over the relief with 0.

 ![q_drape](https://canada1.discourse-cdn.com/flex047/uploads/gmt/original/2X/d/d53b26ed33125c91aeb0a9eaf05a3d1bdef53125.jpeg)

Full script:

```
DOMAIN_volc=-R-61.7713244711/-61.5554875289/15.9359105289/16.1517474711
gmt grdcut @earth_relief_01s ${DOMAIN_volc} -Gisland.nc
gmt grdclip island.nc -Gisland_NaN.nc -Sb0/NaN -Sr0/NaN
gmt grdclip island.nc -Gtopo_island.nc -Sb0/0

gmt begin q png
gmt makecpt -Coleron -T0/2000 -M --COLOR_NAN=lightblue
gmt grdimage island_NaN.nc -C
gmt end

gmt grdview topo_island.nc -Gq.png -Bf -JM10c -JZ1c -Qc -p135/20 -png q_drape

```

---

<div class="post-metadata">

**Author:** ![Joaquim](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/joaquim/32/16_2.png) [@Joaquim](https://forum.generic-mapping-tools.org/u/Joaquim)\
**Post date:** [January 17, 2024, 3:33pm UTC](https://forum.generic-mapping-tools.org/t/how-to-set-the-colors-of-nan-with-grdview/4608/10 "2024-01-17T15:33:34Z")

</div>

Seeing a _duplicate the grid_ makes ruffle my hair. Looks like the right thing here is to make a NaN/no-NaN mask and add it as an alpha layer to the image. Then the transparency and transparent color machinery should apply. But we didn’t test the alpha machinery to `grdview` before.

---

<div class="post-metadata">

**Author:** ![pwessel](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/pwessel/32/30_2.png) [@pwessel](https://forum.generic-mapping-tools.org/u/pwessel)\
**Post date:** [January 17, 2024, 3:45pm UTC](https://forum.generic-mapping-tools.org/t/how-to-set-the-colors-of-nan-with-grdview/4608/11 "2024-01-17T15:45:14Z")

</div>

Yes, **grdview** needs some love and care. Just posted a PR to do some standardization on syntax with **grdimage** at least.
