Rotate coordinate system axes

62 ビュー (過去 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 Center および File Exchange2-D and 3-D Plots についてさらに検索

タグ

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by