I have a three-dimensional surface, followed by a color bar created
surf = ax.plot_surface(xv, yv, zv, ...) cb = fig.colorbar(surf)
When it works, it looks like this:

The problem is that some of the values may be NaN, in which case the color bar cannot be generated, for example:

C:\Users\Sam\Anaconda\lib\site-packages\matplotlib\colorbar.py:581: RuntimeWarning: invalid value encountered in greater inrange = (ticks > -0.001) & (ticks < 1.001) C:\Users\Sam\Anaconda\lib\site-packages\matplotlib\colorbar.py:581: RuntimeWarning: invalid value encountered in less inrange = (ticks > -0.001) & (ticks < 1.001) C:\Users\Sam\Anaconda\lib\site-packages\matplotlib\colors.py:576: RuntimeWarning: invalid value encountered in less cbook._putmask(xa, xa < 0.0, -1)
I could try replacing NaN zv values with 0 ( zv[isnan(zv)] = 0 ), but then the resulting graph has vertical cliffs that confuse and obscure some of the functions, and it distorts the color bar.

I want the color bar to ignore NaN values. To do this, I could manually set the color bar (before amin(zv[~isnan(zv)]) and amax(zv[~isnan(zv)]) ) after creating it, but I don’t know how to do it.
Any suggestions on how to ignore NaN when calculating a color panel and when painting a 3d surface?
source share