Need help creating a plot with x,y,z (2d plot with contour)

조회 수: 5 (최근 30일)
Mackenzie Weeks
Mackenzie Weeks 2021년 2월 7일
편집: Cris LaPierre 2021년 5월 6일
Hi! I need help creating a plot. I am given x,y, and z data points. I need to plot x and y and contour the z points. I've provided the code I created, although not sure if im on the right track or not. I have also attached a plot of what the plot should look similar to.
clear all; close all; clc
%knownn info
x = [0 13 48 56 101 208 255 297 321 342 451 541 567 599 605 632 675 700 710 750];
y = [123 540 22 321 111 679 314 220 543 119 440 256 12 431 337 709 229 109 334 402];
z = [130.64 131.99 130.58 131.66 131.28 133.31 132.71 132.36 133.36 132.28 134.04 134.09 133.24 134.53 134.57 135.73 134.40 134.02 135.79 135.20];
xv = linspace(min(x),max(x),numel(x));
yv = linspace(min(y),max(y),numel(y));
[Xm,Ym] = ngrid(xv, yv);
Zm = griddata (x,y,z,Xm,Ym);
figure
contourf(Xm,Ym,Zm)
grid
  댓글 수: 2
Muh Alam
Muh Alam 2021년 2월 7일
Z coordinates( in your code Zm) must be matrix 2x2 at least. it is vector in the code you gave.
Mackenzie Weeks
Mackenzie Weeks 2021년 2월 7일
yes, but how would I go about that?

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

채택된 답변

Cris LaPierre
Cris LaPierre 2021년 2월 8일
편집: Cris LaPierre 2021년 5월 6일
You have to do some interpolation to create a contour plot from your data. When plotting, you must have gridded data, meaning all your z values in a column must be for the same x value, and your row values must be for the same y value. I would suggest using scatteredinterpolant.
%knownn info
x = [0 13 48 56 101 208 255 297 321 342 451 541 567 599 605 632 675 700 710 750];
y = [123 540 22 321 111 679 314 220 543 119 440 256 12 431 337 709 229 109 334 402];
z = [130.64 131.99 130.58 131.66 131.28 133.31 132.71 132.36 133.36 132.28 134.04 134.09 133.24 134.53 134.57 135.73 134.40 134.02 135.79 135.20];
% Create a scattered interpolant for the existing data
F=scatteredInterpolant(x',y',z',"linear","linear");
% Now create regularly spaced x and y values
xq = linspace(min(x),max(x),50);
yq = linspace(min(y),max(y),50);
[Xq,Yq] = meshgrid(xq,yq);
% Use the meshgrid of x and y to compute your grid of z values
Zq = F(Xq,Yq);
contourf(Xq,Yq,Zq)
Just be aware that your interpolation is an approximation based on the data you have. The more data, the better.
  댓글 수: 3
Cris LaPierre
Cris LaPierre 2021년 2월 8일
편집: Cris LaPierre 2021년 2월 8일
You can use the ShowText name-value pair in contourf.
%knownn info
x = [0 13 48 56 101 208 255 297 321 342 451 541 567 599 605 632 675 700 710 750];
y = [123 540 22 321 111 679 314 220 543 119 440 256 12 431 337 709 229 109 334 402];
z = [130.64 131.99 130.58 131.66 131.28 133.31 132.71 132.36 133.36 132.28 134.04 134.09 133.24 134.53 134.57 135.73 134.40 134.02 135.79 135.20];
% Create a scattered interpolant for the existing data
F=scatteredInterpolant(x',y',z',"linear","linear");
% Now create regularly spaced x and y values
xq = linspace(min(x),max(x),50);
yq = linspace(min(y),max(y),50);
[Xq,Yq] = meshgrid(xq,yq);
% Use the meshgrid of x and y to compute your grid of z values
Zq = F(Xq,Yq);
contourf(Xq,Yq,Zq,'ShowText',true)
You can also use the clabel function.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Surface and Mesh Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by