设置matplotlib tricontourf的蒙版 [英] Set mask for matplotlib tricontourf

查看:323
本文介绍了设置matplotlib tricontourf的蒙版的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些numpy数组,其中包含要在2D网格上可视化的数据.一些数据是不真实的,我想掩盖这些数据.但是,我不知道如何正确设置tricontour的mask属性.我试过了:

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 out how to set the mask attribute of tricontour correctly. I tried:

import matplotlib.pyplot as mp
import numpy as np

with open('some_data.dat', 'r') as infile:
        x, y, z = np.loadtxt(infile, usecols=(0, 1, 2), unpack=True)
isbad = np.less(z, 1.4) | np.greater(z, 2.1)
mp.tricontourf(x, y, z, mask = isbad)

但是结果数字并没有被掩盖.我尝试在matplotlib中掩盖了轮廓线图的一部分,即

But the resulting figure is simply not masked. I tried masking part of a contourf plot in matplotlib, i.e.

z2 = np.ma.array(z, mask= isbad)
mp.tricontourf(x, y, z2)

这也不起作用.我想使用contourftricontourf instrad,因为我不想对我的数据进行网格化.

which did not work either. I want to use tricontourf instrad of contourf, because I do not want to grid my data.

z[isbad] = np.nan

在调用tricontourf时导致分段错误

results in a Segmentation fault when calling tricontourf

这是数字,红色是我要标记为不自然的颜色.

Here's the figure, the red colours are the ones I would like to mark as unphysical.

推荐答案

这就是诀窍.我需要收集三角形的索引(它们是z的索引!),评估它们是否好,然后仅接受至少一个角有效的三角形(将尺寸从(ntri,3)减小为ntri

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()

受此链接启发: http://matplotlib.org/examples/pylab_examples/tripcolor_demo. html

这篇关于设置matplotlib tricontourf的蒙版的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆