Meshgrid on inhomogeneous surface plot

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])

Answers (2)

% 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 Comments

The lines do not seem to be at regular intervals.
% 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)
surf(xx,yy,ZZ)
% 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), 20);
yGrid = linspace(min(y), max(y), 20);
% 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), 'r'); % '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,:), 'r'); % '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]);
% 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.
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.

Sign in to comment.

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 Comments

One of my requirements is to make a coarse grid.
A fine grid results in too many black lines (as shown in my original data plot), which is not visually appealing. It might even reduce readability of data.
Unfortunately, using a coarse/coarser grid with griddata does not retain enough of the shape.
You can try experimenting with movmean or movmedian, like I have shown below, but that won't solve the underlying issue - As long as the original data is irregular, so will be the generated surface.
The idea is clear - to make the data regular. The method is not, atleast not to me yet.
We have already tried interpolation to do that, but as you can see, it has its own limitations.
% 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])
hold on
xxh = movmedian(xx, 3, 1);
yyh = movmedian(yy, 3, 1);
ZZh = movmedian(ZZ, 3, 1);
for k=1:size(xx,1)
plot3(xxh(k,:), yyh(k,:), ZZh(k,:), 'w--')
end
xxv = movmedian(xx, [5 2], 2);
yyv = movmedian(yy, [5 2], 2);
ZZv = movmedian(ZZ, [5 2], 2);
for k=1:size(xx,2)
plot3(xxv(:,k), yyv(:,k), ZZv(:,k), 'w--')
end
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.

Sign in to comment.

Categories

Asked:

on 16 Jan 2024

Commented:

on 19 Jan 2024

Community Treasure Hunt

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

Start Hunting!