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

42 vues (au cours des 30 derniers jours)
Navid
Navid le 23 Nov 2023
Commenté : Navid le 28 Nov 2023
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')

Réponse acceptée

Bruno Luong
Bruno Luong le 24 Nov 2023
Modifié(e) : Bruno Luong le 24 Nov 2023
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 commentaires
Bruno Luong
Bruno Luong le 28 Nov 2023
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 le 28 Nov 2023
Thank you very much for your kindness and for taking the time to help me. Your assistance is greatly appreciated.

Connectez-vous pour commenter.

Plus de réponses (0)

Produits


Version

R2016b

Community Treasure Hunt

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

Start Hunting!

Translated by