Main Content

添加网格线和编辑布局

此示例说明如何在图形中添加网格线。它还说明了如何编辑网格线布局和修改网格线外观。

显示网格线

创建条形图并显示网格线。网格线显示在刻度线处。

y = rand(10,1);
bar(y)
grid on

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

在刻度线之间添加次网格线。

grid minor

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

关闭所有网格线。

grid off

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

在特定方向显示网格线

通过访问 Axes 对象并设置 XGridYGridZGrid 属性,可在特定方向显示网格线。这些属性可以设置为 'on''off'

创建二维绘图且仅在 y 方向显示网格线。

y = rand(10,1);
bar(y)
ax = gca;
ax.XGrid = 'off';
ax.YGrid = 'on';

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

创建三维绘图且仅在 z 方向显示网格线。使用 box on 命令可显示坐标区框轮廓。

[X,Y,Z] = peaks;
surf(X,Y,Z)
box on
ax = gca;
ax.ZGrid = 'on';
ax.XGrid = 'off';
ax.YGrid = 'off';

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

编辑网格线布局

创建一个由随机数据组成的散点图并显示网格线。

x = rand(50,1);
y = rand(50,1);
scatter(x,y)
grid on

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

网格线显示在刻度线位置。通过更改刻度线位置可编辑网格线的布局。

xticks(0:0.2:1)
yticks([0 0.5 0.8 1])

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

修改网格线的可视外观

更改区域图网格线的颜色、线型和透明度。通过访问 Axes 对象修改网格线的外观。然后设置与网格相关的属性,例如 GridColorGridLineStyleGridAlpha 属性。通过设置 Layer 属性可在绘图上显示网格线。

y = rand(10,1);
area(y)
grid on

ax = gca;
ax.GridColor = [0 .5 .5];
ax.GridLineStyle = '--';
ax.GridAlpha = 0.5;
ax.Layer = 'top';

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

另请参阅

函数

属性

相关主题