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

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

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

Here's a demo on this topic that I wrote for another purpose and thought I'd share. It produces two axes and programmatically pan the identical data & text in both axes but only one of the axes contains text with the 'Clipping' set 'on'.
clf()
s(1) = subplot(1,3,1);
x = rand(1,20)*2;
y = rand(1,20)*2;
plot(x,y, 'm.')
axis square
labs = cellstr(char(randi([97,122],20,1)));
text(x,y,labs,'Clipping','on')
title('Clipping on')
s(2) = subplot(1,3,3);
plot(x,y,'m.')
axis square
text(x,y,labs,'Clipping','off')
title('Clipping off')
set(s,'XTickLabel',[],'YTickLabel',[])
% Pan plots until user closes figure
fh = gcf();
xl = xlim(s(1));
yl = ylim(s(2));
th = 0:pi/50:2*pi;
xc = .8*cos(th);
yc = .8*sin(th);
while ishghandle(fh)
for i = 1:numel(xc)
if ~ishghandle(fh)
continue
end
xlim(s(1),xl-xc(i))
ylim(s(1),yl-yc(i))
xlim(s(2),xl-xc(i))
ylim(s(2),yl-yc(i))
drawnow();
end
end
Hmmm... it is as-if the letters are anchored to their top left corner and the clipping is done according to whether that coordinate is inside the axes.
It appears that clipping considers the text position, not its extent or alignment. The text is clipped only if its anchor is outside of the axis limits.
figure()
plot([1 .5 0 .5], [.5 1 .5 0], 'rx', 'MarkerSize', 10, 'LineWidth',1)
text(1, .5, 'Example 1', 'Clipping', 'on', 'Horiz', 'Left', 'Vert', 'middle')
text(.5, 1, 'Example 2', 'Clipping', 'on', 'Horiz', 'center', 'Vert', 'bottom')
text(0, .5, 'Example 3', 'Clipping', 'on', 'Horiz', 'RIght', 'Vert', 'middle')
text(.5, 0, 'Example 4', 'Clipping', 'on', 'Horiz', 'center', 'Vert', 'top')
xlim([0,1])
ylim([0,1])
set(gca,'xticklabel',[],'yticklabel',[])
Now decrease axis range by 0.002.
xlim([.001, .999])
ylim([.001, .999])
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

Products

Community Treasure Hunt

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

Start Hunting!