Rotate coordinate system axes

37 views (last 30 days)
broken_arrow
broken_arrow on 5 Nov 2021
Edited: broken_arrow on 8 Nov 2021
Sometimes it is handy to rename the x,y and z axis of a coordinate system in a plot, for example to comply with external conventions. Of course one can name the axes however they see fit, but commands like xticklabel always refer to the "native" axis name, which can be confusing (e. g. if the y axis is relabeled to "x", one now has to use yticklabel for their designated x axis etc.)
I've been (unsuccessfully) trying to figure out if it there is a way to actually rotate the default coordinate system (not the camera angle) to achieve a different orientation of the native axes. Does anyone know if this is possible? Consider the following example for clarification. The first plot has a default coordinate system while the second one has an alternative valid right hand system (which I could only achieve by reversing the z axis and swapping the y and z axis labels)
clear
close all
% Default CS
figure
ax1=axes;
hold on
grid on
view([-0.5 -1 1])
xlabel('x')
ylabel('y')
zlabel('z')
surf(ax1,[1 1;2 2],[1 2;1 2],[1 1;1 1])
title('default CS')
set(gcf,'Color','white')
% New CS
figure
ax2=axes;
ax2.ZDir = 'reverse';
hold on
grid on
view([-0.5 -1 1])
xlabel('x')
ylabel('z')
zlabel('y')
surf(ax2,[1 1;2 2],[1 2;1 2],[1 1;1 1])
title('new CS')
set(gcf,'Color','white')
  3 Comments
broken_arrow
broken_arrow on 8 Nov 2021
Edited: broken_arrow on 8 Nov 2021
Changing the camera properties actually seems to cause a problem with interactive rotation. Follow up question on the issue: https://mathworks.com/matlabcentral/answers/1581459-camera-up-vector-changes-when-rotating-interactively

Sign in to comment.

Accepted Answer

Matt J
Matt J on 6 Nov 2021
Edited: Matt J on 6 Nov 2021
The attached class permAxis3D might be what you want. It acts as a wrapper interface for an ordinary figure axis object, but permutes the axes definitions according to the constructor input, perm. Internally, it will swap in ZDir when you say YDir and zlabel when you say ylabel etc..., all out of sight and out of mind.
The slight downside is that you must pass in the object to any and all axis modification commands that you want to make, even when the axis you are working on is current. Example:
perm=[1,3,2]; %permute the y and z axes
ax=permAxis3D( axes ,perm); %construct the object
hold on
grid on
view([-0.5 -1 1])
xlabel(ax,'x')
ylabel(ax,'y')
zlabel(ax,'z')
surf(ax,[1 1;2 2],[1 1;1 1],[1 2;1 2])
ax.YDir='reverse';
set(gcf,'Color','white')
  2 Comments
Matt J
Matt J on 8 Nov 2021
Edited: Matt J on 8 Nov 2021
That should become a native Matlab feature ;)
I agree! Let's hope they see this thread.
Bear in mind, though, that this interface does not affect the way the data is stored in the surf object or other graphics primitives. I.e., if you were to do,
Hsurf = surf(ax,[1 1;2 2],[1 1;1 1],[1 2;1 2])
you would find that the ZData property of Hsurf contains what should be the YData and vice versa. A similar wrapper for graphics objects like Hsurf could be created to swap the roles of YData and ZData and other graphics properties.

Sign in to comment.

More Answers (0)

Tags

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!