Main Content

在绘图和图中进行裁剪

以下示例演示 MATLAB® 如何在绘图中使用裁剪以及如何控制裁剪。

什么是裁剪?

当对象的一部分延伸到绘图中坐标区的边界之外时即会出现裁剪的情况。在 MATLAB® 中,被裁剪的对象部分不会显示在屏幕上或打印输出中。默认情况下,MATLAB 会在轴范围处裁剪大多数对象。

关闭裁剪

在二维绘图中裁剪

二维绘图中会出现裁剪情况。例如,MATLAB 会裁剪以下二维绘图中的正弦波。

x = 0:pi/20:2*pi;
y = sin(x);
plot(x,y)
xlim([0,2*pi])
ylim([-0.9 0.9])

Figure contains an axes object. The axes object contains an object of type line.

您可以使用坐标区的 Clipping 属性来控制正弦波的裁剪。将裁剪设置为 "off" 可显示整个正弦波。

ax = gca;
ax.Clipping = "off";

Figure contains an axes object. The axes object contains an object of type line.

在三维绘图中裁剪

三维绘图中也会出现裁剪情况。例如,MATLAB 会裁剪以下三维绘图中的曲面。

figure
surf(peaks)
zlim([-4 4])

Figure contains an axes object. The axes object contains an object of type surface.

您可以使用坐标区的 Clipping 属性来控制坐标区的裁剪行为。将裁剪设置为 "off" 可显示整个曲面。

ax = gca;
ax.Clipping = "off";

Figure contains an axes object. The axes object contains an object of type surface.

控制裁剪样式

使用 ClippingStyle 属性来控制裁剪关于坐标区的工作方式。如果将 ClippingStyle 设置为 "3dbox",则 MATLAB 会将对象裁剪到由 xyz 轴的范围所定义的三维体。如果将 ClippingStyle 设置为 "rectangle",则 MATLAB 会在围绕 xyz 轴外侧绘制的虚构矩形处裁剪对象。以下两个图显示了这两种裁剪样式之间的差异。Two side-by-side surface plots. The plot with the ClippingStyle property set to "3dbox" shows the surface clipped at the axis limits. The plot with the ClippingStyle property set to "rectangle" shows the surface clipped beyond the axis limits.

控制文本裁剪

坐标区以外的对象具有 Clipping 属性。MATLAB 还可以在坐标区图框中裁剪文本对象。例如,绘制一些数据并添加文本描述。默认情况下,文本对象的 Clipping 属性设置为 "off",因此,如果缩放或平移,文本可能会超出轴范围。

figure
x = 1:10;
y = -10:10;
[X,Y] = meshgrid(x,y);
Z = X.^2-Y.^2;
surf(X,Y,Z);
ylim([-20.5 1.5])

t = text(1,7,60,"example");

Figure contains an axes object. The axes object contains 2 objects of type surface, text.

如果将文本的 Clipping 属性设置为 "on",则当文本对象的 (x,y,z) 位置(也称为锚点)位于坐标区图框之外时,完整文本将从视图中消失。

t.Clipping = "on";

Figure contains an axes object. The axes object contains 2 objects of type surface, text.

如果您要在坐标区图框处裁剪文本并显示部分文本,则还应将坐标区的 ClippingStyle 属性设置为 "rectangle"

ax = gca;
ax.ClippingStyle = "rectangle";

Figure contains an axes object. The axes object contains 2 objects of type surface, text.

裁剪和标记

只要每个数据点本身在绘图的 xy 轴范围内,裁剪就不影响在每个数据点上绘制的标记。MATLAB 会显示整个标记,即便它稍微超出坐标区的边界,也会显示出来。

p = plot(1:10,'*');
p.MarkerSize = 10;
axis([1 10 1 10])

Figure contains an axes object. The axes contains a line object which displays its values using only markers.

另请参阅

函数

属性

相关主题