How Does the Size of a Plot Annotation Text Box Matter?
31 次查看(过去 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!