How to plot X-Y-Z triplets in a 2D polygon region boundary defined by X-Y pairs?

11 ビュー (過去 30 日間)
I want to plot numerical data with x, y, and z vectors, where x and y are the coordinates with values specified in z. The continuous relationship "z = f(x, y)" is unknown since the data is discrete. How do I produce a 2D contour plot of this data within the boundary of the X-Y plane specified by vectors x and y?

採用された回答

MathWorks Support Team
MathWorks Support Team 2025 年 1 月 29 日 0:00
You can follow these steps to plot your X-Y-Z triplets in the corresponding X-Y polygon region:
  1. Build an evenly spaced 2D grid on X-Y plane.
  2. Interpolate scattered data.
  3. 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');

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeSurface and Mesh Plots についてさらに検索

製品


リリース

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by