Rotate ylabel and keep centered

652 vues (au cours des 30 derniers jours)
pxg882
pxg882 le 7 Mar 2016
Modifié(e) : Adam Danz le 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 commentaires
Geoff Hayes
Geoff Hayes le 7 Mar 2016
Modifié(e) : Geoff Hayes le 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 le 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)

Connectez-vous pour commenter.

Réponses (3)

Geoff Hayes
Geoff Hayes le 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.
  1 commentaire
qaqcvc
qaqcvc le 19 Juil 2019
Thanks, it works for me.

Connectez-vous pour commenter.


Star Strider
Star Strider le 7 Mar 2016
Modifié(e) : Star Strider le 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 commentaires
Rik
Rik le 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') )
Viswambher Kambhampati
Viswambher Kambhampati le 10 Juin 2020
What is y_h here?

Connectez-vous pour commenter.


Adam Danz
Adam Danz le 17 Mar 2023
Modifié(e) : Adam Danz le 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!

Translated by