[Matlab]去除subplot子图周围空白边界

在学术论文写作中,为了图形美观,常常需要对MATLAB生的图形加以修饰,其中空白边界的去除就是很重要的一项。本文所要介绍的方法主要用于去除subplot子图空白边界,当然,若一个figure中仅有一个图形也是可以用的。

具体方法:首先获得要进行去除空白边界的那个子图中的坐标句柄axis_handle,然后调用本文给出的函数Expand_axis_fill_figure( axis_handle )。

函数定义:

以下代码定义了去除空白边界的函数Expand_axis_fill_figure( axis_handle ),使用时,只需要首先在主M文件(函数)获得需要去除空白边界的子图的坐标句柄(无子图也可以),然后调用此函数便可完成去除空白边界的功能。调用时传递参数为主M文件(函数)中获得的坐标句柄。

%输入参数:要去除空白边界的那个绘图所对应的坐标句柄axis_handle
%输出参数:无
%保存为m文件,保存路径与主调函数在同一文件夹
function [ ] = Expand_axis_fill_figure( axis_handle )  %函数定义
% TightInset的位置
inset_vectior = get(axis_handle, '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(axis_handle, '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; % 重设Position的宽
pos_new_h = outer_vector(4) - inset_h - inset_y; % 重设Position的高

% 重设Position
set(axis_handle, 'Position', [pos_new_x, pos_new_y, pos_new_w, pos_new_h]);
%函数结束

实例分析

实例1
1、主调函数(文件):
clc;
clear all;
x = 0:0.01:10;
y1 = sin(x);
y2 = (sin(x)).^2;
y3 = cos(x);
y4 =(cos(x)).^3+(sin(x)).^3;
subplot(2,2,1)
h1 = plot(x,y1); %获得当前图形对像然句柄
h1_axis = gca;   %获得当前坐标的句柄
subplot(2,2,2)
h2 = plot(x,y2);  %获得当前图形对像然句柄
h2_axis = gca;   %获得当前坐标的句柄
subplot(2,2,3)
h3 = plot(x,y3);  %获得当前图形对像然句柄
h3_axis = gca;      %获得当前坐标的句柄
subplot(2,2,4)
h4 = plot(x,y4);  %获得当前图形对像然句柄
h4_axis = gca;      %获得当前坐标的句柄
%%%%%%%%%%%%%%%%%%%%%%%%
%坐标轴标注,第1幅图
xlabel(h1_axis,'y1=sin(x)波形图')
ylabel(h1_axis,'sin(x)幅值')
%坐标轴标注,第2幅图
xlabel(h2_axis,'y2=sin^2(x)波形图')
ylabel(h2_axis,'sin^2(x)幅值')
%坐标轴标注,第3幅图
xlabel(h3_axis,'y3=cos(x)波形图')
ylabel(h3_axis,'cos(x)幅值')
%坐标轴标注,第4幅图
xlabel(h4_axis,'y4=cos^3(x)+sin^3(x)波形图')
ylabel(h4_axis,'cos^3(x)+sin^3(x)幅值')
Expand_axis_fill_figure(h1_axis)
Expand_axis_fill_figure(h2_axis)
Expand_axis_fill_figure(h3_axis)
Expand_axis_fill_figure(h4_axis)
2、分析

以上代码中最后四行为函数调用,分别去除从左到右,从上到下4个子图的空白边界,以达到坐标轴填充满整个子图或figure。

调用前图形如下图:

《[Matlab]去除subplot子图周围空白边界》 函数调用前图形

调用后图形如下图:

《[Matlab]去除subplot子图周围空白边界》 去除空白边界后图形

实例2
1、主调函数(文件):
clc;
clear all;
fontsize = 10;               %定义字体大小
linewidth = 2;              %定义绘图线宽变量
linewidth_axis_legend = 1; %定义坐标轴、legend线宽变量
markersize = 8;             %定义绘图点标识符大小
L1 = 8;
L2 = 12;
contr = [0.06:0.02:0.14];
%透过式传感吸收度
absor_L1 = [0.0208 0.0753 0.1184 0.1473 0.1942];
absor_L2 = [0.0306 0.1099 0.1760 0.2414 0.2666];
plot(contr,absor_L1,':dr','LineWidth',linewidth,'MarkerSize',markersize);hold on
plot(contr,absor_L2,':db','LineWidth',linewidth,'MarkerSize',markersize)
%反射式传感吸收度
absor_L1 = [0.0408 0.1433 0.2211 0.2749 0.4020];
absor_L2 = [0.0615 0.2119 0.3184 0.4256 0.5515];
plot(contr,absor_L1,'-or','LineWidth',linewidth,'MarkerSize',markersize)
plot(contr,absor_L2,'-ob','LineWidth',linewidth,'MarkerSize',markersize)
xlabel('浓度 (mg/ml)','FontWeight','bold','FontSize',fontsize)
%xlabel('浓度 (mg/ml)','FontSize',fontsize)
ylabel('absorption','FontWeight','bold','FontSize',fontsize)
legend({'透射式吸收长度L=8cm','透射式吸收长度L=12cm','反射式吸收长度L=8cm','反射式吸收长度L=12cm'}, ...
       'FontWeight','bold','LineWidth',linewidth_axis_legend,'location','northwest')
set(gca,'LineWidth',linewidth_axis_legend,'FontSize',fontsize,'FontWeight','bold')
%set (gca,'position',[0.1,0.1,0.6,0.6] );
%set(gcf,'PaperUnits','centimeters','PaperPosition',[0 0 15 12])
%set(gcf,'unit','centimeters','position',[10 5 15 12])
set(gca,'XTick',[0:0.02:0.16]) %设定横坐标轴刻度步长及显示范围
set(gcf,'windowstyle','normal')%确保当前Figure没有docked,一般情况可不用该行代码
%set(gcf,'unit','centimeters','position',[10 5 12 9])  %设定Copy到Word中的图片的原始尺寸12*8cm,注意先把copy option设置为所见即所得状态。
%set(gca,'looseInset',[0 0 0 0]) %让坐标轴填满整个图形
Expand_axis_fill_figure(gca)
2、分析

以上代码中最后一行为函数调用,去除figure 1的空白边界,以达到坐标轴填充满整个figure 1。

调用前图形如下图

《[Matlab]去除subplot子图周围空白边界》 函数调用前图形

调用后图形如下图

《[Matlab]去除subplot子图周围空白边界》 去除空白边界后图形

结论

通过编写函数实现对指定的绘图(或子图)消除空白边界,避免了各类子图形空白边界消除的麻烦,代码具有通过性,可移植性强。

参考

【Matlab】去除图片周围空白区域(plot subplot)

    原文作者:愤怒的大菠菜
    原文地址: https://www.jianshu.com/p/0cacce5bc294
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞