How to not plot value zero on grid?

I have a grdimage() I want to plot on my PyGMT.Figure() with the cmap viridis, and the value zero corresponds to a yellowish color. However, the zeros on my grid represent points with no information. How can I set it so the value zero isn’t plotted, or is plotted white?

I’m very confused by it because I already managed to do it before, but can’t seem to replicate it now. I think pygmt.makecpt() doesn’t completely overwrite previous color palette tables you make, and a certain combination of colormaps implemented one after the other did the job. So if anyone knows if that’s the case, that color tables aren’t overwritten when you call pygmt.makecpt() again, and knows how to set it to be the case I would also appreciate it.

It could also be the case that an option on fig.grdimage() can let me not plot the zeros, but I haven’t found it either.

Thank you very much!

Edited for clarification :slight_smile:

1 Like

Please, respect the tags/topics.
This is the channel for PyGMT questions, which isn’t the case of this one.

I apologize but I fail to see how this isn’t a pygmt question. I’m asking about a pygmt figure and the function pygmt.makecpt() in particular.

So sorry of me … but I still can’t see any reference to PyGMT.

not pygmt.makecpt()

Edited for clarification! @Joaquim

1 Like

Hello @Feva67,

maybe the nan_transparent parameter of pygmt.Figure.grdimage (pygmt.Figure.grdimage — PyGMT) is useful for you? It is possible to state the z-value which is plotted as transparent (Default is NaN). Therefore, you have to use +zvalue. This is currently only documented in the upstream GMT documentation at grdimage — GMT 6.6.0 documentation.

Below, you find a code example, which may serve as an orientation. However, for a more detailed help, please share your code with us. This will make it significantly easier for others to help you :slightly_smiling_face:.

Example code:

import pygmt

# Load sample grid dataset
grid_relief = pygmt.datasets.load_earth_relief()

# Create figure object
fig = pygmt.Figure()

    region=[0, 360, -90, 90],  # lon_min, lon_max, lat_min, lat_max
    projection="H15c",  # Hammer projection with a width of 15 centimeters
# Set up colormap
    series=[-8500, 6000, 100],  # min, max], step
# Plot colored grid
    # make z-values = 0 transparent
    # change the value after "+z" to the z-value you want appear transparent

fig.colorbar(frame="+lelevation in m")
# fig.savefig("grdimage_nan_transparent.png")

Output figure:


Thank you so very much @yvonnefroehlich! This is exactly what I needed.

I’m left to wonder about the fact of CPTs not being overwritten, maybe I’ll try to make a simple example and post it later.