MATLAB Answers

How to create a secondary Cartesian coordinate system (three perpendicular coordinate axes) in Matlab?

조회 수: 4(최근 30일)
Sim 2021년 8월 21일
편집: Adam Danz 2021년 8월 23일
Hi, is it possible to create a secondary Cartesian coordinate system (i.e. the three mutually perpendicular coordinate axes) with origin at one of the three objects in this figure? If Yes, how? Any command or example?

채택된 답변

Adam Danz
Adam Danz 2021년 8월 22일
편집: Adam Danz 2021년 8월 23일
To add the additional axis lines,
box on
% Or
box(ax, 'on') % ax is the axis handle
but I didn't understand this part: with origin at one of the three objects
The undocumented Matlab blog shows how to move the location of axes origin
ax1 = nexttile;
xyz = [.5, .5, .5];
plot3(ax1, xyz(1), xyz(2), xyz(3), 'o', 'MarkerSize', 14)
grid(ax1, 'on')
axis(ax1, 'equal')
xlim(ax1, [0,1])
ylim(ax1, [0,1])
zlim(ax1, [0,1])
ax1.XRuler.FirstCrossoverValue = xyz(2); % y
ax1.XRuler.SecondCrossoverValue = xyz(3); % z
ax1.YRuler.FirstCrossoverValue = xyz(1); % x
ax1.YRuler.SecondCrossoverValue = xyz(3); % z
ax1.ZRuler.FirstCrossoverValue = xyz(1); % x
ax1.ZRuler.SecondCrossoverValue = xyz(2); % y
% Axis labels will also move with the axes
% xlabel(ax1, 'X axis')
% ylabel(ax1, 'Y axis')
% zlabel(ax1, 'Z axis')
title(ax1, 'Origin at xyz coordinate')
ax2 = nexttile;
xyz = [.5, .5, .5];
plot3(ax2, xyz(1), xyz(2), xyz(3), 'o', 'MarkerSize', 14)
grid(ax2, 'on')
axis(ax2, 'equal')
xlim(ax2, [0,1])
ylim(ax2, [0,1])
zlim(ax2, [0,1])
ax2.XRuler.FirstCrossoverValue = xyz(2); % y
ax2.YRuler.FirstCrossoverValue = xyz(1); % x
ax2.ZRuler.FirstCrossoverValue = xyz(1); % x
ax2.ZRuler.SecondCrossoverValue = xyz(2); % y
title(ax2, 'Origin on XY plane')
% Axis labels will also move with the axes
% xlabel(ax2, 'X axis')
% ylabel(ax2, 'Y axis')
% zlabel(ax2, 'Z axis')
If all you're looking for is reference lines without the ticks, you can use xline() and yline() to add the x and y ref lines. There is no zline() so you'll have to use plot3 something related. Note that xline and yline lines will update when the axis limits change but that won't happen with the plot3 line. Also note that the xline and yline lines are partially transparent by default. You can set the alpha levels to 1 to remove transparency or you can set the transparency of the z-line to match the x & y lines by adding a 4th value to the RGB color (ie, set(..., 'Color', [0 0 0 .4]))
ax = gca();
xyz = [.5, .5, .5];
plot3(ax, xyz(1), xyz(2), xyz(3), 'o', 'MarkerSize', 14)
grid(ax, 'on')
axis equal
xlim(ax, [0,1])
ylim(ax, [0,1])
zlim(ax, [0,1])
xline(ax, xyz(1), 'k--', 'XAxis') % see xline properties to control label placement
yline(ax, xyz(2), 'k--', 'YAxis')
plot3(ax, xyz([1,1]), xyz([2,2]), zlim(ax), 'k--')
title(ax, 'line objects')
  댓글 수: 5
Sim 2021년 8월 23일
Chapeau! I am really confident that all these examples will be very useful to the Matlab community! Many many thanks! :)

댓글을 달려면 로그인하십시오.

추가 답변(0개)

Community Treasure Hunt

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

Start Hunting!

Translated by