爱气象,爱气象家园! 

气象家园

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博登陆

只需一步, 快速开始

搜索
查看: 18923|回复: 6

[程序设计] MATLAB |【引用总结】关于去除图片空白区域或自定义图片位置的参考程序

[复制链接]

新浪微博达人勋

发表于 2021-7-10 17:40:50 | 显示全部楼层 |阅读模式

登录后查看更多精彩内容~

您需要 登录 才可以下载或查看,没有帐号?立即注册 新浪微博登陆

x
本帖最后由 淘气包马小跳 于 2021-8-5 14:50 编辑
声明:这是一篇引用总结类帖子

目的:使MATLAB输出的图片美观,便于论文使用。
工具:MATLAB R2015b。
资料来源:参考CSDN论坛、气象家园。

方法一:适用于plot去除图片空白的RemovePlotWhiteArea.m。
(来源:此为CSDN博主「itsc」的原创文章,原文链接:https://blog.csdn.net/shanchuan2012/article/details/53980288
  1. <font face="楷体, 楷体_GB2312"><font face="楷体, 楷体_GB2312">% RemovePlotWhiteArea: 去除Plot画的图的空白部分
  2. % RemovePlotWhiteArea(gca)
  3. % 输入
  4. % gca: axes句柄

  5. % author : TSC
  6. % time   : 2017-01-02
  7. % email  :

  8. function [] = RemovePlotWhiteArea(gca)
  9. % TightInset的位置
  10. inset_vectior = get(gca, 'TightInset');
  11. inset_x = inset_vectior(1);
  12. inset_y = inset_vectior(2);
  13. inset_w = inset_vectior(3);
  14. inset_h = inset_vectior(4);

  15. % OuterPosition的位置
  16. outer_vector = get(gca, 'OuterPosition');
  17. pos_new_x = outer_vector(1) + inset_x; % 将Position的原点移到到TightInset的原点
  18. pos_new_y = outer_vector(2) + inset_y;
  19. pos_new_w = outer_vector(3) - inset_w - inset_x; % 重设Position的宽
  20. pos_new_h = outer_vector(4) - inset_h - inset_y; % 重设Position的高

  21. % 重设Position
  22. set(gca, 'Position', [pos_new_x, pos_new_y, pos_new_w, pos_new_h]);</font></font></font>
复制代码

方法二:适用于subplot去除图片空白的RemoveSubplotWhiteArea.m。
(来源:此为CSDN博主「itsc」的原创文章,原文链接:https://blog.csdn.net/shanchuan2012/article/details/53980288
  1. % RemoveSubplotWhiteArea: 去除subplot周围的空白部分
  2. % RemoveSubplotWhiteArea(gca, sub_row, sub_col, current_row, current_col)
  3. % 输入
  4. % gca                  :axes句柄
  5. % sub_row     :subplot的行数
  6. % sub_col     :subplot的列数
  7. % current_row :当前列数
  8. % current_col :当前行数
  9. %
  10. % 注意:使用如下语句,print保存图片的时候使其按照设置来保存,否则修改无效
  11. % set(gcf, 'PaperPositionMode', 'auto');

  12. function [] = RemoveSubplotWhiteArea(gca, sub_row, sub_col, current_row, current_col)
  13. % 设置OuterPosition
  14. sub_axes_x = current_col*1/sub_col - 1/sub_col;
  15. sub_axes_y = 1-current_row*1/sub_row; % y是从上往下的
  16. sub_axes_w = 1/sub_col;
  17. sub_axes_h = 1/sub_row;
  18. set(gca, 'OuterPosition', [sub_axes_x, sub_axes_y, sub_axes_w, sub_axes_h]); % 重设OuterPosition

  19. % TightInset的位置
  20. inset_vectior = get(gca, 'TightInset');
  21. inset_x = inset_vectior(1);
  22. inset_y = inset_vectior(2);
  23. inset_w = inset_vectior(3);
  24. inset_h = inset_vectior(4);

  25. % OuterPosition的位置
  26. outer_vector = get(gca, 'OuterPosition');
  27. pos_new_x = outer_vector(1) + inset_x; % 将Position的原点移到到TightInset的原点
  28. pos_new_y = outer_vector(2) + inset_y;
  29. pos_new_w = outer_vector(3) - inset_w - inset_x; % 重设Position的宽
  30. pos_new_h = outer_vector(4) - inset_h - inset_y; % 重设Position的高

  31. % 重设Position
  32. set(gca, 'Position', [pos_new_x, pos_new_y, pos_new_w, pos_new_h]);
复制代码


方法三:自定义图片位置和大小的tight_subplot.m。
(来源:此为CSDN博主「五道口纳什」的原创文章,原文链接:https://blog.csdn.net/lanchunhui/article/details/49820721
  1. <font face="楷体, 楷体_GB2312">
  2. <font face="楷体, 楷体_GB2312">function [ha, pos] = tight_subplot(Nh, Nw, gap, marg_h, marg_w)

  3. % tight_subplot creates "subplot" axes with adjustable gaps and margins
  4. %
  5. % [ha, pos] = tight_subplot(Nh, Nw, gap, marg_h, marg_w)
  6. %
  7. %   in:  Nh      number of axes in hight (vertical direction)
  8. %        Nw      number of axes in width (horizontaldirection)
  9. %        gap     gaps between the axes in normalized units (0...1)
  10. %                   or [gap_h gap_w] for different gaps in height and width
  11. %        marg_h  margins in height in normalized units (0...1)
  12. %                   or [lower upper] for different lower and upper margins
  13. %        marg_w  margins in width in normalized units (0...1)
  14. %                   or [left right] for different left and right margins
  15. %
  16. %  out:  ha     array of handles of the axes objects
  17. %                   starting from upper left corner, going row-wise as in
  18. %                   subplot
  19. %        pos    positions of the axes objects
  20. %
  21. %  Example: ha = tight_subplot(3,2,[.01 .03],[.1 .01],[.01 .01])
  22. %           for ii = 1:6; axes(ha(ii)); plot(randn(10,ii)); end
  23. %           set(ha(1:4),'XTickLabel',''); set(ha,'YTickLabel','')

  24. % Pekka Kumpulainen 21.5.2012   @tut.fi
  25. % Tampere University of Technology / Automation Science and Engineering


  26. if nargin<3; gap = .02; end
  27. if nargin<4 || isempty(marg_h); marg_h = .05; end
  28. if nargin<5; marg_w = .05; end

  29. if numel(gap)==1;
  30.     gap = [gap gap];
  31. end
  32. if numel(marg_w)==1;
  33.     marg_w = [marg_w marg_w];
  34. end
  35. if numel(marg_h)==1;
  36.     marg_h = [marg_h marg_h];
  37. end

  38. axh = (1-sum(marg_h)-(Nh-1)*gap(1))/Nh;
  39. axw = (1-sum(marg_w)-(Nw-1)*gap(2))/Nw;

  40. py = 1-marg_h(2)-axh;

  41. % ha = zeros(Nh*Nw,1);
  42. ii = 0;
  43. for ih = 1:Nh
  44.     px = marg_w(1);
  45.    
  46.     for ix = 1:Nw
  47.         ii = ii+1;
  48.         ha(ii) = axes('Units','normalized', ...
  49.             'Position',[px py axw axh], ...
  50.             'XTickLabel','', ...
  51.             'YTickLabel','');
  52.         px = px+axw+gap(2);
  53.     end
  54.     py = py-axh-gap(1);
  55. end
  56. if nargout > 1
  57.     pos = get(ha,'Position');
  58. end
  59. ha = ha(:);</font>
  60. </font>
复制代码

简单说明:
tight_subplot(Nh, Nw, gap, marg_h, marg_w)
我们先来介绍参数的含义:Nh, Nw用法同subplot(row, col)表示行数和列数,gap(如[0.01, 0.1])表示子图之间垂直方向和水平方向的间隔,marg_h表示的是全部子图到figure上下边界的距离,marg_w则表示的是全部子图到figure左右边界的距离。



下面给出三个m文件:
tight_subplot.m (2.03 KB, 下载次数: 8)

方法三图片示例

方法三图片示例

评分

参与人数 1金钱 +10 贡献 +5 收起 理由
二爷名声在外 + 10 + 5

查看全部评分

密码修改失败请联系微信:mofangbao

新浪微博达人勋

 成长值: 32430
发表于 2021-7-10 22:24:29 | 显示全部楼层
可以把前两个方法的示意图也放上来
密码修改失败请联系微信:mofangbao
回复 支持 反对

使用道具 举报

新浪微博达人勋

发表于 2021-7-11 10:27:43 | 显示全部楼层
真的用心分享了
密码修改失败请联系微信:mofangbao
回复 支持 反对

使用道具 举报

新浪微博达人勋

 楼主| 发表于 2021-7-12 12:13:37 | 显示全部楼层
13top 发表于 2021-7-11 10:27
真的用心分享了

第一次分享,谢谢。
密码修改失败请联系微信:mofangbao
回复 支持 反对

使用道具 举报

新浪微博达人勋

发表于 2021-7-25 17:12:53 | 显示全部楼层
很棒的分享,也很实用,&#128077;
密码修改失败请联系微信:mofangbao
回复 支持 反对

使用道具 举报

新浪微博达人勋

 楼主| 发表于 2021-7-26 14:26:50 | 显示全部楼层
流云洗出天空蓝 发表于 2021-7-25 17:12
很棒的分享,也很实用,&#128077;

谢谢!!!
密码修改失败请联系微信:mofangbao
回复 支持 反对

使用道具 举报

新浪微博达人勋

发表于 2021-10-29 10:45:42 | 显示全部楼层
感谢,刚好是我需要的!
密码修改失败请联系微信:mofangbao
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册 新浪微博登陆

本版积分规则

Copyright ©2011-2014 bbs.06climate.com All Rights Reserved.  Powered by Discuz! (京ICP-10201084)

本站信息均由会员发表,不代表气象家园立场,禁止在本站发表与国家法律相抵触言论

快速回复 返回顶部 返回列表