1. 程式人生 > >matlab畫圖顯示部分曲線顯示legend

matlab畫圖顯示部分曲線顯示legend

h1=plot(x1,y1);
h2=plot(x2,y2);
h3=plot(x3,y3);
h4=plot(x4,y4);
legend([h1 h3],'x1-y1','x3-y3');
四線,圖例僅顯示1,3

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

  • %生成25*25矩陣

  • data = rand(25)+repmat(1:25,25,1);

  • H = plot(data);

  • %只對其中的部分曲線進行標註

  • h1=legend(H([1 6 11 16 21]),'1','6','11','16','21')

  • %使得標註橫向顯示

  • set(h1,'Orientation','horizon')

  • %去除標記框的外圍長方形

  • set(h1,'Box','off');

  • %%%%%%%%%%%%%%%%%%%%%%%%%%

  • 方法1:複雜到吐血

    例如你有25條曲線,想顯示其中1,6,11,16,21的legend,則

    for i = [2:5 7:10 12:15 17:20 22:25]
         set(get(get(H(i),'Annotation'),'LegendInformation'),'IconDisplayStyle','off'); 
    end
    legend('1','6','11','16','21');

    方法2:簡單到鬱悶

    H = plot(data);
    legend(H([1 6 11 16 21],'1,'6','11’,'16','21');



    如果一個圖中我們畫了n條曲線,但是我們只想加圖例說明(legend)的只有m條 (m

     


    1. 最簡單,最超級無敵的方法:把想要標註的圖形命令給個變數名,然後再legend命令中指定。

    x = -3.14:0.1:3.14;
    y1 = sin(x);
    y2 = cos(x);
    y3 = .1*exp(x);
    y4 = y1.*y3;
    hold on
    h1 = plot(x, y1, 'r');
    h2 = plot(x, y2, 'g');
    h3 = plot(x, y3, 'k');
    h4 = plot(x, y4, 'm');
    hold off
    xlim auto
    legend([h1,h3],'sin', 'exp');

    2.通過控制Annotation屬性來實現,詳細控制方法參見http://www.mathworks.cn/help/techdoc/creating_plots/braliom.html;jsessionid=HPs0TNGQxP2TXMcZgQv4zkMvmSsZYbhG6Lwjd3JT271PLqXnHxhY!-1484299157。但是需要注意的是在稍微低版本的MATLAB中,並不提供Annotation的控制(至少我的R14SP1不支援)。

    x = -3.14:0.1:3.14;
    y1 = sin(x);
    y2 = cos(x);
    y3 = .1*exp(x);
    y4 = y1.*y3;
    hold on
    h1 = plot(x, y1, 'r');
    h2 = plot(x, y2, 'g');
    h3 = plot(x, y3, 'k');
    h4 = plot(x, y4, 'm');
    hold off
    xlim auto
    set(get(get(h2, 'Annotation'), 'LegendInformation'), 'IconDisplayStyle', 'off');
    set(get(get(h4, 'Annotation'), 'LegendInformation'), 'IconDisplayStyle', 'off');
    legend('sin', 'exp');

    3.多個legend以及標註部分圖列構成多列圖列

    t=0:pi/100:2*pi; 
    y1=sin(t); 
    y2=cos(t); 
    y3=y1.*y2; 
    y4=0.5*(y1+y2); 
    hold on 
    h1=plot(t,y1,'-r') 
    %h11 = plot(t(1:10:end),y1(1:10:end),'o','MarkerFaceColor','r','MarkerEdgeColor','r'); 
    h11 = plot(t(1:20:end),y1(1:20:end),'ro'); 
    h2=plot(t,y2,'b-'); 
    %h22 = plot(t(1:10:end),y2(1:10:end),'^','MarkerFaceColor','b','MarkerEdgeColor','b'); 
    h22 = plot(t(1:20:end),y2(1:20:end),'b^')
    h3=plot(t,y3,'c'); 
    h4=plot(t,y4,'g'); 
    hold off 
    [legh,objh,outh,outm]=legend([h1,h2],'y1','y2',1);
    legend boxoff

    % matlab 6.5.1 
    %set(objh(3),'marker','*'); 
    %set(objh(5),'marker','.');

    % matlab7 
    set(objh(4),'marker','o'); 
    set(objh(6),'marker','^');


    legh2=copyobj(legh,gcf); 
    [legh2,objh2]=legend([h3,h4],'y3','y4',2); 
    legend boxoff

    這樣畫好後,只有第二個legend可拖動,而第一個legend不可拖動,原因不明。

    4.Matlab提供的legend函式,給出的legend經常覆蓋了某些曲線,這樣就需要把legend分成幾個,相對獨立,這樣可以使用滑鼠隨意移動,確保不遮擋曲線。

    a=linspace(0,2*pi,100);
    y1=100*sin(a);
    y2=50*cos(a);
    y3=tan(a);
    y4=log(a);
    y=[y1;y2;y3;y4];
    figure
    p=plot(a,y)

    legend(p(1:2),'sin','cos');
    ah=axes('position',get(gca,'position'),...
                'visible','off');
    legend(ah,p(3:4),'tan','log','location','west');

    5.用plot函式對兩個長度為30的向量分別繪製曲線的時候,在兩條曲線上各畫了一個marker(因為如果把所有的marker都放上去的話,感覺很擁擠,不是很好看),在對最終的畫圖效果做legend標註的時候,我希望將曲線及其上的marker一起標註

    clc;
    clear;
    close all;

    figure_handle = figure;
    set(figure_handle, 'Color', 'w');
    data=zeros(1, 100);
    data(1) = 1;
    data(2) = 1;
    data(3) = 1;
    t = [1 : 30] / 100 * 2 * pi;
    cur1=real(fft(data) / 3);
    cur1p = cur1(1 : 30);
    h_1(1) = plot(t, cur1p, 'LineStyle', '-', 'LineWidth', 2, 'Color', 'r');
    hold on
    h_1(2) = plot(t(15), cur1p(15), 'LineStyle', '-', 'LineWidth', 2,  'Color', 'r', 'Marker', 's', 'MarkerSize', 10, 'MarkerEdgeColor', 'r', 'MarkerFaceColor', 'r');
    set(h_1, 'Parent', hSGroup);
    set(get(get(hSGroup, 'Annotation'), 'LegendInformation'), 'IconDisplayStyle', 'on'); 
    cur2 = real(fft(data)) .* real(fft(data))/9;
    cur2p = cur2(1 : 30);
    h_2(1) = plot(t, cur2p, 'LineStyle', '-', 'LineWidth', 2, 'Color', 'g');
    h_2(2) = plot(t(12), cur2p(12), 'LineStyle', '-', 'LineWidth', 2, 'Color', 'g', 'Marker', 'o', 'MarkerSize',10, 'MarkerEdgeColor', 'g', 'MarkerFaceColor', 'g');
    set(h_2, 'Parent', hCGroup);
    set(get(get(hCGroup, 'Annotation'), 'LegendInformation'), 'IconDisplayStyle', 'on'); 
    legend('1 level', '2 levels');