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

23 views (last 30 days)
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 Comments
Muh Alam
Muh Alam on 7 Feb 2021
Z coordinates( in your code Zm) must be matrix 2x2 at least. it is vector in the code you gave.

Sign in to comment.

Accepted Answer

Cris LaPierre
Cris LaPierre on 8 Feb 2021
Edited: Cris LaPierre on 6 May 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 Comments
Cris LaPierre
Cris LaPierre on 8 Feb 2021
Edited: Cris LaPierre on 8 Feb 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.

Sign in to comment.

More Answers (0)

Categories

Find more on Contour Plots in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!