小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

【Matlab】去除圖片周圍空白區(qū)域(plot subplot)

 聽語思祈 2018-04-13

1. 原理

figure如同一張畫布,axes是坐標(biāo)軸用來控制所畫圖的位置和大小。

在matlab的幫助文檔中Graphics->Formatting and Annotation->Coordinate System->Properties->Axes Properties有axes的屬性,在Location and Size中可以看到主要有:

Position

TightInset

OuterPosition

下圖是三者的關(guān)系,OuterPosition是外邊框(紅色虛線),Position是內(nèi)邊框(綠色實線),TightInset是有效邊界(藍色)與Position之間的部分(理解它才能自定義axes)。

圖片引自matlab幫助手冊。

2. plot畫出來的圖的空白邊緣消除

Plot畫出來的為單一的一張圖,與之對應(yīng)的是subplot,在一個figure中畫多個圖。消除Plot的空白區(qū)域有3種方法。

2.1

加一句命令即可:

set(gca,'LooseInset',get(gca,'TightInset'))
  • 1

去除的不是很完全。

2.2

加一句命令即可:

set(gca,'looseInset',[0 0 0 0])
  • 1

2.3

第3種要麻煩一些,我把它寫成了一個函數(shù),方便調(diào)用,如果您有需要,只需要在您的代碼中使用該函數(shù)即可:

% RemovePlotWhiteArea: 去除Plot畫的圖的空白部分
% 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的原點移到到TightInset的原點
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é)果

測試代碼:

% 去除一張圖片周圍的空白區(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畫出來的圖的空白邊緣消除

3.1 code

subplot以子圖的形式畫多幅圖,所以有多少axes需要控制,比起plot要復(fù)雜一些。

原理是一樣的,先把每個子圖的位置和大小定下來,再設(shè)置每個子圖里面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ù)
%
% 注意:使用如下語句,print保存圖片的時候使其按照設(shè)置來保存,否則修改無效
% 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的原點移到到TightInset的原點
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é)果

測試代碼

% 去除subplot畫出來的圖的周圍空白部分
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出來的與屏幕顯示大小相同
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列左邊還是有一點點空白,我不能消除了,對axes的原理還是有疑問的,暫時這樣吧。

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多