Main Content

指定坐标轴范围

您可以设置 x 轴、y 轴和 z 坐标轴范围,以控制数据在坐标区上的显示位置。您也可以更改 x 轴线和 y 轴线的显示位置(仅适用于二维绘图),或反转值沿每条轴递增的方向。

更改坐标轴范围

创建一个线图。使用 xlimylim 函数指定坐标轴范围。对于三维绘图,请使用 zlim 函数。将 [min max] 形式的二元素向量传递给函数。

x = linspace(-10,10,200); 
y = sin(4*x)./exp(x);
plot(x,y)
xlim([0 10])
ylim([-0.4 0.8])

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

使用半自动坐标轴范围

x 轴范围最大值设为 0,y 轴范围最小值设为 -1。其他范围则由 MATLAB 选择。对于自动计算的最小值或最小值范围,分别使用 -infinf 来表示。

[X,Y,Z] = peaks;
surf(X,Y,Z)
xlabel('x-axis')
ylabel('y-axis')
xlim([-inf 0]) 
ylim([-1 inf])

Figure contains an axes object. The axes object with xlabel x-axis, ylabel y-axis contains an object of type surface.

还原为默认范围

创建一个网格图并更改坐标轴范围,然后还原为默认范围。

[X,Y,Z] = peaks;
mesh(X,Y,Z)
xlim([-2 2])
ylim([-2 2])
zlim([-5 5])

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

xlim auto
ylim auto
zlim auto

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

反转坐标轴方向

通过设置 Axes 对象的 XDirYDir 属性,可控制 x 轴和 y 轴值递增的方向。这些属性可以设置为 'reverse''normal'(默认值)。使用 gca 命令可访问 Axes 对象。

stem(1:10)
ax = gca;
ax.XDir = 'reverse';
ax.YDir = 'reverse';

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

显示通过原点的轴线

默认情况下,x 轴和 y 轴沿坐标区的外边界显示。通过设置 Axes 对象的 XAxisLocationYAxisLocation 属性来更改轴线位置,以使轴线在原点 (0,0) 处交叉。将 XAxisLocation 设置为 'top''bottom''origin'。将 YAxisLocation 设置为 'left''right''origin'。这些属性仅适用于二维视图中的坐标区。

x = linspace(-5,5);
y = sin(x);
plot(x,y)

ax = gca;
ax.XAxisLocation = 'origin';
ax.YAxisLocation = 'origin';

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

删除坐标区框轮廓。

box off

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

另请参阅

函数

属性

相关主题