I'm trying to plot the landmask (logical 0 or 1) using a uniform color. Below is my code:
% Turning the landmask from logical to numerical.
M = NaN(landmask);
M(landmask) = 1;
[C1, h1] = m_contourf (X, Y, M, [1, 1]); %This is the contour I need help with.
hold on
% another contourf plot:
[C2, h2] = m_contourf (X, Y, Z2, [-5:1:45], 'LineStyle', 'none' );
caxis([0 45]);
How do I specify the color of the first contour to a uniform color of [0.7 0.7 0.7]?
I tried the below and it did not work.
[C1, h1] = m_contourf (X, Y, M, [1, 1], 'color', [0.7 0.7 0.7] );
Right now, its color is basically dictated by the "caxis([0 45])" command the same way as the 2nd contourf plot.
Thanks.

 Réponse acceptée

Maybe you can avoid the problem by using fill or patch by extracting the coordinates of the perimeters of the landmask?
Something like this:
[C1, h1] = m_contourf (X, Y, M, [1, 1]);
i1 = 1;
i2 = C1(2,i1);
idxPatch = 1;
while i1 < size(C1,2)
i2 = C1(2,i1);
i1 = i1+1;
hF(idxPatch) = fill(C1(1,i1:(i1+i2-1)),C1(2,i1:(i1+i2-1)),[0.7 0.7 0.7]);
hold on
idxPatch = idxPatch + 1; % yeah, yeah, dynamically growing hF but too much work to find out how many fields in landmask...
i1 = i2+i1;
end
Now you should've replaced the contour-patches with regular patches where you can controll the colour directly (in my matlab-version there's no obvious color or facecolor property of the h1).
HTH

6 commentaires

Many thanks. I just gave it a try and got the below error:
Index in position 2 exceeds array bounds.
i2 = C1(2,i1);
Size(C1) gives me a 2 x 0 answer.
Oops. It just dawned on me that I used the plain contourf function for my example. Which m_contourf are you using?
The test-example I used was:
[C1,c2] = contourf(peaks(123),[1 1]*2.3);
i1 = 1;
hold on
while i1 < size(C1,2)
i2 = C1(2,i1);
i1 = i1+1;
fill(C1(1,i1:(i1+i2-1)),C1(2,i1:(i1+i2-1)),[0.7 0.7 0.7])
i1 = i2+i1;
end
That you get an empty array out from m_contourf indicates that you should have no contours returned from the function? (according to the help for the m_contourf-function I have it should return a C1 in the same manner as the built-in contourf-function, but my version is a user-developed tool...)
Leon
Leon le 3 Sep 2021
Modifié(e) : Leon le 3 Sep 2021
Thank you! I tried "contourf" and it gave me the same issue, i.e., size(C1) is 2 x 0.
Please see attached for my X, Y and M data that I used in the below command:
[C1, h1] = m_contourf (X, Y, M, [1, 1]);
OK, it seems that all the nans in M makes things go kaboom.
This solves most of the problems:
M(~isfinite(M(:))) = 0;
M(1,:) = 0;
M(:,1) = 0;
M(:,end) = 0;
M(end,:) = 0;
[C1,C2] = contourf(X,Y,M,[1 1]);
i1 = 1;
hold on
while i1 < size(C1,2)
i2 = C1(2,i1);
i1 = i1+1;
fill(C1(1,i1:(i1+i2-1)),C1(2,i1:(i1+i2-1)),[0.7 0.7 0.7])
i1 = i2+i1;
end
This leaves you with all interior waters filled in as well. Maybe you can "handle that later".
The fill-function also handles holes, provided that the orientation around the outer and inner edges are oposite, but when I tried to use that trick on the C1-array "it didn't turn out ideal":
[C1,C2] = contourf(X,Y,M,[1 1],'r');
xC = [];
yC = [];
i1 = 1;
while i1 < size(C1,2)
i2 = C1(2,i1);
i1 = i1+1;
xC = [xC,C1(1,i1:(i1+i2-1))];
yC = [yC,C1(2,i1:(i1+i2-1))];
i1 = i2+i1;
end
fill(xC,yC,[0.7 0.7 0.7])
Perhaps you can use parts of this.
Leon
Leon le 3 Sep 2021
Working now! Thank you so much.
My pleasure.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Contour Plots dans Centre d'aide et File Exchange

Produits

Version

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by