【问题标题】:Plot borders of NaNs region in contour在等高线中绘制 NaNs 区域的边界
【发布时间】:2019-04-22 00:10:10
【问题描述】:
我正在尝试用 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()
我明白了:
我想要:
【问题讨论】:
标签:
python
matplotlib
nan
contour
【解决方案1】:
您可以绘制被遮蔽区域的另一个轮廓。为此,可以使用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()