删除死区或增加子图中的图形大小 [英] Remove deadspace or increase size of figure in subplot

查看:112
本文介绍了删除死区或增加子图中的图形大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在matlab中遇到了问题。我输出一个图像,如示例所示,并通过print命令保存。我想要做的是以没有死空间的方式保存图像,即我想删除保存图像中的空白区域。

I have a problem in matlab. I output an image as shown in the example and save it by the print command. What I want to do is save the image in such a way that there is no deadspace i.e, I want to remove the white-space in the image saved.

示例:

黑色边框显示总数图像占据的区域。这真是一个浪费空间。我想删除它。我想要做的是:

The black border shows the total area the image is occupying. This is a really a wastage of space. I want to remove this. What I want to do is this:

我手动删除了图像周围的白色区域。但我想通过print命令自动执行此操作。可以吗?如果可能,标题仍然可以吗?即使它被删除然后也没关系,但如果标题可以保持更好。

I have manually removed the white area around the image. But I want to do this automatically by the print command. Can it be done? Also if possible can the title remain ? Even if it gets removed then also its okay but if the title can remain its better.

推荐答案

我在这里回复了这个其他主题并举例说明了如何在这里改进轴(子图)空间使用情况(搜索函数 kmeans_test 中的子函数 setCustomPlotArea

I answered this at this other topic and also gave an example of how to improve axes (subplot) space usage here (search for the subfunction setCustomPlotArea inside the function kmeans_test).

简短的回答是扩展轴位置占据整个数字如下:

The short answer is to spread axes position to occupy the whole figure as follows:

set(gca,'Position',[0 0 1 1]) % Make the axes occupy the whole figure

但如果你想保留ylabel,xlabel ,依此类推,您将不得不使用以下方法:

But if you want to keep ylabel, xlabel, and so on, you will have to use the following approach:

figure
plot([1 3])
title('Cool title')
ylabel('Ylabel yeah')
xlabel('Xlabel nah')
% Approach
tightPos=get(gca,'TightInset')
noDeadSpacePos = [0 0 1 1] + [tightPos(1:2) -(tightPos(1:2) + ...
  tightPos(3:4))];
set(gca,'Position',noDeadSpacePos)

这给出了下图:

我已经调整了setCustomPlotArea,如下所示:

I have adapted the setCustomPlotArea as follows:

function squeeze_axes(handles)
%
% squeeze_axes(handles) Squeeze axes to remove dead space.
%
%   Inputs:
%
% -> handles: the subplot axes handles organized as a grid. I.e.
% handles(1,1) is the axes in the first line and first column, whereas
% handles(4,4) is the axes in the forth line and forth column.
%

% - Creation Date: Mon, 16 Sep 2013 
% - Last Modified: Tue, 17 Sep 2013 
% - Author(s): 
%   - W.S.Freund <wsfreund_at_gmail_dot_com> 

% TODO: Make squeeze axes compatible with axes that occupy multiple
% subplot places.

nHorSubPlot =  size(handles,2);
nVertSubPlot = size(handles,1);

subplotWidth = 1/nHorSubPlot;
subplotHeight = 1/nVertSubPlot;

botPos = linspace(1-subplotHeight,0,nVertSubPlot);
leftPos = linspace(0,1-subplotWidth,nHorSubPlot);

for curLine=1:nVertSubPlot
  for curColumn=1:nHorSubPlot
    curAxes = handles(curLine,curColumn);
    if curAxes 
      % Set OuterPosition to occupy as most space as possible
      curAxesOuterPos = [leftPos(curColumn) botPos(curLine) subplotWidth ...
        subplotHeight];
      set(curAxes,'OuterPosition',curAxesOuterPos);
      % Remove dead space inside subplot border:
      curAxesTightPos=get(curAxes,'TightInset');
      noDeadSpacePos = curAxesOuterPos + [curAxesTightPos(1:2) ...
        -(curAxesTightPos(1:2) + curAxesTightPos(3:4))];
      set(curAxes,'Position',noDeadSpacePos)
    end
  end                                                         
end                                                           

end

绘制常用的matlab子图函数如下:

Ploting the common matlab subplot function as follows:

figure
nLines = 2;
nColumns = 3;
handles = zeros(nLines,nColumns)
for line = 1:nLines
  for column = 1:nColumns
    handles(line,column)=subplot(nLines,nColumns,column+(line-1)*nColumns);
    plot([line column]);
    title(sprintf('Cool title (%d,%d)',line,column))
    ylabel(sprintf('Ylabel yeah (%d,%d)',line,column))
    xlabel(sprintf('Xlabel nah (%d,%d)',line,column))
  end
end

给你:

删除死区:

squeeze_axes(handles)

作为练习,我让你的轴有一个占据网格中多个空间的情况。

As an exercise I let the case where you have an axes occupying more than one space in the grid.

这篇关于删除死区或增加子图中的图形大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆