Set mask for matplotlib tricontourf

后端 未结 2 1105
轻奢々
轻奢々 2021-01-19 19:27

I have some numpy array containing data that I would visualize on a 2D grid. Some of the data is unphysical and I would like to mask this data. However, I could not figure o

相关标签:
2条回答
  • 2021-01-19 19:51

    Here comes the trick. I need to collect the indices of triangles (which are indices into z!), evaluate whether they are good or not and then accept only the triangles for that at least one corner is valid (reducing the dimension from (ntri, 3) to ntri

    triang = tr.Triangulation(x, y)
    mask = np.all(np.where(isbad[triang.triangles], True, False), axis=1)
    triang.set_mask(mask)
    colplt = mp.tricontourf(triang, z)
    mp.colorbar()
    

    Inspired by this link: http://matplotlib.org/examples/pylab_examples/tripcolor_demo.html

    Coloured contour plot with maksed unphysical region

    0 讨论(0)
  • 2021-01-19 19:51

    wsj's answer didn't work for me since it didn't remove certain masked points (I think when not all of the nodes where bad).

    This solution did:

    z[isbad] = numpy.NaN
    z = numpy.ma.masked_invalid(z)
    vmin, vmax = z.min(), z.max()
    z = z.filled(fill_value=-999)
    
    levels = numpy.linspace(vmin, vmax, n_points)
    plt.tricontourf(x, y, z, levels=levels)
    
    0 讨论(0)
提交回复
热议问题