1

My question is somehow related to this old one (Size of color bar including labels in Matlab R2014b) but the answer provided is not really satisfactory

I want to tightly fit horizontally two subplots with the first one having a colorbar which has its own label.

Here's what it should look like

tight subplots with colorbar

And here's the code used to generate it

h = figure('units','normalized','Position',[0,0,1,1]) ;

ax(1) = subplot(1,2,1);

hc = colorbar('EastOutside');

hc.Position(1) = ax(1).Position(1) + ax(1).Position(3)  ; % xfig + fig width

yl  = ylabel(hc,'$\sqrt{u^2 + v^2}$','interpreter','latex','Units','normalized','FontSize',16) ; % normalized with respect to the colorbar !

ext = yl.Extent ;

ti_lbrt(1,:) = ax(1).TightInset ; % Margins of the previous figure

ax(2) = subplot(1,2,2) ;

ti_lbrt(2,:) = ax(2).TightInset ; % Margins of the second figure


ax(2).Position(1) = ax(1).Position(1) + ax(1).Position(3) ...   lower right corner of previous figure
                    + ti_lbrt(1,3) ... right margin of the previous figure
                    + hc.Position(3) ... width of the colorbar
                    + ti_lbrt(2,1) ... % left margin of the new subplot
                    + ext(3)*hc.Position(3)...  width of the colorbar label
                    + 0.01 ; % Some arbitrary value; 

Why do I have to add that extra 0.01 value to make it not overlap ?

Thanks.

Community
  • 1
  • 1
Luc Rebillout
  • 11
  • 1
  • 6

0 Answers0