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:
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:
Update: Taking the NaN
region and superimposing the background image results in:
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With