How to get a specific shape of a contour plot in MATLAB
Show older comments
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)
Bibigul
on 1 Oct 2023
Dyuman Joshi
on 1 Oct 2023
As requested before, please share the code used for generating the contour plot shown in the image.
Bibigul
on 1 Oct 2023
Bibigul
on 1 Oct 2023
Accepted Answer
More Answers (1)
Dyuman Joshi
on 1 Oct 2023
Edited: Dyuman Joshi
on 1 Oct 2023
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')
Categories
Find more on Big Data Processing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!



