How to edit grid lines on a 3D plot

64 Ansichten (letzte 30 Tage)
Hans123
Hans123 am 14 Mai 2020
Beantwortet: Tommy am 14 Mai 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?
  2 Kommentare
Hans123
Hans123 am 14 Mai 2020
Bearbeitet: Rik am 14 Mai 2020
Below is a method to do what I am hoping for, but it is not as elegant as I hoped and involves multiple plots that mimic grid lines
x=[20:0.1:80];
y=sin(x);
plot(x,y,'r','Linewidth',2)
ylim([0 4]);
xlim([0 100]);
% gridlines ---------------------------
hold on
g_y=[0:0.1:4]; % user defined grid Y [start:spaces:end]
g_x=[0:2:100]; % user defined grid X [start:spaces:end]
for i=1:length(g_x)
plot([g_x(i) g_x(i)],[g_y(1) g_y(end)],'k:') %y grid lines
hold on
end
for i=1:length(g_y)
plot([g_x(1) g_x(end)],[g_y(i) g_y(i)],'k:') %x grid lines
hold on
end
print(1,'-dpng','-r300','K1') %save plot as png (looks better)
Hans123
Hans123 am 14 Mai 2020
Bearbeitet: Rik am 14 Mai 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
end
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
end
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
end
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

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Tommy
Tommy am 14 Mai 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,...
'Color','none',...
'YLim',ax1.YLim,...
'XLim',ax1.XLim,...
'ZLim',ax1.ZLim,...
'TickLength',[0 0],...
'YTickLabel',[],...
'XTickLabel',[],...
'ZTickLabel',[],...
'View',ax1.View);
% 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.

Weitere Antworten (0)

Kategorien

Mehr zu 2-D and 3-D Plots finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by