我试图用轮廓线在Matplotlib中生成一个填充的轮廓图。在绘图底部附近的锯齿状图案中缺少数据。不仅在原始数据被屏蔽的情况下,轮廓图是空白的,而且在由于良好数据的邻域不足而轮廓算法无法精确插值的情况下,轮廓图也是空白的。
我知道如何扩展数据集以在这些口袋中生成合理的轮廓。然而,如果我绘制扩展数据,我会得到所有的轮廓填充。我想把原始数据丢失的区域用黑色或白色屏蔽掉。
在上一个线程中,我学习了如何通过绘制第一个图像,然后用另一个掩盖坏区域的图像覆盖它来为图像执行此操作。下面的代码段是模拟的,但它不适用于轮廓…我不能用坏数据来掩盖扩展的轮廓图。有可能吗?
谢谢,
ELI

import matplotlib.pyplot as plt
lev = [0.0,0.1,0.2,0.5,1.0,2.0,4.0,8.0,16.0,32.0]
norml = colors.BoundaryNorm(lev, 256)
# this is the contour plot, using extended_data so that the contours are plausibly extended
cs = plt.contourf(x,z,extended_data,levels = lev, cmap = cm.RdBu_r,norm = norml)
# now the attempt to cover it up -- but imshow will not cover up the original plot as it will with another image
bad_data = np.ma.masked_where(~data.mask, data.mask, copy=True)
plt.imshow(bad_data, interpolation='nearest', aspect = 'auto', cmap=cm.gray)
plt.show()

最佳答案

如果我错了,请纠正我,但据我所知,你有这种情况:

import numpy as np
import matplotlib.pyplot as plt
# generate some data with np.nan values (the missing values)
d = np.random.rand(10, 10)
d[2, 2], d[3, 5] = np.nan, np.nan
# and in your case you actually have masked values too:
d = np.ma.array(d, mask=d < .2)
# now all of the above is just for us to get some data with missing (np.nan) and
# masked values

通过绘制上面的曲线图,
plt.contourf(d)
plt.show()

我得到:
它不会显示(空白)屏蔽值(d
# the following line is replaced by your interpolation routine for
# removing np.nan values
d[np.isnan(d)] = 1
# then because we use the masked array only the masked values will still be masked
# but the np.nan values which were replaced through the interpolation algorithm
# will show up if we do the contourf plot
plt.contourf(d)
plt.show()

我不知道在这种情况下使用蒙面数组的速度有多快,但无论如何,我都会这样做。如果你想要一个不同的颜色而不是空白点(WHIT),你需要把下面的轴的颜色涂上,因为轮廓线实际上没有绘制任何没有数据或掩蔽数据的东西:
# make the background dark gray (call this before the contourf)
plt.gca().patch.set_color('.25')
plt.contourf(d)
plt.show()

得到:

08-20 04:47