How to keep the subplot sizes unchanged after putting a colorbar

前端 未结 2 1185
长发绾君心
长发绾君心 2021-02-05 23:42

Let us say we have a 1-by-2 subplot and we plot some graphics inside as follows:

subplot(1,2,1)
surf(peaks(20))

subplot(1,2,2)
surf(peaks(20))

2条回答
  •  伪装坚强ぢ
    2021-02-05 23:50

    You could just extract the position of the first plot and use on the second. MATLAB automatically moves the colorbar to the right when rescaling.

    f1=figure(1);clf;
    s1=subplot(1,2,1);
    surf(peaks(20));
    
    s2=subplot(1,2,2);
    surf(peaks(20));
    hb = colorbar('location','eastoutside');
    
    %% # Solution:
    s1Pos = get(s1,'position');
    s2Pos = get(s2,'position');
    s2Pos(3:4) = [s1Pos(3:4)];
    set(s2,'position',s2Pos);
    
    
    
    %% # Alternative method. Brute force placement
    set(s1,'Units','normalized', 'position', [0.1 0.2 0.3 0.6]);
    set(s2,'Units','normalized', 'position', [0.5 0.2 0.3 0.6]);
    set(hb,'Units','normalized', 'position', [0.9 0.2 0.05 0.6]);
    

    enter image description here

提交回复
热议问题