How to get a specific shape of a contour plot in MATLAB

I have plotted this figure using the MATLAB contourf(x,y,z) function, where x and y are the vectors and Z is a matrix. How can only I get the Lower triangle?
Thank You.
x = linspace(0,1,11);
y = linspace(0,1,11);
Z = [0 0 0 0 0 0 0 0 0 0 0
0 -2.5688 -3.8969 -1.6041 2.3402 6.1319 7.8457 4.7247 0.3582 0 0
0 -4.6769 -8.1296 -4.7086 3.0256 9.9652 12.6373 7.2991 0 0 0
0 -6.0168 -10.9420 -8.0819 0.2356 7.1892 7.7445 0 0 0 0
0 -7.1333 -12.4002 -9.9456 -2.8682 2.7014 0 0 0 0 0
0 -7.6917 -11.9661 -8.3691 -2.2996 0 0 0 0 0 0
0 -6.8522 -9.0808 -4.4436 0 0 0 0 0 0 0
0 -4.3650 -4.4620 0 0 0 0 0 0 0 0
0 -1.4376 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0];
newpoints = 100;
[xq,yq] = meshgrid(...
linspace(min(min(x,[],2)),max(max(x,[],2)),newpoints ),...
linspace(min(min(y,[],1)),max(max(y,[],1)),newpoints )...
);
Bumatrixq = interp2(x,y,Z',xq,yq,'cubic');
figure(1)
[~,~]=contourf(xq,yq,Bumatrixq,10,'Linestyle',':','Linewidth',0.01);
xlabel('$x$','FontSize',20,'interpreter','latex')
ylabel('$y$','FontSize',20,'interpreter','latex')

6 Comments

@Bibigul, Can you show the code for the generating the same contour in the image?
Do you mean to plot a straight black line between (0, 1) and (1, 0)?
plot([0 1], [1 0], 'k', LineWidth=3)
@Sam Chak No I want the lower triangle part of the plot.
As requested before, please share the code used for generating the contour plot shown in the image.
@Bibigul, Do you mean to display the Lower Triangle protion with a white shade by covering the Upper Triangle?

Sign in to comment.

 Accepted Answer

Check if this is what you want.
x = linspace(0,1,11);
y = linspace(0,1,11);
Z = [0 0 0 0 0 0 0 0 0 0 0
0 -2.5688 -3.8969 -1.6041 2.3402 6.1319 7.8457 4.7247 0.3582 0 0
0 -4.6769 -8.1296 -4.7086 3.0256 9.9652 12.6373 7.2991 0 0 0
0 -6.0168 -10.9420 -8.0819 0.2356 7.1892 7.7445 0 0 0 0
0 -7.1333 -12.4002 -9.9456 -2.8682 2.7014 0 0 0 0 0
0 -7.6917 -11.9661 -8.3691 -2.2996 0 0 0 0 0 0
0 -6.8522 -9.0808 -4.4436 0 0 0 0 0 0 0
0 -4.3650 -4.4620 0 0 0 0 0 0 0 0
0 -1.4376 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0];
newpoints = 100;
[xq,yq] = meshgrid(...
linspace(min(min(x,[],2)),max(max(x,[],2)),newpoints ),...
linspace(min(min(y,[],1)),max(max(y,[],1)),newpoints )...
);
Bumatrixq = interp2(x,y,Z',xq,yq,'cubic');
contourf(xq, yq, Bumatrixq, 8), hold on
xlabel('x'), ylabel('y')
xx = [0 1 1];
yy = [1 0 1];
patch(xx, yy, 'white')

More Answers (1)

One simple way to obtain that is to fill the upper half with white color -
x = linspace(0,1,11);
y = linspace(0,1,11);
Z = [0 0 0 0 0 0 0 0 0 0 0
0 -2.5688 -3.8969 -1.6041 2.3402 6.1319 7.8457 4.7247 0.3582 0 0
0 -4.6769 -8.1296 -4.7086 3.0256 9.9652 12.6373 7.2991 0 0 0
0 -6.0168 -10.9420 -8.0819 0.2356 7.1892 7.7445 0 0 0 0
0 -7.1333 -12.4002 -9.9456 -2.8682 2.7014 0 0 0 0 0
0 -7.6917 -11.9661 -8.3691 -2.2996 0 0 0 0 0 0
0 -6.8522 -9.0808 -4.4436 0 0 0 0 0 0 0
0 -4.3650 -4.4620 0 0 0 0 0 0 0 0
0 -1.4376 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0];
newpoints = 100;
[xq,yq] = meshgrid(linspace(min(min(x,[],2)),max(max(x,[],2)),newpoints ),...
linspace(min(min(y,[],1)),max(max(y,[],1)),newpoints ));
Bumatrixq = interp2(x,y,Z',xq,yq,'cubic');
figure(1)
hold on
contourf(xq,yq,Bumatrixq,10,'Linestyle',':','Linewidth',0.01)
xlabel('$x$','FontSize',20,'interpreter','latex')
ylabel('$y$','FontSize',20,'interpreter','latex')
%Add border for the region
plot([0 1 0 0],[1 0 0 1],'k-','LineWidth',2)
%Fill the upper region with white color
fill([0 1 1],[1 0 1],'w')

3 Comments

@Dyuman Joshi thank you very much for the help.
Hi @Bibigul, the 'fill()' function also works, and the @Dyuman Joshi's solution deserves a vote.
Yes @Sam Chak I appriciate @Dyuman Joshi's effort too. I have voted both solutions. Thank you both for your help.

Sign in to comment.

Asked:

on 1 Oct 2023

Commented:

on 1 Oct 2023

Community Treasure Hunt

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

Start Hunting!