How can I rotate the axes' labels parallel to the orientation of the axes?

83 ビュー (過去 30 日間)
Navid
Navid 2023 年 11 月 23 日
コメント済み: Navid 2023 年 11 月 28 日
Hi. I would like to know how to rotate labels and keep them centered with their orientation. Please see Fig 1. This figure is the default view output using the following syntax.
xlabel('x','Rotation',35)
ylabel('y','Rotation',-35)
However, in Fig 2, the plot view is set to:
view([73 24])
Is there a solution to rotate the labels parallel to the orientation of the axes by changing the view values? In this case, the 'Rotation' value (in this example, 35 and -35) changes as a function of the 'View' value (i.e., 73 and 24). Thank you so much for your time and consideration.
[X,Y,Z] = peaks(25);
figure
subplot(1,2,1)
surf(X,Y,Z);
xlabel('x','Rotation',35)
ylabel('y','Rotation',-35)
zlabel('z')
title('Fig.1')
subplot(1,2,2)
surf(X,Y,Z);
xlabel('x')
ylabel('y')
zlabel('z')
view([73 24])
title('Fig.2')

採用された回答

Bruno Luong
Bruno Luong 2023 年 11 月 24 日
編集済み: Bruno Luong 2023 年 11 月 24 日
Try this (the letters of label are rotated but the aspect ratio remain constant so they can be read easily but are not look like "projected" on the xy plane)
The angles depend on figure/axes aspect ratio, so if you resize them you have to compute the angle again.
[X,Y,Z] = peaks(25);
fig = figure(1);
clf(fig);
t = tiledlayout(2,2);
for k = 1:prod(t.GridSize)
ax = nexttile(t);
surf(ax, X, Y, Z);
%axis(ax, 'equal')
view(ax, rand*360, 40); % random azimuth
set(ax,'unit','pixel');
axPosition = ax.Position;
dx = axPosition(3);
dy = axPosition(4);
[xx, xy] = ScreenProjection(ax, xlim(ax), [0 0], [0 0]);
thetax = atan(diff(xy)/diff(xx)*dy/dx);
xlabel('here is the x-label', 'Rotation', rad2deg(thetax))
[xx, xy] = ScreenProjection(ax, [0 0], ylim(ax), [0 0]);
thetay = atan(diff(xy)/diff(xx)*dy/dx);
ylabel('here is the y-label', 'Rotation', rad2deg(thetay))
zlabel('z')
end
% https://www.mathworks.com/matlabcentral/answers/430790-how-can-i-get-the-screen-coordinates-from-perspective-projection?s_tid=srchtitle
function [xcam, ycam] = ScreenProjection(ax, X, Y, Z)
dataRatio = get(ax, 'DataAspectRatio');
matrixRescale = diag(1./dataRatio);
CT = get(ax,'CameraTarget');
CP = get(ax,'CameraPosition');
CU = get(ax,'CameraUpVector');
cadeg = get(ax,'CameraViewAngle');
CT = CT(:);
CP = CP(:);
CU = CU(:);
ca = cadeg*pi/180;
%
c = CT-CP;
d = norm(c);
c = c / d;
u = CU - dot(c, CU)*c;
u = u/norm(u);
p = cross(c,u);
R=[c,p,u];
XYZ = [X(:),Y(:),Z(:)]';
CPU = R'*(matrixRescale*(XYZ-CP));
XYcam = CPU(2:3,:)./CPU(1,:);
xcam = XYcam(1,:);
ycam = XYcam(2,:);
end
  9 件のコメント
Bruno Luong
Bruno Luong 2023 年 11 月 28 日
Disapointly the authority's answer of this thread explains the reason of the flaw and unable to suggest a satisfactition way to center label to axe position, beside "do it manually".
When I have time I'll make a code to position and center the labels.
Navid
Navid 2023 年 11 月 28 日
Thank you very much for your kindness and for taking the time to help me. Your assistance is greatly appreciated.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeGraphics Object Programming についてさらに検索

製品


リリース

R2016b

Community Treasure Hunt

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

Start Hunting!

Translated by