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

조회 수: 16 (최근 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 CenterFile Exchange에서 Surface and Mesh Plots에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by