在matplotlib中掩盖轮廓线图的一部分 [英] masking part of a contourf plot in matplotlib

查看:219
本文介绍了在matplotlib中掩盖轮廓线图的一部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用outlinef在matplotlib中生成一个填充的轮廓图。数据在图的底部附近呈锯齿状丢失。等高线图不仅在原始数据被掩盖的地方变成空白,而且在那些由于良好数据的邻域不足而无法对轮廓算法进行清晰插值的口袋中。



我知道如何扩展数据集以在这些口袋中产生合理的轮廓。但是,如果绘制扩展数据,到处都会得到轮廓填充。我想用黑色或白色掩盖原始数据丢失的区域。



在上一个线程中,我学会了如何绘制图像,方法是绘制第一个图像,然后用另一个遮盖坏区域的图像将其覆盖。类似物将是下面的代码片段,但不适用于轮廓...我无法得到bad_data显示来掩盖扩展的轮廓图。



谢谢,
Eli

 导入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)
#这是等高线图,使用extended_data使等高线合理地延伸
cs = plt.contourf(x,z,extended_data,levels = lev,cmap = cm.RdBu_r,norm = norml)
#现在试图掩盖它-但是imshow不会掩盖原始图,就像用另一张图像
bad_data = np.ma.masked_where(〜data.mask,data.mask,copy = True )
plt.imshow(bad_data,插值='nearest',Aspect ='auto',cmap = cm.gray)
plt.show()


解决方案

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

  import numpy as np 
import matplotlib.pyplot as plt
#生成一些带有np.nan值(缺失值)的数据
d = np.random.rand(10,10)
d [2,2],d [3,5] = np.nan,np.nan
#并且在您的情况下,您实际上也具有掩码值:
d = np.ma.array( d,mask = d < .2)
#现在,以上所有内容只是为了让我们获得一些缺少的数据(np.nan)和
#蒙版值

通过用轮廓线f绘制以上内容,

  plt.contourf( d)
plt.show()

我得到:





不会显示(空白)被屏蔽的值(d <.2)或np.nan值(d [2,2],d [3,5])!并且您希望matplotlib仅不显示掩码值。因此,我们可以这样做:

 #下面的行被插入程序替换为
#删除np.nan values
d [np.isnan(d)] = 1
#然后因为我们使用了被遮罩的数组,所以只有被遮罩的值仍会被遮罩
#但是被替换为np.nan的值如果执行轮廓线图
plt.contourf(d)
plt.show()
# p $ p>



在这种情况下,我不知道使用遮罩数组的速度有多快,但是无论如何,这就是我要做的。如果要使用其他颜色而不是空白点(白色),则需要为下面的轴的补丁着色,因为Contourf实际上不会在没有数据或遮罩数据的地方绘制任何东西:

 #使背景变为深灰色(在轮廓f之前调用)
plt.gca()。patch.set_color('。25')
plt.contourf(d)
plt.show()

得到:




I am trying to produce a filled contour plot in matplotlib using contourf. Data are missing in a jagged pattern near the bottom of the plot. The contour plot turns out blank not only where the original data are masked, but also in pockets where the contour algorithm cannot interpolate cleanly because there is an insufficient neighborhood of good data.

I know how extend the dataset to produce plausible contours in these pockets. However, if I plot the extended data I get contour fill everywhere. I would like to mask out the regions where the original data were missing in black or white.

On a previous thread I learned how to do this for an image by plotting the first image and then covering it up with another image that masks the bad areas. The analog would be the code snippet below but it doesn't work for a contour ... I can't get the bad_data imshow to cover up the extended contourf plot. Is it possible?

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

解决方案

Correct me if I'm wrong but as I understand you have this situation:

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

By plotting the above with contourf,

plt.contourf(d)
plt.show()

I get:

which doesn't show (blank) the masked values (d < .2) nor the np.nan values (d[2, 2], d[3, 5])! and you want for matplotlib to only not show the masked values. So we can do this:

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

I don't know how fast using the masked array is in this case, but anyways this is how I'd do it. If you want a different color instead of the blank spots (whit) you need to color the patch of the axes beneath because contourf actually doesn't plot anything where there is no data, or masked data:

# make the background dark gray (call this before the contourf)
plt.gca().patch.set_color('.25')
plt.contourf(d)
plt.show()

to get:

这篇关于在matplotlib中掩盖轮廓线图的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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