Matlab中的pcolor阴影和遮罩处理

3

实际上有两个问题,但是我感觉它们有重叠的部分,所以我会在一个地方问(如果可以的话)。

我使用以下代码在matlab中创建了一个pcolor:

p = pcolor(Lon', Lat', data);

但现在我想添加一些信息。我有一个名为mask的矩阵,它与数据具有相同的维度,但由1和0填充。当它包含1时,我想保留pcolor中的像素;当它包含0时,删除它(不仅仅是将值变为零,因为这不是我的色彩地图中表示为白色像素的)。
其次,我有第二个名为“stipple”的矩阵,它也包含0和1。现在,我想用点刻画效果覆盖任何由1表示的位置。
这样做的目的是创建像这样的图像: http://www.ipcc.ch/publications_and_data/ar4/wg1/en/figure-spm-7.html 其中平均值被涂上,但存在过多争议的区域被涂成白色,而存在大量协议的区域则被点刻画。
提前感谢!
2个回答

4
我会采用使用遮罩实现透明度的方法。对于pcolor,这可能会稍微复杂一些,因为在创建图时它不允许您直接设置属性,但是您可以获取到它的句柄并以这种方式进行设置。
要叠加点画,您只需要使用'hold on'以便pcolor图不会消失,并使用'plot'在您想要的位置绘制点。
Lon = (-179:1:180)'; %# comment for formatting'
Lat = -90:1:90;
data = randn(length(Lon),length(Lat)); #% generate random data
mask = randi(2,size(data))-1; #% generate a random mask (zeros and ones)
point_matrix = randi(4,size(data)-1)==4; #% random (logical) matrix
[r c]=find(point_matrix==1); %# get the coordinates of the ones
figure;
hold on;
#% save a handle to the surface object created by pcolor
h=pcolor(repmat(Lon ,1,length(Lat)),repmat(Lat,length(Lon),1),data);
#% set properties of that surface: use mask as alphadata for transparency
#% and set 'facealpha' property to 'flat', and turn off edges (optional)
set(h,'alphadata',mask,'facealpha','flat','edgecolor','none');
#% overlay black dots on the center of the randomly chosen tiles
plot(r-179.5,c-89.5,'.k')

0

tmpearce的回答很好,但我发现对于Matlab 2022a,我需要添加alphadatamapping设置。

set(h,'alphadata',mask,'alphadatamapping','none',...
      'facealpha','flat','edgealpha','flat',...
      'edgecolor','none');

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接