COMMON_COLOURBAR make a joint colourbar for several subplots a = common_colourbar(ha,img) adds a common colourbar to the subplots identified in the array of handles ha, passing the img input to eidors_colourbar for actual rendering, and returning a handle to a new parent axis of the colourbar (a). a = common_colourbar(ha, ...) uses all inputs beside ha to call eidors_colourbar. common_colourbar('clear',a) removes the colourbar previously added by a call to common_colourbar.
0001 function a = common_colourbar(ha,varargin) 0002 %COMMON_COLOURBAR make a joint colourbar for several subplots 0003 % a = common_colourbar(ha,img) adds a common colourbar to the subplots 0004 % identified in the array of handles ha, passing the img input to 0005 % eidors_colourbar for actual rendering, and returning a handle to a new 0006 % parent axis of the colourbar (a). 0007 % 0008 % a = common_colourbar(ha, ...) uses all inputs beside ha to call 0009 % eidors_colourbar. 0010 % 0011 % common_colourbar('clear',a) removes the colourbar previously added by 0012 % a call to common_colourbar. 0013 0014 % (C) 2013 Bartlomiej Grychtol. License: GPL version 2 or 3. 0015 % $Id: common_colourbar.m 4930 2015-05-08 13:01:30Z aadler $ 0016 0017 if ischar(ha) 0018 switch ha 0019 case 'UNIT_TEST' 0020 do_unit_test; 0021 return; 0022 case 'clear' 0023 img = varargin{1}; 0024 delete(img); 0025 otherwise 0026 error('huh?'); 0027 end 0028 end 0029 0030 fig = get(ha(1),'Parent'); 0031 ca = get(fig,'CurrentAxes'); 0032 %1. Figure out position limits 0033 x_min = inf; x_max = -inf; 0034 y_min = inf; y_max = -inf; 0035 for i = 1:length(ha) 0036 u = get(ha(i),'Units'); 0037 set(ha(i),'Units','points') 0038 pos = get(ha(i), 'Position'); 0039 x_min = min(x_min, pos(1)); 0040 x_max = max(x_max, pos(1)+pos(3)); 0041 y_min = min(y_min, pos(2)); 0042 y_max = max(y_max, pos(2)+pos(4)); 0043 set(ha(i),'Units',u); 0044 end 0045 %2. Create an axis that overlaps them all 0046 pos = [x_min y_min x_max-x_min y_max-y_min]; 0047 % this is how Matlab R2011a calculates space for the colorbar 0048 pp = get(gca,'Position'); 0049 sz = min(max(pp(3:4)*0.3,20),40); 0050 % so we pre-emptively extend the axis by this amount 0051 pos(3) = pos(3) + sz(1); 0052 0053 a = axes('Units','points','Position',pos); 0054 axis off 0055 0056 %3. Add eidors colorbar 0057 eidors_colourbar(varargin{:}); 0058 0059 %4. Minimize impact on the figure from user's perspective 0060 0061 % put the new axis behind everything else 0062 set(fig,'Children',circshift(get(fig,'Children'),-1)); 0063 0064 % return focus to the previous axes 0065 set(fig,'CurrentAxes',ca) 0066 0067 0068 0069 0070 function do_unit_test 0071 clf 0072 for i = 1:6 0073 h(i) = subplot(2,3,i); 0074 end 0075 common_colourbar(h([1 2 4 5]),2,0); 0076 common_colourbar(h(4:6),5,1); 0077 eidors_msg('CHECK GRAPHICS',0);