1. 原理
figure如同一張畫(huà)布,axes是坐標(biāo)軸用來(lái)控制所畫(huà)圖的位置和大小。
在matlab的幫助文檔中Graphics->Formatting and Annotation->Coordinate System->Properties->Axes Properties有axes的屬性,在Location and Size中可以看到主要有:
Position
TightInset
OuterPosition
下圖是三者的關(guān)系,OuterPosition是外邊框(紅色虛線),Position是內(nèi)邊框(綠色實(shí)線),TightInset是有效邊界(藍(lán)色)與Position之間的部分(理解它才能自定義axes)。

圖片引自matlab幫助手冊(cè)。
2. plot畫(huà)出來(lái)的圖的空白邊緣消除
Plot畫(huà)出來(lái)的為單一的一張圖,與之對(duì)應(yīng)的是subplot,在一個(gè)figure中畫(huà)多個(gè)圖。消除Plot的空白區(qū)域有3種方法。
2.1
加一句命令即可:
set(gca,'LooseInset',get(gca,'TightInset'))
去除的不是很完全。
2.2
加一句命令即可:
set(gca,'looseInset',[0 0 0 0])
2.3
第3種要麻煩一些,我把它寫(xiě)成了一個(gè)函數(shù),方便調(diào)用,如果您有需要,只需要在您的代碼中使用該函數(shù)即可:
% RemovePlotWhiteArea: 去除Plot畫(huà)的圖的空白部分
% RemovePlotWhiteArea(gca)
% 輸入
% gca: axes句柄
% author : TSC
% time : 2017-01-02
% email : 292936085#qq.com(將#替換為@)
function [] = RemovePlotWhiteArea(gca)
% TightInset的位置
inset_vectior = get(gca, 'TightInset');
inset_x = inset_vectior(1);
inset_y = inset_vectior(2);
inset_w = inset_vectior(3);
inset_h = inset_vectior(4);
% OuterPosition的位置
outer_vector = get(gca, 'OuterPosition');
pos_new_x = outer_vector(1) + inset_x; % 將Position的原點(diǎn)移到到TightInset的原點(diǎn)
pos_new_y = outer_vector(2) + inset_y;
pos_new_w = outer_vector(3) - inset_w - inset_x; % 重設(shè)Position的寬
pos_new_h = outer_vector(4) - inset_h - inset_y; % 重設(shè)Position的高
% 重設(shè)Position
set(gca, 'Position', [pos_new_x, pos_new_y, pos_new_w, pos_new_h]);
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
2.4 結(jié)果
測(cè)試代碼:
% 去除一張圖片周圍的空白區(qū)域
% *************************************************************************
remove_flag = 3; %1,2,3 任選一種查看效果
% -------------------------------------------------------------------------
x = 0:0.1:10;
y = sin(x);
figure('color', [0.8, 0.8, 0.8]); % 為區(qū)分邊界,將底色改為灰色
set(gcf, 'InvertHardCopy', 'off'); % 讓設(shè)置的背景色有效
plot(x,y);
title('sinx');
xlabel('x');
ylabel('y');
% 去除空白的第1種方式
if 1 == remove_flag
set(gca,'LooseInset',get(gca,'TightInset'))
end
% 去除空白的第2種方式
if 2 == remove_flag
set(gca,'looseInset',[0 0 0 0])
end
% 去除空白的第3種方式
if 3 == remove_flag
RemovePlotWhiteArea(gca);
end
set(gcf, 'PaperPositionMode', 'auto');
print(gcf, '-djpeg', '-r300', ['respic/', num2str(remove_flag), '.jpg']);
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
結(jié)果圖片:
原圖:

去除空白的第1種方式:

去除空白的第2種方式:

去除空白的第3種方式

3. subplot畫(huà)出來(lái)的圖的空白邊緣消除
3.1 code
subplot以子圖的形式畫(huà)多幅圖,所以有多少axes需要控制,比起plot要復(fù)雜一些。
原理是一樣的,先把每個(gè)子圖的位置和大小定下來(lái),再設(shè)置每個(gè)子圖里面axes的位置和大小,直接給出函數(shù):
% RemoveSubplotWhiteArea: 去除subplot周圍的空白部分
% RemoveSubplotWhiteArea(gca, sub_row, sub_col, current_row, current_col)
% 輸入
% gca :axes句柄
% sub_row :subplot的行數(shù)
% sub_col :subplot的列數(shù)
% current_row :當(dāng)前列數(shù)
% current_col :當(dāng)前行數(shù)
%
% 注意:使用如下語(yǔ)句,print保存圖片的時(shí)候使其按照設(shè)置來(lái)保存,否則修改無(wú)效
% set(gcf, 'PaperPositionMode', 'auto');
% author : TSC
% time : 2017-01-02
% email : 292936085#qq.com(將#替換為@)
function [] = RemoveSubplotWhiteArea(gca, sub_row, sub_col, current_row, current_col)
% 設(shè)置OuterPosition
sub_axes_x = current_col*1/sub_col - 1/sub_col;
sub_axes_y = 1-current_row*1/sub_row; % y是從上往下的
sub_axes_w = 1/sub_col;
sub_axes_h = 1/sub_row;
set(gca, 'OuterPosition', [sub_axes_x, sub_axes_y, sub_axes_w, sub_axes_h]); % 重設(shè)OuterPosition
% TightInset的位置
inset_vectior = get(gca, 'TightInset');
inset_x = inset_vectior(1);
inset_y = inset_vectior(2);
inset_w = inset_vectior(3);
inset_h = inset_vectior(4);
% OuterPosition的位置
outer_vector = get(gca, 'OuterPosition');
pos_new_x = outer_vector(1) + inset_x; % 將Position的原點(diǎn)移到到TightInset的原點(diǎn)
pos_new_y = outer_vector(2) + inset_y;
pos_new_w = outer_vector(3) - inset_w - inset_x; % 重設(shè)Position的寬
pos_new_h = outer_vector(4) - inset_h - inset_y; % 重設(shè)Position的高
% 重設(shè)Position
set(gca, 'Position', [pos_new_x, pos_new_y, pos_new_w, pos_new_h]);
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
3.2 結(jié)果
測(cè)試代碼
% 去除subplot畫(huà)出來(lái)的圖的周圍空白部分
x = 0:0.1:10;
y = sin(x);
figure('color', [0.8, 0.8, 0.8], 'position', [100, 100, 800,400]); % 為區(qū)分邊界,將底色改為灰色
set(gcf, 'InvertHardCopy', 'off'); % 讓設(shè)置的背景色有效
sub_row = 4; % 子圖行數(shù)
sub_col = 4; % 子圖列數(shù)
for i_row = 1 : sub_row
for j_col = 1 : sub_col
order = (i_row-1)*sub_col+j_col; % 子圖的順序
subplot(sub_row, sub_col, order);
plot(y);
title([num2str(i_row), num2str(j_col)]);
xlabel('x');
ylabel('y');
RemoveSubplotWhiteArea(gca, sub_row, sub_col, i_row, j_col); % 去除空白部分
end
end
set(gcf, 'PaperPositionMode', 'auto'); % 使print出來(lái)的與屏幕顯示大小相同
print(gcf, '-djpeg', '-r300', ['respic/sub', num2str(sub_row), num2str(sub_col), '.jpg']);
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
結(jié)果圖片
1行1列:

2行1列:

1行2列:

2行2列:

3行3列:

4行4列:

可以看到,3行3列和4行4列左邊還是有一點(diǎn)點(diǎn)空白,我不能消除了,對(duì)axes的原理還是有疑問(wèn)的,暫時(shí)這樣吧。
|