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

19 vues (au cours des 30 derniers jours)
Mackenzie Weeks
Mackenzie Weeks le 7 Fév 2021
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 commentaires
Muh Alam
Muh Alam le 7 Fév 2021
Z coordinates( in your code Zm) must be matrix 2x2 at least. it is vector in the code you gave.
Mackenzie Weeks
Mackenzie Weeks le 7 Fév 2021
yes, but how would I go about that?

Connectez-vous pour commenter.

Réponse acceptée

Cris LaPierre
Cris LaPierre le 8 Fév 2021
Modifié(e) : Cris LaPierre le 6 Mai 2021
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 commentaires
Cris LaPierre
Cris LaPierre le 8 Fév 2021
Modifié(e) : Cris LaPierre le 8 Fév 2021
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 le 6 Mai 2021
thanks so much!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Contour Plots dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by