How Does the Size of a Plot Annotation Text Box Matter?

조회 수: 28 (최근 30일)
Allen Hammack
Allen Hammack 2021년 12월 7일
댓글: Allen Hammack 2021년 12월 8일
I'm trying to create a text annotation for a plot. I'm using the following link as a reference:
Specifically, I'm looking at this example:
figure
plot(1:10)
dim = [.2 .5 .3 .3];
str = 'Straight Line Plot from 1 to 10';
annotation('textbox',dim,'String',str,'FitBoxToText','on');
The ".3" and ".3" values in the "dim" definition define the length and width of the text box. I don't understand how to use those values to define the position of the text box because the starting x- and y-coordinates of the text box are set by the ".2" and ".5" values, respectively. Why does the size of the text box matter because the text box should fit the text for the annotation because the "FitBoxToText" option is used?
I want to be able to explicitly define the starting location of the text box.

채택된 답변

Adam Danz
Adam Danz 2021년 12월 7일
편집: Adam Danz 2021년 12월 7일
Welcome to the convoluted world of annotation panes.
You need to set the VerticalAlignment property to Bottom.
figure
plot(1:10)
dim = [.2 .5 .3 .3];
str = 'Straight Line Plot from 1 to 10';
h = annotation('textbox',dim,...
'String',str,...
'FitBoxToText','on',...
'VerticalAlignment','bottom');
Explanation
TL;DL The default text position within the annotation box starts in the upper-left of the textbox before the fit-box is applied. You have to indicate that the text should be in the lower-left of the box.
The default VerticalAlignment is "top" so let's take a look at what's going on when VerticalAlignment is Top and when the box is not fit to the text.
figure
plot(1:10)
dim = [.2 .5 .3 .3];
str = 'Straight Line Plot from 1 to 10';
h = annotation('textbox',dim,'String',str);
% Add axes that fills figure
ax2 = axes('Units','Normalized',...
'Position',[0 0 1 1],...
'visible','off', ...
'XLim', [0,1], ...
'YLim', [0,1]);
% Show the desired x and y position of the textbox
xline(ax2, dim(1))
yline(ax2,dim(2))
% Show desired textbox position and size
rectangle(ax2,'position', dim, 'linewidth', 2,'EdgeColor','r', 'LineStyle','--')
You can see why the text seems to be vertically displaced. The function applies the box-fit after the box is positioned.
Now let's set VerticalAlignment to Bottom,
figure
plot(1:10)
dim = [.2 .5 .3 .3];
str = 'Straight Line Plot from 1 to 10';
h = annotation('textbox',dim,'String',str,'VerticalAlignment', 'bottom');
% Add axes that fills figure
ax2 = axes('Units','Normalized',...
'Position',[0 0 1 1],...
'visible','off', ...
'XLim', [0,1], ...
'YLim', [0,1]);
% Show the desired x and y position of the textbox
xline(ax2, dim(1))
yline(ax2,dim(2))
% Show desired textbox position and size
rectangle(ax2,'position', dim, 'linewidth', 2,'EdgeColor','r', 'LineStyle','--')
Now when the fit-box is applied, the text is correctly positioned at the specified coordinate.
  댓글 수: 1
Allen Hammack
Allen Hammack 2021년 12월 8일
Thank you so much, Adam! Your solution is working great!
I was struggling with this ALL DAY yesterday.

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

추가 답변 (0개)

카테고리

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

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by