Rotate ylabel and keep centered
조회 수: 401 (최근 30일)
이전 댓글 표시
Hi,
Is there anyway to rotate the ylabel on a plot and ensure that the label is still centered on the axis?
Using
set(get(gca,'YLabel'),'Rotation',0)
I find that the label is 'shifted up' the y-axis after rotation.
Any help would be great.
Thanks.
댓글 수: 2
Geoff Hayes
2016년 3월 7일
편집: Geoff Hayes
2016년 3월 7일
I tried this with a very simple example on R2014a and the rotated label appeared as expected (in the centre of the y-axis). Which version of MATLAB are you using? What is the label that you are trying to rotate?
답변 (3개)
Geoff Hayes
2016년 3월 7일
Okay, so the 'y' label is just slightly "north" of 0.5 whereas when it was not rotated, the label was centred on 0.5. Try changing the vertical alignment for the label as
hYLabel = get(gca,'YLabel');
set(hYLabel,'rotation',0,'VerticalAlignment','middle')
This may do what you require.
Star Strider
2016년 3월 7일
편집: Star Strider
2016년 3월 7일
If you’re talking about 3D plots, no. There are some File Exchange contributions that apparently work well, judging by their ratings and the number of downloads they’ve gotten. (I’ve no experience with them.) Click on the link for a list of them.
EDIT — With your example to work from, I see the problem. It’s easily remedied by changing the 'VerticalAlignment' and 'HorizontalAlignment' properties:
x = [0, 10];
y = x / 10;
figure
plot(x,y)
xlabel('x')
ylabel('y')
ylh = get(gca,'ylabel');
gyl = get(ylh); % Object Information
ylp = get(ylh, 'Position');
set(ylh, 'Rotation',0, 'Position',ylp, 'VerticalAlignment','middle', 'HorizontalAlignment','right')
I’m not certain if setting the 'Position' property is absolutely necessary, but I did anyway. You can delete the ‘Object Information’ line. I put it in so I could see what properties were available to set.
This is in R2016a but should work with R2015b.
댓글 수: 2
Rik
2017년 3월 28일
Just in case someone else stumbles upon this answer in search of a rotation of 180 degrees (making the orientation 270 degrees): don't forget to account for the extent of the label. The label is not turning around the center, so setting 'Rotation' to 270 will let it overlap with the tick labels. This code will rotate the ylabel:
ylp = get(ylh, 'Position');
ext=get(y_h,'Extent');
set(y_h, 'Rotation',270, 'Position',ylp+[ext(3) 0 0])
I used this to rotate a second y-axis (created with with yyaxis('right') )
Adam Danz
2023년 3월 17일
편집: Adam Danz
2023년 3월 19일
Starting in MATLAB R2023a when you change the Rotation property of an axis label in a 2-D plot, the HorizontalAlignment and the VerticalAlignment properties of the label automatically update to prevent overlap between the label and the axes.
x = [0, 10];
y = x / 10;
figure
plot(x,y)
xlabel('x')
ylabel('ylabel','Rotation',0)
Prior to R2023a, set the VerticalAlignment and HorizontalAlignment properties of the axis label text object as demonstrated in the other answers in this thread.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Axis Labels에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!