天天看點

Matlab中添加網格線和編輯布局

目錄

顯示網格線

在特定方向顯示網格線

編輯網格線布局

修改網格線的可視外觀

此示例說明如何在圖形中添加網格線。它還說明了如何編輯網格線布局和修改網格線外觀。

顯示網格線

建立條形圖并顯示網格線。網格線顯示在刻度線處。

y = rand(10,1);
bar(y)
grid on
           
Matlab中添加網格線和編輯布局

在刻度線之間添加次網格線。

y = rand(10,1);
bar(y)
grid on
grid minor
           
Matlab中添加網格線和編輯布局

關閉所有網格線。

y = rand(10,1);
bar(y)
grid on
grid minor
grid off
           
Matlab中添加網格線和編輯布局

在特定方向顯示網格線

通過通路 

Axes

 對象并設定 

XGrid

YGrid

 和 

ZGrid

 屬性,可在特定方向顯示網格線。這些屬性可以設定為 

'on'

 或 

'off'

。建立二維繪圖且僅在 y 方向顯示網格線。

y = rand(10,1);
bar(y)
ax = gca;
ax.XGrid = 'off';
ax.YGrid = 'on';
           
Matlab中添加網格線和編輯布局

建立三維繪圖且僅在 z 方向顯示網格線。使用 

box on

 指令可顯示坐标區框輪廓。

[X,Y,Z] = peaks;
surf(X,Y,Z)
box on
ax = gca;
ax.ZGrid = 'on';
ax.XGrid = 'off';
ax.YGrid = 'off';
           
Matlab中添加網格線和編輯布局

編輯網格線布局

建立一個由随機資料組成的散點圖并顯示網格線。

x = rand(50,1);
y = rand(50,1);
scatter(x,y)
grid on
           
Matlab中添加網格線和編輯布局

網格線顯示在刻度線位置。通過更改刻度線位置可編輯網格線的布局。

x = rand(50,1);
y = rand(50,1);
scatter(x,y)
grid on
xticks(0:0.2:1)
yticks([0 0.5 0.8 1])
           
Matlab中添加網格線和編輯布局

修改網格線的可視外觀

更改區域圖網格線的顔色、線型和透明度。通過通路 

Axes

 對象修改網格線的外觀。然後設定與網格相關的屬性,例如 

GridColor

GridLineStyle

 和 

GridAlpha

 屬性。通過設定 

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';
           
Matlab中添加網格線和編輯布局

繼續閱讀