tight_subplot : creates "subplot" axes with adjustable gaps and margins ha = tight_subplot(Nh, Nw, gap, marg_h, marg_w) in: Nh number of axes in hight (vertical direction) Nw number of axes in width (horizontaldirection) gap gaps between the axes in normalized units (0...1) or [gap_h gap_w] for different gaps in height and width marg_h margins in height in normalized units (0...1) or [lower upper] for different lower and upper margins marg_w margins in width in normalized units (0...1) or [left right] for different left and right margins out: ha array of handles of the axes objects starting from upper left corner, going row-wise as in going row-wise as in Example: ha = tight_subplot(3,2,[.01 .03],[.1 .01],[.01 .01]) for ii = 1:6; axes(ha(ii)); plot(randn(10,ii)); end set(ha(1:4),'XTickLabel',''); set(ha,'YTickLabel','')
0001 function ha = tight_subplot(Nh, Nw, gap, marg_h, marg_w) 0002 0003 % tight_subplot : creates "subplot" axes with adjustable gaps and margins 0004 % 0005 % ha = tight_subplot(Nh, Nw, gap, marg_h, marg_w) 0006 % 0007 % in: Nh number of axes in hight (vertical direction) 0008 % Nw number of axes in width (horizontaldirection) 0009 % gap gaps between the axes in normalized units (0...1) 0010 % or [gap_h gap_w] for different gaps in height and width 0011 % marg_h margins in height in normalized units (0...1) 0012 % or [lower upper] for different lower and upper margins 0013 % marg_w margins in width in normalized units (0...1) 0014 % or [left right] for different left and right margins 0015 % 0016 % out: ha array of handles of the axes objects 0017 % starting from upper left corner, going row-wise as in 0018 % going row-wise as in 0019 % 0020 % Example: ha = tight_subplot(3,2,[.01 .03],[.1 .01],[.01 .01]) 0021 % for ii = 1:6; axes(ha(ii)); plot(randn(10,ii)); end 0022 % set(ha(1:4),'XTickLabel',''); set(ha,'YTickLabel','') 0023 0024 % Pekka Kumpulainen 20.6.2010 @tut.fi 0025 % Tampere University of Technology / Automation Science and Engineering 0026 0027 % BSD LICENSE : 0028 % Copyright (c) 2016, Pekka Kumpulainen 0029 % All rights reserved. 0030 % 0031 % Redistribution and use in source and binary forms, with or without 0032 % modification, are permitted provided that the following conditions are 0033 % met: 0034 % 0035 % * Redistributions of source code must retain the above copyright 0036 % notice, this list of conditions and the following disclaimer. 0037 % * Redistributions in binary form must reproduce the above copyright 0038 % notice, this list of conditions and the following disclaimer in 0039 % the documentation and/or other materials provided with the distribution 0040 % 0041 % THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 0042 % AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 0043 % IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 0044 % ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 0045 % LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 0046 % CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 0047 % SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 0048 % INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 0049 % CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 0050 % ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 0051 % POSSIBILITY OF SUCH DAMAGE. 0052 0053 0054 0055 if nargin<3; gap = .02; end 0056 if nargin<4 || isempty(marg_h); marg_h = .05; end 0057 if nargin<5; marg_w = .05; end 0058 0059 if numel(gap)==1; 0060 gap = [gap gap]; 0061 end 0062 if numel(marg_w)==1; 0063 marg_w = [marg_w marg_w]; 0064 end 0065 if numel(marg_h)==1; 0066 marg_h = [marg_h marg_h]; 0067 end 0068 0069 axh = (1-sum(marg_h)-(Nh-1)*gap(1))/Nh; 0070 axw = (1-sum(marg_w)-(Nw-1)*gap(2))/Nw; 0071 0072 py = 1-marg_h(2)-axh; 0073 0074 ha = zeros(Nh*Nw,1); 0075 ii = 0; 0076 for ih = 1:Nh 0077 px = marg_w(1); 0078 0079 for ix = 1:Nw 0080 ii = ii+1; 0081 ha(ii) = axes('Units','normalized', ... 0082 'Position',[px py axw axh], ... 0083 'XTickLabel','', ... 0084 'YTickLabel',''); 0085 px = px+axw+gap(2); 0086 end 0087 py = py-axh-gap(1); 0088 end 0089