How Does the Size of a Plot Annotation Text Box Matter?
조회 수: 28 (최근 30일)
이전 댓글 표시
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.
댓글 수: 0
채택된 답변
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.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Annotations에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!