Rotate coordinate system axes

51 次查看(过去 30 天)
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