Remove deadspace or increase size of figure in subplot

后端 未结 1 2017
心在旅途
心在旅途 2021-01-03 06:30

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 de

相关标签:
1条回答
  • 2021-01-03 07:00

    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
    

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

    Removing dead space for only one axes

    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)
    

    Which gives you the following figure:

    Squeeze axes for only one axes

    Removing dead space for multiple axes

    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
    

    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
    

    Gives you:

    builtin subplot

    Removing its deadspace:

    squeeze_axes(handles)
    

    squeeze axes

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

    0 讨论(0)
提交回复
热议问题