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

11 ビュー (過去 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.
Mackenzie Weeks
Mackenzie Weeks 2021 年 5 月 6 日
thanks so much!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeContour Plots についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by