Contourf和NaNs(如何使白色区域透明) [英] Contourf and NaNs (how to make white regions transparent)

查看:298
本文介绍了Contourf和NaNs(如何使白色区域透明)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作 contourf 图,但数据数组的某些区域有NaN(仅在数据矩阵中,x和y网格网格矩阵已满) 。我希望这些NaN是透明的,它们用于矩形边界上的NaN。但是,数据矩阵内的连续NaN区域是白色而不是透明的。以下是一个例子:

I'm trying to make a contourf plot but certain areas of the data array have NaNs (only in the data matrix, the x and y meshgrid matrices are full). I'd like these NaNs to be transparent, and they are for NaNs on the boundary of the rectangle. But, contiguous NaN regions inside the data matrix are white instead of transparent. Below is an example:

代码:

[X Y] = meshgrid(10:50);
Z = X.*Y;
Z(10:30,10:30) = NaN;
figure
imshow(uint8(repmat(1:4:240,[60,1,3])));
hold on;
contourf(X,Y,Z);
colormap jet;

输出:

提示:

使用以下代码附加上述代码:

Appending the above code with:

% Find Face
set(findobj(h,'FaceColor',[1 1 1]),'FaceAlpha',0))

会找到白色补丁对象并将其设置为透明。不幸的是,下面的补丁已满:

Will find the white patch object and set it transparent. Unfortunately, the patch underneath is full:

更新:获取 NaN 区域并叠加背景图片结果:

Update: Taking the NaN region and superimposing the background image results in:

正如你所看到的,它并没有覆盖整个图像。如果我使用 imdilate 它会消除白色区域,但同时也会破坏黑色边框以及一些数据。

As you can see it doesnt cover the entire image. If I use imdilate it gets rid of the white area but at the same time also destroys the black border as well as a little bit of the data.

推荐答案

在H. Muster的答案的基础上,我通过分割数据人为地创建了乐队(你可以用一个简单的函数来选择乐队,或者在假人物上用轮廓创建它们,返回它们并重新使用它们。)

Building on H. Muster's answer, I've artificially created the bands by segmenting the data (you could use a simple function to choose the bands, or create them with contour on a dummy figure, return them, and re-use them).

[X Y] = meshgrid(10:0.1:50);
Z = X.*Y;
Z(100:300,100:300) = NaN;
figure
hold on;
h = pcolor(X,Y,round(Z/500)*500);
set(h,'Edgecolor',  'interp');
colormap jet;
set(gca, 'XLim', [0 60], 'YLim', [0 60]);

恐怕我没有包含 imread <的工具箱/ code>所以无法显示基础渐变,但我认为这样可行。我必须将分辨率提高10倍才能获得相当平滑的图像。

I'm afraid I don't have the toolbox that includes imread so can't show the underlying gradient, but I think this would work. I've had to increase the resolution by a factor of 10 to get a reasonably smooth image.

这篇关于Contourf和NaNs(如何使白色区域透明)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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