pcolormesh with missing values?

前端 未结 3 971
[愿得一人]
[愿得一人] 2020-12-05 11:04

I have 3 1-D ndarrays: x, y, z

and the following code:

import numpy as np
import matplotlib.pyplot as plt
import scipy.interpolate as spinterp

## de         


        
相关标签:
3条回答
  • 2020-12-05 11:26

    Got it. This seems round-about, but this was the solution:

    import numpy.ma as ma
    
    Zm = ma.masked_where(np.isnan(Z),Z)
    plt.pcolormesh(X,Y,Zm.T)
    

    If the Z matrix contains nan's, it has to be a masked array for pcolormesh, which has to be created with ma.masked_where, or, alternatively,

    Zm = ma.array(Z,mask=np.isnan(Z))
    
    0 讨论(0)
  • 2020-12-05 11:32

    A slight improvement on the chosen answer

    import numpy.ma as ma
    Zm = ma.masked_invalid(Z)
    plt.pcolormesh(X, Y, Zm.T)
    

    masked_invalid masks all NaN values, thereby saving the need to specify

    mask = np.isnan(Z)
    
    0 讨论(0)
  • 2020-12-05 11:35

    Note that the explicit masking is no longer necessary in matplotlib master as arrays are now masked automatically internally. Will be incorporated into matplotlib >2.1. See my merged pull request https://github.com/matplotlib/matplotlib/pull/5451

    So now it's as simple as

    plt.pcolormesh(X,Y,Z.T)
    
    0 讨论(0)
提交回复
热议问题