matlaboverlaymaskstipple

shading and masking over pcolor in matlab


Two questions really, but I feel they overlap, so I'll ask them in one place (if that's OK).

I've created a pcolor in matlab using:

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

But now I want to add some information. I have a matrix mask which has the same dimensions as data, but is populated by 1s and 0s. Where it contains a 1, I would like to retain the pixel in the pcolor, and when it contains a 0, remove it (not just turn the value to zero, which is not represented by a white pixel in my colormap).

Secondly, I have a second matrix, called 'stipple', which again contains 0s and 1s. I now want to overlay any location represented by a 1 with a stipple effect.

The purpose of this is to create an image something like this: http://www.ipcc.ch/publications_and_data/ar4/wg1/en/figure-spm-7.html where the average is painted on, but areas where there is too much disagreement are whited out, and areas where there is much agreement are 'stippled'.

Thanks in advance!


Solution

  • The approach I would take is to use your mask for transparency. This is slightly more complicated for pcolor because it doesn't let you set properties directly when creating the plot, but you can grab the handle to it and do it that way.

    To overlay stippling, you can just hold on so the pcolor plot doesn't disappear, and use 'plot' to plot points where you want them.

    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')