Rotate ylabel and keep centered
655 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
Geoff Hayes
am 7 Mär. 2016
Bearbeitet: Geoff Hayes
am 7 Mär. 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?
Antworten (3)
Geoff Hayes
am 7 Mär. 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
am 7 Mär. 2016
Bearbeitet: Star Strider
am 7 Mär. 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 Kommentare
Rik
am 28 Mär. 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') )
Adam Danz
am 17 Mär. 2023
Bearbeitet: Adam Danz
am 19 Mär. 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.
0 Kommentare
Siehe auch
Kategorien
Mehr zu Axis Labels finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!