You can follow these steps to plot your X-Y-Z triplets in the corresponding X-Y polygon region:
- Build an evenly spaced 2D grid on X-Y plane.
- Interpolate scattered data.
- Extract the boundary in X-Y plane and plot the data.
Here is an example using the attached "data.mat", which stores an 800×3 matrix, with each column corresponding to x, y, and z information. Below is the example code for importing data into the workspace and plotting the data:
load data.mat
x=data(:, 1); y=data(:, 2); z=data(:, 3);
figure(1);
plot3(x, y, z ,'.');
xlabel('x');
ylabel('y');
zlabel('z');
title('3D raw data');
grid on
The 3D view of dataset and 2D view of X-Y plane are shown.
You can create an evenly spaced, 2D grid using the "meshgrid" function. This will help in organizing your scattered data into a structured grid format. Then use the "griddata" function to interpolate new z-values at the points contained in the output of "meshgrid". This step is crucial for generating a smooth surface or contour plot.
[xq, yq] = meshgrid(x, y);
zq = griddata(x, y, z, xq, yq, 'nearest');
figure(2);
contourf(xq, yq, zq, 'LineStyle', 'none');
xlabel('x');
ylabel('y');
zlabel('z');
title('Contour Plot');
Extract the polygon region formed by the data points in the "x" and "y" vectors using the "boundary" and "inpolygon" functions. By setting the z-values outside the polygon region as "NaN", those areas will not appear in the generated 2D contour plot.
k = boundary(x, y, 1);
mask = inpolygon(xq, yq, x(k), y(k));
zq(~mask) = NaN;
numcontours = 30;
figure(3); contourf(xq, yq, zq, numcontours, 'LineStyle', 'none');
xlabel('x');
ylabel('y');
zlabel('z');
title('Contour Plot with Boundary');