Rotate coordinate system axes

조회 수: 51 (최근 30일)
broken_arrow
broken_arrow 2021년 11월 5일
편집: broken_arrow 2021년 11월 8일
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
broken_arrow
broken_arrow 2021년 11월 8일
In the end it doesn't really matter, but i'd like to preserve the default plot view for convenience ;)
broken_arrow
broken_arrow 2021년 11월 8일
편집: broken_arrow 2021년 11월 8일
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

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

채택된 답변

Matt J
Matt J 2021년 11월 6일
편집: Matt J 2021년 11월 6일
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
broken_arrow
broken_arrow 2021년 11월 8일
Ok, thank you. That should become a native Matlab feature ;)
Matt J
Matt J 2021년 11월 8일
편집: Matt J 2021년 11월 8일
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.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Graphics에 대해 자세히 알아보기

태그

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by