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

21 次查看(过去 30 天)
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

采纳的回答

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 中查找有关 Contour Plots 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by