How do I plot an empty circle with no values in the middle of meshgrid plot3

5 ビュー (過去 30 日間)
Chua Wei Yi
Chua Wei Yi 2022 年 10 月 11 日
回答済み: Jaswanth 2024 年 8 月 2 日
Hi everyone.
Bascially I am tracking the depth of a surface using grids. x,y coordinates and z for depth. To fill the voids in between grids, I am using meshgrid with natural interpolation. However I have placed a circular piece in the middle of the grid hence on a plot3 it is supposed to be a white circular figue. How do I make sure this is drawn correctly because the image I attached does not look correct. Also how do I make sure that the meshgrid interpolates around the shape of the circle in the middle? This the code I have currently.
x = data(:,1); y = data(:,2); z = data(:,3)
x = table2array(x); y = table2array(y); z = table2array(z)
xlin = linspace(min(x), max(x), 100);
ylin = linspace(min(y), max(y), 100);
[X,Y] = meshgrid(xlin, ylin);
Z = griddata(x,y,z,X,Y,'natural');
% Z = griddata(x,y,z,X,Y,'cubic');
% Z = griddata(x,y,z,X,Y,'v4');
mesh(X,Y,Z)
axis tight; hold on
plot3(x,y,z,'.','MarkerSize',15)

回答 (1 件)

Jaswanth
Jaswanth 2024 年 8 月 2 日
Hi,
To plot an empty circle with no values in the middle of a meshgrid plot and ensure the meshgridinterpolates around the shape of the circle, you can mask out the circular region in the Z matrix. Here is how you can modify your code to achieve this:
  • Define the center and radius of the circle.
  • Create a mask for the circular region.
  • Apply the mask to the Z matrix to set the values inside the circle to NaN.
  • Plot the meshgrid with the masked Z matrix.
Please refer to the following example code with assumed data:
% Define the data directly
x = rand(100,1) * 100; % Example x-coordinates
y = rand(100,1) * 100; % Example y-coordinates
z = rand(100,1) * 10; % Example z-coordinates (depth)
xlin = linspace(min(x), max(x), 100);
ylin = linspace(min(y), max(y), 100);
[X,Y] = meshgrid(xlin, ylin);
Z = griddata(x,y,z,X,Y,'natural');
% Define the center and radius of the circle
centerX = mean(x); % or specify the exact center
centerY = mean(y); % or specify the exact center
radius = 10; % specify the radius of the circle
% Create a mask for the circular region
distanceFromCenter = sqrt((X - centerX).^2 + (Y - centerY).^2);
mask = distanceFromCenter <= radius;
% Apply the mask to the Z matrix
Z(mask) = NaN;
% Plot the meshgrid with the masked Z matrix
mesh(X,Y,Z)
axis tight; hold on
plot3(x,y,z,'.','MarkerSize',15)
I hope the solution provided above is helpful.

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by