"plot" versus "text" command

3 次查看(过去 30 天)
Why does the following code:
x = [1, 2, 3, 4, 5];
y = [2, 4, 1, 5, 3];
labels = {'A', 'B', 'C', 'D', 'E'};
plot(x, y, 'LineStyle', 'none');
hold on;
text(x, y, labels, 'HorizontalAlignment', 'center', 'VerticalAlignment', 'middle');
result in something different than this code?:
x = [1, 2, 3, 4, 5];
y = [2, 4, 1, 5, 3];
labels = {'A', 'B', 'C', 'D', 'E'};
text(x, y, labels, 'HorizontalAlignment', 'center', 'VerticalAlignment', 'middle')

采纳的回答

Adam Danz
Adam Danz 2025-3-6
编辑:Adam Danz 2025-3-6
Text's property AffectAutoLimits is set to off by default. This means that the axes limits will not update if a text object is plotted outside of the axes limits. To auto-adjust the axes limits for text objects, include the name-value pair "AffectAutoLimits", "on" in the text(__) command.
x = [1, 2, 3, 4, 5];
y = [2, 4, 1, 5, 3];
labels = {'A', 'B', 'C', 'D', 'E'};
text(x, y, labels, ...
'AffectAutoLimits', 'on', ... % <-------------
'HorizontalAlignment', 'center', ...
'VerticalAlignment', 'middle')
When AffectAutoLimits is on, axes limits will be adjust to include the text anchor coordinates, not the full text extent. The text anchor is the coordinate that defines the text location but the text extent could extend outside of the axes limits. Auto-adjusting axes limits to include text extents is currently not possible .
Example:
figure
plot([-2 2],[1 1], 'rx')
text(-2, 1, 'LeftLabel', ...
'AffectAutoLimits', 'on', ...
'HorizontalAlignment', 'Center',...
'VerticalAlignment', 'bottom', ...
'Color','b')
text(2, 1, 'RightLabel', ...
'AffectAutoLimits', 'on', ...
'HorizontalAlignment', 'Center',...
'VerticalAlignment', 'bottom', ...
'Color','b')
box on

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Annotations 的更多信息

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by