How to get contour plot of multiple Datas in the same plot?

조회 수: 2 (최근 30일)
Vaswati Biswas
Vaswati Biswas 2022년 11월 26일
댓글: Star Strider 2022년 11월 27일
I have X axis values [0 10 20 30 40 50 55] and y axis values starting from 300 to 2000 at the interval of 2. I also have seven Z axis values matching the dimension of y axis. I want to have the contour plot of all the datas in the same plot. My code is given below:
[x,y] = meshgrid(X,Y);
z= [Z1,Z2,Z3,Z4,Z5,Z6,Z7];
Z = zeros(length(x),length(y)) ;
for i = 1:length(x)
for j = 1:length(y)
if i==j % z data exist for only for x(n) y(n) location, n = 1,2,3...
Z(i,j) = z(i);
end
end
end
contourf(x,y,Z)
colorbar
But I am not getting the correct result. Kindly help.

답변 (1개)

Star Strider
Star Strider 2022년 11월 26일
I did something like this recently in Contourf plot of magnitude of transferfunction along trajectory. See if you can adapt that approach to your problem.
  댓글 수: 2
Vaswati Biswas
Vaswati Biswas 2022년 11월 27일
Thanks for your reply. But In this case one function is plotted but in my case I have different data therefore when I am trying to incoparate this approch I am getting the error "Data dimensions must agree".
Star Strider
Star Strider 2022년 11월 27일
They of course must agree.
You can use interp1 to make their lengths agree.
Example —
X1 = 0:0.1:10;
Z1 = X1.*exp(-0.75*X1)
Z1 = 1×101
0 0.0928 0.1721 0.2396 0.2963 0.3436 0.3826 0.4141 0.4390 0.4582 0.4724 0.4821 0.4879 0.4904 0.4899 0.4870 0.4819 0.4750 0.4666 0.4570 0.4463 0.4347 0.4225 0.4098 0.3967 0.3834 0.3699 0.3564 0.3429 0.3295
X2 = 0:0.5:20;
Z2 = sin(2*pi*X2*5)
Z2 = 1×41
1.0e-13 * 0 0.0061 -0.0122 0.0539 -0.0245 -0.0049 -0.1078 0.0784 -0.0490 0.0196 0.0098 0.2450 -0.2156 -0.0980 -0.1568 0.1274 -0.0980 0.0686 -0.0392 0.0098 0.0196 -0.0490 -0.4900 0.4606 -0.4312 -0.1667 0.1961 0.3430 -0.3136 -0.2843
X3 = 0:25;
Z3 = exp(-0.1*X3) .* cos(2*pi*3*X3)
Z3 = 1×26
1.0000 0.9048 0.8187 0.7408 0.6703 0.6065 0.5488 0.4966 0.4493 0.4066 0.3679 0.3329 0.3012 0.2725 0.2466 0.2231 0.2019 0.1827 0.1653 0.1496 0.1353 0.1225 0.1108 0.1003 0.0907 0.0821
N = 200;
xfcn = @(x) linspace(min(x), max(x), N); % Create Independent Interpolation Variable Vector For Each Vector
Z1m = interp1(X1, Z1, xfcn(X1)); % Interpolate To Same Lengths
Z2m = interp1(X2, Z2, xfcn(X2)); % Interpolate To Same Lengths
Z3m = interp1(X3, Z3, xfcn(X3)); % Interpolate To Same Lengths
figure
contourf([Z1m; Z2m; Z3m])
colormap(turbo)
Here, they’re interpolated to the same length, then (since they’re row vectors in this example), vertically concatenated to create a matrix, and then presented as that matrix to contourf.
.

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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by