Why do text objects on plots move outside axes boundaries when using the pan functionality?

62 views (last 30 days)
A text object, placed on a plot or a figure using the TEXT command, moves outside the axes boundaries when the figure is panned horizontally or vertically. While the rest of the image or graph gets clipped at the ends of the axes while panning, the text objects do not.
For example, the following code will plot a graph, place a text object on the graph and set the pan mode to on.
figure; plot(1:10);
text(5, 5, 'Sample Text');
pan on;
When this figure is panned using the mouse, the text object is visible outside of the axes boundaries whereas the rest of the graph conforms to these axes boundaries.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 25 Aug 2010
Graphical objects have a 'Clipping' property which MATLAB uses to clip area graphs to the axes plot box. By default, while the clipping mode is 'on' for lines, surfaces, and patches, it is 'off' for text objects. Setting this clipping mode to 'on' will result in text objects being clipped to the axes box plot during pan.
Thus, when placing a text object on a plot, retrieve its handle as follows.
figure; plot(1:10);
h = text(5, 5, 'Sample Text');
pan on;
The clipping mode can be switched on by using the SET command as follows.
set (h, 'Clipping', 'on');
  5 Comments
Adam Danz
Adam Danz on 30 Mar 2022
I've recently learned that, for text objects, setting the axes' ClippingStyle to rectangle allows for proper text clipping.
figure()
tiledlayout(1,2)
ax1 = nexttile;
text(ax1,0.65,0.75,'Long String name','Color','b','Clipping','on')
xlim(ax1,[0,1])
box(ax1, 'on')
axis(ax1,'square')
title(ax1,'ClippingStyle: 3dbox')
subtitle(ax1,'Clipping: on')
ax2 = nexttile;
text(ax2,0.65,0.75,'Long String name','Color','b','Clipping','on')
xlim(ax2,[0,1])
box(ax2,'on')
axis(ax2,'square')
ax2.ClippingStyle = 'rectangle';
title(ax2,'ClippingStyle: rectangle')
subtitle(ax2,'Clipping: on')

Sign in to comment.

More Answers (0)

Categories

Find more on Labels and Annotations in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!