How to get rid of the white margin in MATLAB's saveas or print outputs

本文介绍如何在使用Matlab保存PDF格式的图表时去除周围多余的空白区域。通过调整图表边界、单位及纸张大小等参数实现紧凑布局。
Wan2.2-T2V-A5B

Wan2.2-T2V-A5B

文生视频
Wan2.2

Wan2.2是由通义万相开源高效文本到视频生成模型,是有​50亿参数的轻量级视频生成模型,专为快速内容创作优化。支持480P视频生成,具备优秀的时序连贯性和运动推理能力

website   http://tipstrickshowtos.blogspot.com/2010/08/how-to-get-rid-of-white-margin-in.html
When you want to include your plots/figures in your LaTeX document, you preferably want them in vector graphics format so that they scale/print nicely in your LaTeX PDF output. Fortunately, MATLAB can render vector graphics output if you choose to save your figure in PDF format (using either the ``saveas" or the ``print" command).

One annoying thing while output PDFs of your figures is that MATLAB automatically adds white space/margin around them. So you want to get rid of them. One option is to open the PDF file in a vector graphics program (like inkspace) and cut the unwanted white space. But this becomes a problem if you have a lot of figures. So, here is an automatic way to do it. It took me sometime to figure out how to do it: 

Let's go step by step. (Those who are not interested in the details and just want to get the code, skip to the bottom of this post. 


  1. Do your plots:

    x = -pi:.1:pi;
    y = sin(x);
    h=figure;
    plot(x,y)
  2. Make your figure boundaries tight:

    ti = get(gca,'TightInset')
    set(gca,'Position',[ti(1) ti(2) 1-ti(3)-ti(1) 1-ti(4)-ti(2)]);
  3. Now you have a tight figure on the screen but if you directly do saveas (or print), MATLAB will still add the annoying white space. To get rid of them, we need to adjust the ``paper size":

    set(gca,'units','centimeters')
    pos = get(gca,'Position');
    ti = get(gca,'TightInset');

    set(gcf, 'PaperUnits','centimeters');
    set(gcf, 'PaperSize', [pos(3)+ti(1)+ti(3) pos(4)+ti(2)+ti(4)]);
    set(gcf, 'PaperPositionMode', 'manual');
    set(gcf, 'PaperPosition',[0 0 pos(3)+ti(1)+ti(3) pos(4)+ti(2)+ti(4)]);
  4. Done! Now you can saveas your figure:

    saveas(h,'output.pdf');

    Or:

    print -dpdf output.pdf
If you want to understand the details of step 3, you need to look at ``Position" and ``TightInset" in  Axes Properties, and ``PaperSize", ``PaperPosition" in  Figure Properties.  

The code

Download here 

Usage:

saveTightFigure(h,'output.pdf');

Output format can be any format that MATLAB supports: jpeg, png, eps, pdf, etc.

Enjoy. 

您可能感兴趣的与本文相关的镜像

Wan2.2-T2V-A5B

Wan2.2-T2V-A5B

文生视频
Wan2.2

Wan2.2是由通义万相开源高效文本到视频生成模型,是有​50亿参数的轻量级视频生成模型,专为快速内容创作优化。支持480P视频生成,具备优秀的时序连贯性和运动推理能力

独立储能的现货电能量与调频辅助服务市场出清协调机制(Matlab代码实现)内容概要:本文围绕“独立储能的现货电能量与调频辅助服务市场出清协调机制”展开,提出了一种基于Matlab代码实现的优化模型,旨在协调独立储能系统在电力现货市场与调频辅助服务市场中的联合出清问题。文中结合鲁棒优化、大M法和C&CG算法处理不确定性因素,构建了多市场耦合的双层或两阶段优化框架,实现了储能资源在能量市场和辅助服务市场间的最优分配。研究涵盖了市场出清机制设计、储能运行策略建模、不确定性建模及求解算法实现,并通过Matlab仿真验证了所提方法的有效性和经济性。; 适合人群:具备一定电力系统基础知识和Matlab编程能力的研究生、科研人员及从事电力市场、储能调度相关工作的工程技术人员。; 使用场景及目标:①用于研究独立储能在多电力市场环境下的协同优化运行机制;②支撑电力市场机制设计、储能参与市场的竞价策略分析及政策仿真;③为学术论文复现、课题研究和技术开发提供可运行的代码参考。; 阅读建议:建议读者结合文档中提供的Matlab代码与算法原理同步学习,重点关注模型构建逻辑、不确定性处理方式及C&CG算法的具体实现步骤,宜在掌握基础优化理论的前提下进行深入研读与仿真调试。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值