MATLAB Answers

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

3 views (last 30 days)
Sim
Sim on 21 Aug 2021
Edited: Adam Danz on 23 Aug 2021
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?

Accepted Answer

Adam Danz
Adam Danz on 22 Aug 2021
Edited: Adam Danz on 23 Aug 2021
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
Update
The undocumented Matlab blog shows how to move the location of axes origin
figure()
tiledlayout(1,2,'padding','none')
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]))
figure()
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')
hold(ax,'on')
plot3(ax, xyz([1,1]), xyz([2,2]), zlim(ax), 'k--')
title(ax, 'line objects')
  5 Comments
Sim
Sim on 23 Aug 2021
Chapeau! I am really confident that all these examples will be very useful to the Matlab community! Many many thanks! :)

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by