Rotate ylabel and keep centered

631 views (last 30 days)
pxg882
pxg882 on 7 Mar 2016
Edited: Adam Danz on 19 Mar 2023
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 Comments
Geoff Hayes
Geoff Hayes on 7 Mar 2016
Edited: Geoff Hayes on 7 Mar 2016
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?
pxg882
pxg882 on 7 Mar 2016
Using R2015a.
Here's my test case...
x = [0, 10];
y = x / 10;
figure
plot(x,y)
xlabel('x')
ylabel('y')
set(get(gca,'ylabel'),'rotation',0)

Sign in to comment.

Answers (3)

Geoff Hayes
Geoff Hayes on 7 Mar 2016
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
Star Strider on 7 Mar 2016
Edited: Star Strider on 7 Mar 2016
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 Comments
Rik
Rik on 28 Mar 2017
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') )

Sign in to comment.


Adam Danz
Adam Danz on 17 Mar 2023
Edited: Adam Danz on 19 Mar 2023
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.

Tags

Community Treasure Hunt

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

Start Hunting!