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

31 vues (au cours des 30 derniers jours)
Allen Hammack
Allen Hammack le 7 Déc 2021
Commenté : Allen Hammack le 8 Déc 2021
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.

Réponse acceptée

Adam Danz
Adam Danz le 7 Déc 2021
Modifié(e) : Adam Danz le 7 Déc 2021
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 commentaire
Allen Hammack
Allen Hammack le 8 Déc 2021
Thank you so much, Adam! Your solution is working great!
I was struggling with this ALL DAY yesterday.

Connectez-vous pour commenter.

Plus de réponses (0)

Produits


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by