How to edit grid lines on a 3D plot

29 views (last 30 days)
Hans123 on 14 May 2020
Answered: Tommy on 14 May 2020
I am trying to change the traditional grid on a MATLAB figure to one that I have calculated according to my data points.
I have a used
[X,Y,Z] = meshgrid(xdim,ydim,zdim);
and using grid on wiil not cause the grid on my 3D plot to have the same divisions.
Could someone point out how I can fix this?
Hans123 on 14 May 2020
Edited: Rik on 14 May 2020
for i=1:length(g_x)
plot3([g_x(i) g_x(i)],[g_y(1) g_y(end)],[g_z(1) g_z(end)],'k') %xz grid lines
hold on
for i=1:length(g_y)
plot3([g_x(1) g_x(end)],[g_y(i) g_y(i)],[g_z(1) g_z(end)],'k') %xy grid lines
hold on
for i=1:length(g_z)
plot3([g_x(1) g_x(end)],[g_y(1) g_y(end)],[g_z(i) g_z(i)],'k') %zy grid lines
hold on
Is my fix for a 3D plot, however it dows not work as I expect to. I am trying to get a xz,xy, and, yz plane all to intersect and form a grid

Sign in to comment.

Accepted Answer

Tommy on 14 May 2020
It seems to me that Dillen.A's answer from your link is pretty elegant.
Adapting that code to work with 3D axes:
f = figure;
ax1 = axes(f);
surf(ax1, peaks(50)); % example plot
grid(ax1, 'off');
axis(ax1, 'tight');
% Second invisible axes:
ax2 = axes('Position',ax1.Position,...
'TickLength',[0 0],...
% Set the ticks to whatever you want:
ax2.YTick = linspace(ax1.YLim(1),ax1.YLim(2),5);
ax2.XTick = linspace(ax1.XLim(1),ax1.XLim(2),5);
ax2.ZTick = linspace(ax1.ZLim(1),ax1.ZLim(2),5);
% Show the gridlines, link the axes:
grid(ax2, 'on');
linkprop([ax1, ax2],{'CameraUpVector', 'CameraPosition', 'CameraTarget', 'XLim', 'YLim', 'ZLim'});
Credit to this answer for the code which linked the two sets of axes.

More Answers (0)


Find more on Graphics Object Properties in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!