在轮廓中绘制NaNs区域的边界

时间:2018-11-19 15:33:38

标签: python matplotlib nan contour

我正在尝试使用NaN绘制轮廓数据(没有解决方案)。我想用黑线表示NaN的边框。到目前为止,我只找到了如何填充整个NaN区域(hatch a NaN region in a contourplot in matplotlib),但是我只想要轮廓。

fig, ax = plt.subplots()

d = np.random.rand(10, 10)
d[2, 2], d[3, 5] = np.nan, np.nan

plt.contour(d)
plt.show()

我得到:

enter image description here

我想要:

enter image description here

1 个答案:

答案 0 :(得分:3)

您可以绘制被遮罩区域的另一个轮廓。为此,可以使用numpy.ma数组屏蔽数据。然后使用其遮罩在接近(但不完全)零的水平上绘制另一个轮廓。

import numpy as np
import matplotlib.pyplot as plt

fig, ax = plt.subplots()

d = np.random.rand(10, 10)

mask = np.zeros(d.shape, dtype=bool)
mask[2, 2], mask[3, 5] = 1, 1

masked_d = np.ma.array(d, mask=mask)

plt.contour(masked_d)

plt.contour(mask, [0.01], colors="k", linewidths=3)

plt.show()

enter image description here

相关问题