3D plot: draw xy grid line on the middle of the plot box

2 views (last 30 days)
I have a 3D plot. The grid lines typically render at limiting planes of the plot box. I'd like them to render on a plane inside those limits. For example, I have a box that is +/-2 units in x, y, and z. I turn off the z grid. I'd like the xy grid to render in the xy plane at z=0. Is there a way to do this with out explicitly drawing lines manually?
  2 Comments
William Rose
William Rose on 16 Apr 2025
You can move the axes in a 2D plot, as shown below. If you could move the axes in a 3D plot, maybe the gridlines would move too. But XAxisLocation and YAxis location seem ineffecual in a 3D plot.
x=-2:.05:2; y=2*sin(x*pi);
figure
plot(x,y); grid on
ax=gca; ax.XAxisLocation='origin'; ax.YAxisLocation='origin';
Here's the 3D version. The axis locations do not move to the origin, despite the command to do so.
x=-2:0.2:2; y=-2:0.2:2;
[X,Y]=meshgrid(x,y);
Z=-2+4*exp(-(X.^2+Y.^2)/4);
surf(X,Y,Z); grid on;
ax=gca; ax.XAxisLocation='origin'; ax.YAxisLocation='origin';
Maybe someone else knows the way to make it happen.
Lloyd Bumm
Lloyd Bumm on 16 Apr 2025
You can controll the axes crossing point like this:
fh.XRuler.FirstCrossoverValue = YAcross; % X crossover with Y axis
fh.XRuler.SecondCrossoverValue = ZAcross; % X crossover with Z axis
fh.YRuler.FirstCrossoverValue = XAcross; % Y crossover with X axis
fh.YRuler.SecondCrossoverValue = ZAcross; % Y crossover with Z axis
fh.ZRuler.FirstCrossoverValue = XAcross; % Z crossover with X axis
fh.ZRuler.SecondCrossoverValue = YAcross; % Z crossover with Y axis

Sign in to comment.

Answers (2)

William Rose
William Rose on 16 Apr 2025
x=-2:0.2:2; y=-2:0.2:2;
[X,Y]=meshgrid(x,y);
Z=-2+4*exp(-(X.^2+Y.^2)/4);
surf(X,Y,Z);
grid3(x,y,0)
You could edit the function to get black or gray grid points, if you prefer.

Lloyd Bumm
Lloyd Bumm on 16 Apr 2025
I ended up just using xline and yline. That worked well because I wanted the grid ot be at z=0. Otherwise I'd need to use the plot limits and an elevation to creat the grid lines.
function draw_grid
grid_color = [0.15 0.15 0.15];
grid_alpha = 0.15;
x_ticks = [-20 -10, 0, 10, 20, 30];
for ii = 1: length(x_ticks)
xline(x_ticks(ii),'color',grid_color,'LineWidth',2,'alpha',grid_alpha);
end
y_ticks = [-10, 0, 10, 20, 30];
for ii = 1: length(y_ticks)
yline(y_ticks(ii),'color',grid_color,'LineWidth',2,'alpha',grid_alpha);
end
end

Categories

Find more on Axes Appearance in Help Center and File Exchange

Tags

Products


Release

R2024b

Community Treasure Hunt

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

Start Hunting!