How can I rotate the axes' labels parallel to the orientation of the axes?
52 次查看(过去 30 天)
显示 更早的评论
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')
0 个评论
采纳的回答
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
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.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Graphics Object Programming 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!