Meshgrid on inhomogeneous surface plot

2 次查看(过去 30 天)
I have simulated data, that behaves inhomogeneous along x and y, resulting in an surface plot as shown below.
Due to the inhomogeneity, the build-in edges of the surf function results in an accumulation of black lines at the origin as well as the end of thy y-axis. An additional challenge is that the plot is in the shape of a triangular, thus not covering the whole x-y plane.
I want to have lines along x and y with equidistant distance on top of the surface plot. Its kind of the background grid on the x-y plane at z = 0, but projected on the surface plot.
Here is a minimal example to generate an inhomogeneous surface plot:
% Create inhomogenous arrays x and y
x = [0 1 2 2.5 2.6 2.7 2.8 2.85 2.9 2.95 2.975];
y = x+5;
% Create an inhomogenous mesh xx and yy
xx = x;
yy = y;
for i=2:length(x)-1
xx = cat(1,xx,x.^(1+i/10));
yy = cat(1,yy,y.^(2+i/10));
end
ZZ = xx + yy;
figure(1)
clf
surf(xx,yy,ZZ)
xlabel('x')
ylabel('y')
colormap sky
xlim([0 9])
ylim([0 500])
zlim([0 500])

回答(2 个)

Hassaan
Hassaan 2024-1-16
编辑:Hassaan 2024-1-16
% Create inhomogenous arrays x and y
x = [0 1 2 2.5 2.6 2.7 2.8 2.85 2.9 2.95 2.975];
y = x+5;
% Create an inhomogenous mesh xx and yy
xx = x;
yy = y;
for i=2:length(x)-1
xx = cat(1,xx,x.^(1+i/10));
yy = cat(1,yy,y.^(2+i/10));
end
ZZ = xx + yy;
figure(1)
clf
surf(xx,yy,ZZ)
xlabel('x')
ylabel('y')
colormap sky
xlim([0 9])
ylim([0 500])
zlim([0 500])
% Your existing code to create the surface plot
% ...
% Step 2: Determine Grid Points
% Example: Creating 10 evenly spaced grid lines along x and y
xGrid = linspace(min(x), max(x), 10);
yGrid = linspace(min(y), max(y), 10);
% Step 3 & 4: Calculate Grid Line Coordinates and Plot
hold on; % Keep the current surface plot
for i = 1:length(xGrid)
% Find the closest points in xx to the current grid line
[~, idx] = min(abs(xx(1,:) - xGrid(i)), [], 2);
plot3(xx(:,idx), yy(:,idx), ZZ(:,idx), 'k'); % 'k' for black lines
end
for i = 1:length(yGrid)
% Find the closest points in yy to the current grid line
[~, idx] = min(abs(yy(:,1) - yGrid(i)), [], 2);
plot3(xx(idx,:), yy(idx,:), ZZ(idx,:), 'k'); % 'k' for black lines
end
hold off;
% Step 5: Adjust Aesthetics
% Set the properties of the plot as needed
xlabel('x');
ylabel('y');
colormap sky;
xlim([0 9]);
ylim([0 500]);
zlim([0 500]);
This code creates a set of grid lines at regular intervals along the x and y axes and plots them on the surface plot. The plot3 function is used to plot lines in 3D space. Adjust the number of grid lines by changing the number in linspace. Also, fine-tune the appearance of the grid lines (like color, line width) as needed.
-----------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
Professional Interests
  • Technical Services and Consulting
  • Embedded Systems | Firmware Developement | Simulations
  • Electrical and Electronics Engineering
Feel free to contact me.
  3 个评论
Hassaan
Hassaan 2024-1-16
编辑:Hassaan 2024-1-16
% Create a more homogeneous mesh
x = linspace(0, 3, 100); % 100 points from 0 to 3
y = x + 5; % Adjust as needed
[xx, yy] = meshgrid(x, y);
ZZ = xx + yy.^2; % Example calculation for ZZ
% Plotting the surface
figure(1);
clf;
surf(xx, yy, ZZ);
xlabel('x');
ylabel('y');
colormap sky;
xlim([0, 9]);
ylim([5, 8]); % Adjust according to your y range
zlim([0, 500]);
% Adding grid lines
hold on;
xGrid = linspace(min(x), max(x), 10); % 10 evenly spaced x-grid lines
yGrid = linspace(min(y), max(y), 10); % 10 evenly spaced y-grid lines
for i = 1:length(xGrid)
plot3([xGrid(i) xGrid(i)], [min(y) max(y)], [500 500], 'k'); % Grid lines along x-axis
end
for i = 1:length(yGrid)
plot3([min(x) max(x)], [yGrid(i) yGrid(i)], [500 500], 'k'); % Grid lines along y-axis
end
hold off;
-----------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
Professional Interests
  • Technical Services and Consulting
  • Embedded Systems | Firmware Developement | Simulations
  • Electrical and Electronics Engineering
Feel free to contact me.
Christian Hanzl
Christian Hanzl 2024-1-17
As far as I can see, your code generates a grid at a constant altitude z. However, I want the grid in or on top of the surface plot, similar to the contour3 function, which generates contour lines on the surface plot.

请先登录,再进行评论。


Dyuman Joshi
Dyuman Joshi 2024-1-16
To modify the lines on the surface, you will have to modify the underlying data.
You might not be able to get the exact shape, but griddata with a finer grid seems to be the best bet -
% Create inhomogenous arrays x and y
x = [0 1 2 2.5 2.6 2.7 2.8 2.85 2.9 2.95 2.975];
y = x+5;
% Create an inhomogenous mesh xx and yy
xx = x;
yy = y;
for i=2:length(x)-1
xx = cat(1,xx,x.^(1+i/10));
yy = cat(1,yy,y.^(2+i/10));
end
ZZ = xx + yy;
figure()
s = surf(xx,yy,ZZ);
xlim([0 9])
ylim([0 500])
zlim([0 500])
%Generate linearly spaced points in the range of the data
xG = linspace(min(xx,[],'all'), max(xx,[],'all'), 75);
yG = linspace(min(yy,[],'all'), max(yy,[],'all'), 75);
%Make a meshgrid with the linearly spaced points
%to use as input for grid-data
[xq, yq] = meshgrid(xG, yG);
vq = griddata(xx, yy, ZZ, xq, yq);
figure()
surf(xq, yq, vq)
xlim([0 9])
ylim([0 500])
zlim([0 500])
  4 个评论
Christian Hanzl
Christian Hanzl 2024-1-19
I decided to plot only a few selected lines of the underlying data to make it look homogeneous. However, these lines do not represent the lines of the x- and y-axis coordinate systems. Its a compromise I have to live with now.
I have to wait for now until Matlab adds a feature that projects the x-y coordinate system on the surface of an inhomogenous surface plot.
Thanks for your time and help @Dyuman Joshi, I really appreciate it.

请先登录,再进行评论。

类别

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