IT博客汇
  • 首页
  • 精华
  • 技术
  • 设计
  • 资讯
  • 扯淡
  • 权利声明
  • 登录 注册

    [原]Matlab画图 包含两个legend

    bendanban发表于 2016-10-04 22:08:59
    love 0

    没时间闲扯了,直接上代码吧。

    x = 0:0.01:3;
    
    y1 = sin(x);
    y2 = sin(x).^2;
    y3 = sin(x).^sin(x);
    y4 = cos(x).*sin(x);
    
    plotsc{1} = 'ph(i) = plot(x, y1); hold on;';
    plotsc{2} = 'ph(i) = plot(x, y2); hold on;';
    plotsc{3} = 'ph(i) = plot(x, y3); hold on;';
    plotsc{4} = 'ph(i) = plot(x, y4); hold on;';
    
    figure;
    for i = 1:numel(plotsc)
      eval(plotsc{i});
    end
    
    legendsc = {'y_1', 'y_2', 'y_3', 'y_4'};
    
    lg1 = legend(ph(1:2), legendsc(1:2), 'Location', 'SouthWest');
    ah=axes('position',get(gca,'position'), 'visible','off');
    lg2 = legend(ah, ph(3:4), legendsc(3:4));
    lg2.Position = lg1.Position;
    lg2.Position(1) = lg1.Position(1)+lg1.Position(3)+0.02;

    效果如下:
    这里写图片描述



沪ICP备19023445号-2号
友情链接