Plotting two contourf plots on the same plot

조회 수: 24 (최근 30일)
Elie Hatem
Elie Hatem 2020년 6월 20일
답변: Long Hoang Van 2021년 9월 24일
Hello,
I am trying to plot two data matrices on the same plot. However, I do not know how to adjust the transparancy of one of the plots.
The code is shown below with the attached data of Z1 and Z2.
If I try to plot the on top of each other just like the code of figure (3), the data values of the Z1 will not fully be visible since the Z2 data will be on top of it.
Is there a way to adjust the transparency of the plotted Z2 data so that the the full Z1 data will be visible?
And, is there a possibility to change the color of each contourf plot?
Thank you for your time,
Elie Hatem
clear all;clc;
x=-pi:0.1:pi;
y=x;
[X,Y]=meshgrid(x,y);
load('motorization_data.mat','Z1','Z2')
figure(1)
contourf(X,Y,Z1,[0 0])
figure(2)
contourf(X,Y,Z2,[0 0])
figure(3)
contourf(X,Y,Z1,[0 0])
hold on
contourf(X,Y,Z2,[0 0])
hold off
  댓글 수: 5
Elie Hatem
Elie Hatem 2020년 6월 21일
Yes, I saw this link prior to asking my question.
I tried to use "fill". However, my plot was not plotting as desired.
I solved the problem by removing the columns which have zero elements in the X axis since it was clear that neither their X (which is zero) and their Y (belonging to the zero in the X) are part of the data. They were introduced when taking contourf was used. Moreover, the code below solved my problem:
figure(3)
j=contourf(X,Y,Z1,[0 0]);
hold on
contourf(X,Y,Z2,[0 0])
hold off
% Removing the unwanted values from j
i=0;
while (1)
l_j=length(j);
i=i+1;
if i==143
disp('i=143')
end
if j(1,i)==0
j(:,i)=[];
l_j=l_j-1;
end
if i==l_j
break;
end
end
%%
xi = j(1,:);
yi = j(2,:);
% Contour plot
figure(4)
contourf(X,Y,Z2,[0 0 ]);
% Transparent patch
hold on
fill(xi,yi,'m','FaceAlpha',0.5,'LineWidth',1);
hold off
madhan ravi
madhan ravi 2020년 6월 21일
Post it as answer.

댓글을 달려면 로그인하십시오.

채택된 답변

Elie Hatem
Elie Hatem 2020년 6월 21일
I solved the problem by removing the columns which have zero elements in the X axis since it was clear that neither their X (which is zero) and their Y (belonging to the zero in the X) are part of the data. They were introduced when taking contourf was used. Moreover, the code below solved my problem:
figure(3)
j=contourf(X,Y,Z1,[0 0]);
hold on
contourf(X,Y,Z2,[0 0])
hold off
% Removing the unwanted values from j
i=0;
while (1)
l_j=length(j);
i=i+1;
if i==143
disp('i=143')
end
if j(1,i)==0
j(:,i)=[];
l_j=l_j-1;
end
if i==l_j
break;
end
end
%%
xi = j(1,:);
yi = j(2,:);
% Contour plot
figure(4)
contourf(X,Y,Z2,[0 0 ]);
% Transparent patch
hold on
fill(xi,yi,'m','FaceAlpha',0.5,'LineWidth',1);
hold off

추가 답변 (1개)

Long Hoang Van
Long Hoang Van 2021년 9월 24일
clear all;clc;
x=-pi:0.1:pi;
y=x;
[X,Y]=meshgrid(x,y);
load('motorization_data.mat','Z1','Z2')
figure(1)
contourf(X,Y,Z1,[0 0])
figure(2)
contourf(X,Y,Z2,[0 0])
figure(3)
contourf(X,Y,Z1,[0 0])
hold on
contourf(X,Y,Z2,[0 0])
hold off

카테고리

Help CenterFile Exchange에서 Contour Plots에 대해 자세히 알아보기

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by