2013-05-02 314 views
0

我經常遇到與MATLAB圖解中的圖例有關的問題,並想提出一種方法來避免它們在未來。MATLAB:在圖形底部添加圖例而不改變圖形寬度或調整圖的大小

我想什麼做的是以下幾點:

f = figure('Position',[0 0 800 600]

  • 情節不管它是什麼,我想繪製:

    1. 有一個固定大小的創建圖在此圖中

      x = -pi:0.01:pi
      plot(x,sin(x),x,cos(x),x,tan(x))

    2. 在圖的底部處添加一個圖例,但不調整圖的大小(我可以使圖形「更高」可以這麼說「,但我希望這個圖例能夠勝過該圖,軸和其他一切)。如果可能的話,我還想用legendflex包創建圖例(不確定這是否會引發任何問題)。

    有誰知道我該怎麼做?

  • 回答

    2

    我使用Octave而不是MATLAB,但是做了以下工作(或者至少讓你更接近你想要的)?

    % Create the figure and plot 
    f = figure('Position',[0 0 800 600]); 
    x = -pi:0.01:pi; 
    plot(x,sin(x),x,cos(x),x,tan(x)); 
    
    % Set axes and figure units to pixels, get current positions 
    set(f,'Units','pixels') 
    set(gca,'Units','pixels') 
    fig_pos = get(f,'position'); 
    old_ax_pos = get(gca,'position'); 
    
    % Add a legend et get its position too 
    h = legend('L1','L2','L3','location','southoutside'); 
    set(h,'Units','pixels') 
    leg_pos = get(h,'position'); 
    
    % Get the new axes position, look at how much it shifted 
    new_ax_pos = get(gca,'position'); 
    pixel_shift = new_ax_pos - old_ax_pos; % y position shift is positive (axes moved up), y height shift is negative (axes got smaller) 
    
    % Make figure taller and restore axes height to their initial value 
    set(f,'position',fig_pos - [0 0 0 pixel_shift(4)]); 
    set(h,'position',leg_pos) 
    set(gca,'position',old_ax_pos + [0 pixel_shift(2) 0 0]) 
    
    % Create a new figure without legend for comparing 
    f2 = figure('Position',[0 0 800 600]); 
    x = -pi:0.01:pi; 
    plot(x,sin(x),x,cos(x),x,tan(x)); 
    

    阿爾諾

    +0

    這個作品!謝謝! – 2013-05-23 21:59:40