将图例添加到迭代创建的图形

Add legend to graph created iteratively

我正在从各种数据集中迭代创建一个图表,我尝试向其中添加图例,但失败了。

colors = {'g','b','m','k','c'};
subplot(2,3,iii);
hold all;
for jjj=1:length(aggregation_methods),
    aggregate = all_aggregate{jjj};
    std_agg = all_std_agg{jjj};
    plot(coverages, aggregate, colors{jjj});
    h1 = errorbar(coverages,aggregate,std_agg,colors{jjj});
    set(h1,'linestyle','none')
end
plot(coverages, regular, 'r');
h1 = errorbar(coverages,regular,std_reg,'r');
set(h1,'linestyle','none')
title(datasets{iii});
xlabel('coverage');
ylabel('MSE');
legend('enhanced with clustering(k=4)','enhanced random split', 'regular we');

图表似乎生成得很好,只是图例失败了。
我希望图例的颜色是 (top-down):绿色、蓝色、红色。

P.S。我也试过了(没用):

legend({'enhanced with clustering(k=4)','enhanced random split', 'regular we'});

编辑:

this thread的启发,我更改了代码以对每个图使用'DisplayName' 属性:

colors = {'g','b','m','k','c'};
names = {'enhanced with clustering(k=4)','enhanced random split', 'regular we'};
subplot(2,3,iii);
hold all;
for jjj=1:length(aggregation_methods),
    aggregate = all_aggregate{jjj};
    std_agg = all_std_agg{jjj};
    plot(coverages, aggregate, colors{jjj}, 'DisplayName', names{jjj});
    h1 = errorbar(coverages,aggregate,std_agg,colors{jjj});
    set(h1,'linestyle','none')
end
%plot(coverages,aggregate,'g',coverages ,regular,'r');
%h1 = errorbar(coverages,aggregate,std_agg,'g');
%set(h1,'linestyle','none')
plot(coverages, regular, 'r', 'DisplayName', names{length(aggregation_methods) + 1});
h1 = errorbar(coverages,regular,std_reg,'r');
set(h1,'linestyle','none')
%axis([0 1 0 max(mean(rc{iii}))]);
title(datasets{iii});
xlabel('coverage');
ylabel('MSE');
legend('show');

但是,它会产生以下图例,但我想摆脱这些 'dataX' 字符串。

a=[
1 2 3 
4 5 6
7 8 9
];
clist={'g','b','m'};

for i=1:3
    pt(i)=plot(a(i,:),'Color',clist{i});
    hold on;
end
b=[5 6 7];
pt(4)=plot(b,'Color','r');
legend([pt(1:2) pt(4)],{'line1','line2','line4'});