Clear Filters
Clear Filters

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

6 views (last 30 days)
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)

Answers (1)

Jaswanth
Jaswanth on 2 Aug 2024
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.

Community Treasure Hunt

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

Start Hunting!