IT博客汇
  • 首页
  • 精华
  • 技术
  • 设计
  • 资讯
  • 扯淡
  • 权利声明
  • 登录 注册

    [原][Matlab] 如何将axes填充整个Figure

    bendanban发表于 2016-06-01 10:48:08
    love 0

    简介

    在写文章的时候,时常用到Matlab来绘制图形,以更好的说明自己的想法。一般的方法是将Matlab绘制的figure另存为png或者jpeg格式的图像然后将这些图像放到自己的文档中。不管是放到word里还是用过Latex来排版,都面临一个问题:Matlab绘制的区域在整个图像的中间区域且与边缘相差一些像素。我们可以手动剪切这些多余的边缘,但是,如果图像变多了,或者需要重新绘制图形了,每次都手动操作确实让人反感。这里我们用在绘制的时候加一段程序来将我们需要的图形区域填充整个图像,并用程序自动保存图像。

    程序演示

    写一段程序来演示一下这个功能。

    for i = 1:4
      % plot a figure
      figure;
      x = normrnd(0, 3, [100, 3]);
      hist(x);  
      legend({'$C_1$', '$C_2$', '$C_3$'}, 'Interpreter', 'latex', 'Location', 'NorthEast');
      xlabel('\textbf{Variabel} $x$', 'Interpreter', 'latex');
      ylabel('\textbf{Count}', 'Interpreter', 'latex');
      title(['Figure ', num2str(i)]);
    
      % specify figure size
      af = gcf;
      af.Position(3) = af.Position(3)*0.7;
      af.Position(4) = af.Position(4)*0.5;
    
      % expand axes to fill figure
      ax = gca;
      outerpos = ax.OuterPosition;
      ti = ax.TightInset; 
      left = outerpos(1) + ti(1);
      bottom = outerpos(2) + ti(2);
      ax_width = outerpos(3) - ti(1) - ti(3);
      ax_height = outerpos(4) - ti(2) - ti(4);
      ax.Position = [left bottom ax_width ax_height];  
    
      % Specify Page Size
      af.PaperPositionMode = 'auto';
      fig_pos = af.PaperPosition;
      af.PaperSize = [fig_pos(3) fig_pos(4)];  
    
      % save the figure 
      print(af, ['Figure_', num2str(i), '.png'], '-dpng');
    end

    结果如下图所示:

    这里写图片描述
    这里写图片描述
    这里写图片描述
    这里写图片描述

    上面的图像是直接将保存后的图像上传的,四个图像的尺寸完全相同。并且没有多余的空白边缘,无需进一步剪切图像。



沪ICP备19023445号-2号
友情链接