Plotting a circular surface with certain values

5 次查看(过去 30 天)
Hello everybody, here goes my question. I've spent a lot of time at my work trying to plot a circular surface where I charge certain parameter at certain points of this circle. The thing is that I would like to get a nice plot, something like figure 1. I only have the coordinates in the plane X,Y the smaller spheres would represent the parameters I get from a certain measurement.
Thank you very much, so far, honestly, I'm quite tired of trying to plots such a surface by my own, charging values to each cell.

回答(1 个)

Karan Singh
Karan Singh 2025-2-21
A good workflow to be followed will be
  1. Interpolate your 2D scattered data (xi,yi,pi) onto a 2D grid to create a smooth “height field” (surface).
  2. Plot that surface in 3D using "surf", "mesh".
  3. Overlay small spheres at the original data points to visualize exact measurement locations/values.
This approach should help you build a nice 3D visualization similar in spirit to your reference figure though of course the exact “shape” depends on your interpolation and how you scale/color the spheres. I have done the process with some random data
% --- 1) Create some sample scattered data (x, y, p) ---
% (Replace this part with your real data)
N = 30; % Number of sample points
theta = 2*pi*rand(N,1); % Random angles
r = 10*rand(N,1); % Random radii
x = r .* cos(theta); % X-coordinates in a circular region
y = r .* sin(theta); % Y-coordinates in a circular region
p = x.^2 + y.^2; % Some function of x,y (example)
% --- 2) Interpolate onto a grid ---
numGrid = 50; % how many points in each dimension of the grid
xMin = min(x); xMax = max(x);
yMin = min(y); yMax = max(y);
% Create a mesh grid covering the data extent
[xq, yq] = meshgrid(linspace(xMin, xMax, numGrid), ...
linspace(yMin, yMax, numGrid));
% Interpolate p onto the grid
% (methods: 'linear', 'cubic', 'natural', etc.)
zq = griddata(x, y, p, xq, yq, 'cubic');
% --- 3) Plot the interpolated surface ---
figure('Color', 'w'); % new figure, white background
surf(xq, yq, zq);
shading interp; % smooth shading
colormap jet; % choose a colormap
colorbar; % show a color scale
hold on;
axis equal; % preserve aspect ratio (so circles remain circular)
xlabel('X'); ylabel('Y'); zlabel('Parameter');
title('3D Surface + Spheres at Original Data Points');
% --- 4) Overlay small spheres at each original point ---
% Generate a unit sphere for re-use
[sphereX, sphereY, sphereZ] = sphere(12); % 12 = resolution of the sphere
% Loop through each data point and place a small sphere
for i = 1:length(x)
% Radius can be constant or scaled by p(i)
% (Here we just pick a small constant radius)
r = 0.5;
% Shift the unit sphere to (x_i, y_i, p_i)
Sx = r*sphereX + x(i);
Sy = r*sphereY + y(i);
Sz = r*sphereZ + p(i);
% Create the sphere surface
hsphere = surf(Sx, Sy, Sz, ...
'EdgeColor', 'none', ... % no mesh lines
'FaceColor', 'interp'); % interpolate face colors
% Color the entire sphere by p(i) (or you can get fancy with gradients)
cdata = p(i)*ones(size(Sx)); % each vertex has color p(i)
set(hsphere, 'CData', cdata);
end
% Optional: adjust view angle
view(45, 30); % rotate camera [az, el]
Karan

类别

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