主要内容

本页翻译不是最新的。点击此处可查看最新英文版本。

grid

R2026a

显示或隐藏坐标区网格线

描述

grid on 显示 gca 命令返回的当前坐标区的主网格线。主网格线从每个刻度线延伸。

示例

grid off 删除当前坐标区或图上的所有网格线。

示例

grid 切换改变主网格线的可见性。

grid minor 切换改变次网格线的可见性。次网格线出现在刻度线之间。并非所有类型的图都支持次网格线。

示例

grid(visibility) 通过指定 visibility 值来显示或隐藏网格。您可以将 visibility 指定为 "on"、"off"、逻辑值或 OnOffSwitchState 值。

grid(target,___) 使用 target 指定的坐标区或独立可视化,而不是使用当前坐标区。在所有其他参量之前指定 target。

示例

示例

全部折叠

显示正弦图的网格线。

x = linspace(0,10);
y = sin(x);
plot(x,y)
grid on

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

创建一个曲面图并删除网格线。

[X,Y,Z] = peaks; 
surf(X,Y,Z)
grid off

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

显示正弦图的主网格线和次网格线。

x = linspace(0,10);
y = sin(x);
plot(x,y)
grid on
grid minor

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

从 R2019b 开始,您可以使用 tiledlayout 和 nexttile 函数显示分块图。调用 tiledlayout 函数以创建一个 2×1 分块图布局。调用 nexttile 函数以创建坐标区对象 ax1 和 ax2。在每个坐标区中绘制数据。然后将 ax2 传递给 grid 函数,以在底部图中显示网格线。

x = linspace(0,10);
y1 = sin(x);
y2 = sin(3*x);
tiledlayout(2,1)

% Top plot
ax1 = nexttile;
plot(ax1,x,y1)

% Bottom plot
ax2 = nexttile;
plot(ax2,x,y2)
grid(ax2,'on')

Figure contains 2 axes objects. Axes object 1 contains an object of type line. Axes object 2 contains an object of type line.

输入参数

全部折叠

网格可见性,指定以下值之一:

  • "on" 或 "off" - 值 "on" 显示网格,"off" 隐藏网格。您还可以指定字符向量 'on' 或 'off'。

  • 数值或逻辑值 1 (true) 或 0 (false) - 值 1 或 true 显示网格,0 或 false 隐藏网格。 (自 R2024a 起)

  • matlab.lang.OnOffSwitchState 值 - 值 matlab.lang.OnOffSwitchState.on 显示网格,值 matlab.lang.OnOffSwitchState.off 隐藏网格。 (自 R2024a 起)

目标对象,指定为下列值之一:

  • 任何类型的坐标区对象:Axes、PolarAxes 或 GeographicAxes 对象。

  • 具有 GridVisible 属性的独立可视化。例如,heatmap 图具有 GridVisible 属性。

  • 属于同一类的坐标区或独立可视化的数组。要确定类,请使用 class 函数。

如果不指定目标,grid 函数将影响 gca 命令返回的图形对象。

提示

  • 使用地理坐标区时,某些地理坐标区属性会影响网格线的外观。下表列出了与网格线相关的部分地理坐标区属性。注意,GeographicAxes 对象不支持次网格线。

    GeographicAxes 属性描述
    Grid显示经度和纬度网格线
    LineWidth网格线、框轮廓和刻度线的线条宽度
    GridLineStyle网格线的线型
    GridColor网格线的颜色
    GridAlpha网格线的透明度

算法

grid 函数将以下图形对象属性设置为 'on' 或 'off':

  • XGrid、YGrid 和 ZGrid(使用笛卡尔 Axes 对象时)。

  • ThetaGrid 和 RGrid(使用 PolarAxes 对象时)。

  • Grid(使用 GeographicAxes 对象时)。

  • GridVisible(使用其他类型的图形对象时,如 HeatmapChart 对象)。

版本历史记录

在 R2006a 之前推出

全部展开