Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Contourf and NaNs (how to make white regions transparent)

Tags:

image

matlab

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:

Code:

[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;

Output:

enter image description here

Hint:

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:

enter image description here

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

enter image description here

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.

like image 853
JustinBlaber Avatar asked Mar 26 '13 21:03

JustinBlaber


1 Answers

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]);

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.

enter image description here

like image 198
pancake Avatar answered Sep 23 '22 18:09

pancake