Matlab 删除死区或增加子批次中图形的大小
我在matlab中遇到了一个问题。我输出了一个如示例所示的图像,并通过print命令保存它。我想做的是以一种没有死区的方式保存图像,也就是说,我想删除保存的图像中的空白 例如: 黑色边框显示图像占用的总面积。这真是浪费空间。我想删除这个。我想做的是: 我已手动删除图像周围的白色区域。但我想通过print命令自动执行此操作。能做到吗?如果可能,标题还能保留吗?即使它被删除了,那也没关系,但如果标题能保持更好 我回答了这个问题,并给出了一个示例(在函数Matlab 删除死区或增加子批次中图形的大小,matlab,image-processing,subplot,Matlab,Image Processing,Subplot,我在matlab中遇到了一个问题。我输出了一个如示例所示的图像,并通过print命令保存它。我想做的是以一种没有死区的方式保存图像,也就是说,我想删除保存的图像中的空白 例如: 黑色边框显示图像占用的总面积。这真是浪费空间。我想删除这个。我想做的是: 我已手动删除图像周围的白色区域。但我想通过print命令自动执行此操作。能做到吗?如果可能,标题还能保留吗?即使它被删除了,那也没关系,但如果标题能保持更好 我回答了这个问题,并给出了一个示例(在函数kmeans\u test中搜索子函数setC
kmeans\u test
中搜索子函数setCustomPlotArea
)
简而言之,将轴的位置展开以占据整个图形,如下所示:
set(gca,'Position',[0 0 1 1]) % Make the axes occupy the whole figure
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
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
但如果要保留ylabel、xlabel等,则必须使用以下方法:
仅删除一个轴的死区
这将为您提供下图:
消除多轴的死区
我对setCustomPlotArea进行了如下调整:
set(gca,'Position',[0 0 1 1]) % Make the axes occupy the whole figure
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
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)
作为练习,我假设一个轴在网格中占据多个空间。@roni使用
OuterPosition
,但如果只有一个轴,它将不起作用。然后,您可以使用一个较小的百分比,即设置(gca,'Position',[0.08 0.08.84.84])
@roni当您有ylabel时,我将返回到一种更自动的方式来执行此操作。@roni欢迎使用x)。请关闭接受答案的线程。这个答案很好。我有一些大标签的问题,但这是一个很好的开始。我会这样做。我对FEX解决方案不太满意,这可能是最简单的。谢谢