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

时间:2015-01-07 09:21:49

标签: matlab plot

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

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');

enter image description here

图表似乎生成得很好,它只是失败的传奇。 我希望传奇的颜色是(自上而下):绿色,蓝色,红色。

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'字符串。 enter image description here

1 个答案:

答案 0 :(得分:1)

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'});