How can I align text to the ylabel in a plot?

조회 수: 20 (최근 30일)
Alessandro Mancari
Alessandro Mancari 2021년 8월 15일
댓글: Matt J 2021년 8월 15일
I would like to add text to a plot, in such a way that it maintains a fixed position relative to the ylabel. I tried with this code, but the text is not centered with respect to the label, and their relative position changes if I resize the figure (which I wouldn't want). Is there a way to do this?
figure
plot(1,1)
yh = ylabel('label')
set(yh,'Units','Normalized');
ylabPos = get(yh,'Position');
text(ylabPos(1),1,'text', 'Units','Normalized', 'HorizontalAlignment','center')
Alternatively, I could solve it by adding a second ylabel on the same axis, but I haven't found a way to do that either...
Thank you!

답변 (2개)

Star Strider
Star Strider 2021년 8월 15일
One approach —
figure
plot(1,1)
yh = ylabel('label');
set(yh,'Units','Normalized');
ylabPos = get(yh,'Position');
text(ylabPos(1),ylabPos(2),'text', 'Units','Normalized', 'HorizontalAlignment','center', 'Vert','top', 'Rotation',90)
If you want it above (to the left of) the ylabel, that would require re-defining ‘ylabPos(1)’.
.

Matt J
Matt J 2021년 8월 15일
편집: Matt J 2021년 8월 15일
figure
plot(1,1)
yh = ylabel('label');
set(yh,'Units','Normalized','HorizontalAlignment','center','VerticalAlignment','middle');
ylabPos = get(yh,'Position');
delete(yh);
text(ylabPos(1),ylabPos(2),'label', 'Units','Normalized', 'HorizontalAlignment',...
'center','VerticalAlignment','middle','Rotation',90)
text(ylabPos(1),1,'text', 'Units','Normalized', 'HorizontalAlignment','center','VerticalAlignment','middle')
  댓글 수: 2
Alessandro Mancari
Alessandro Mancari 2021년 8월 15일
Thanks, this is the configuration I had in mind, but with your code, if I extend the figure horizontally, 'label' and 'text' end up far from the y-axis. What I was trying to obtain is having them close to the axis no matter how I resize the figure (which is what the ylabel normally does). Or in short, I would need 'text' to behave like a label.
Matt J
Matt J 2021년 8월 15일
I don't think that's possible, unfortunately.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Signal Operations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by