在Matlab图例中获取多个标题或标题

在Matlab图例中获取多个标题或标题,matlab,legend,Matlab,Legend,我有下面的脚本为几个实验生成一种散点图。 实验给出了两个结果,一方面是向量s,另一方面是向量C2007,C2012,O2012和O2015。这4个向量表示不同的执行时间。在单元格n中,练习本身被标记为test1、test2、test3等等 下图显示了脚本的输出: 有两个问题:一方面,图例太长,但使用时占用了太多空间。另一方面,“test1”、“test2”等字符串是重复的,不属于代表不同时间的所用形状(如C2007、C2012、O2012、O2015)。因此,如果有可能使图例中的以下点更明显地

我有下面的脚本为几个实验生成一种散点图。 实验给出了两个结果,一方面是向量
s
,另一方面是向量
C2007
C2012
O2012
O2015
。这4个向量表示不同的执行时间。在单元格
n
中,练习本身被标记为
test1
test2
test3
等等

下图显示了脚本的输出:

有两个问题:一方面,图例太长,但使用时占用了太多空间。另一方面,“test1”、“test2”等字符串是重复的,不属于代表不同时间的所用形状(如
C2007、C2012、O2012、O2015
)。因此,如果有可能使图例中的以下点更明显地属于向量,那就太好了。为了展示我真正想要的,我用颜料画了一个草图:

第一种可能性:

这将解决形状未分类但图例太长的问题

因此,一种完全不同的方法是,使用图形中不存在的形状来显示哪种颜色属于哪种测试,然后只显示哪种形状属于哪种测试日期,再次使用一个小草图来显示我的想象:

遗憾的是,我不知道如何在这种情况下实现这种行为,例如拥有特定的颜色(来自颜色向量)和特定的形状。非常感谢您的帮助

close all;

s = [67,3,7,21,2,58,19,5]
colors = [0 0 0; 1 0 0 ;1 0 0.5; 1 0 1; 1 0.5 0; 1 0.5 0.5; 1 0.7 0; 0.7 0.7 0.5];
n = {'test1','test2','test3', 'test4', 'test5','test6','test7', 'test8'}

C2007 = [1.8, NaN, 7.2,3.8,2.44,2.1,7,NaN];
C2012 = [NaN, 6, 5.9, 2.5, NaN, 5.8, 8.1, NaN];
O2012 = [4,9.5,8.8, NaN, NaN, 1.2,4.5, NaN];
O2015 = [NaN, NaN, NaN, NaN, NaN, 2.8 4.4, 7.1];

figure;

for i=1:length(s)
   hplot1 = plot(s(i),C2007(i),'*','LineWidth',100,'Color',colors(i,:),'DisplayName',char(n(i)));
   legOBJ1 = legend('-DynamicLegend')  
   hold all;
end


for i=1:length(s)
   hplot2 = plot(s(i),C2012(i),'+','LineWidth',100,'Color',colors(i,:),'DisplayName',char(n(i)));
   legend('-DynamicLegend')
end


for i=1:length(s)
   hplot3 = plot(s(i),O2012(i),'o','LineWidth',100,'Color',colors(i,:),'DisplayName',char(n(i)));
   legend('-DynamicLegend')
end


for i=1:length(s)
   hplot4 = plot(s(i),O2015(i),'^','LineWidth',100,'Color',colors(i,:),'DisplayName',char(n(i)));
   legend('-DynamicLegend')
end

grid on

下面的代码将提供一种黑客方式来实现第二个更好的点

为了做到这一点,在所示图形的外部用想要的特征绘制点。为了制作标题,线的颜色被设置为白色,因此人们无法在图例中看到它

close all;

s = [67,3,7,21,2,58,19,5]
colors = [0 0 0; 1 0 0 ;1 0 0.5; 1 0 1; 1 0.5 0; 1 0.5 0.5; 1 0.7 0; 0.7 0.7 0.5];
n = {'test1','test2','test3', 'test4', 'test5','test6','test7', 'test8'}

C2007 = [1.8, NaN, 7.2,3.8,2.44,2.1,7,NaN];
C2012 = [NaN, 6, 5.9, 2.5, NaN, 5.8, 8.1, NaN];
O2012 = [4,9.5,8.8, NaN, NaN, 1.2,4.5, NaN];
O2015 = [NaN, NaN, NaN, NaN, NaN, 2.8 4.4, 7.1];

figure;

for i=1:length(s)
   hplot1 = plot(s(i),C2007(i),'*','LineWidth',100,'Color',colors(i,:)); 
   hold on;
end


for i=1:length(s)
   hplot2 = plot(s(i),C2012(i),'+','LineWidth',100,'Color',colors(i,:));
end


for i=1:length(s)
   hplot3 = plot(s(i),O2012(i),'o','LineWidth',100,'Color',colors(i,:));

end


for i=1:length(s)
   hplot4 = plot(s(i),O2015(i),'^','LineWidth',100,'Color',colors(i,:));

end

grid on

%Just stupid plots for overview
h1 = plot(-5,-5,'Color',[1 1 1]) %white dot, so that it will not be shown on
                                 %legend
h2 = plot(-5,-5,'*','LineWidth',100,'Color',[0.5 0.5 0.5]);
h3 = plot(-5,0,'+','LineWidth',100,'Color',[0.5 0.5 0.5]);
h4 = plot(-5,-5,'o','LineWidth',100,'Color',[0.5 0.5 0.5]);
h5 = plot(-5,-5,'^','LineWidth',100,'Color',[0.5 0.5 0.5]);
h6 = plot(-5,-5,'Color',[1 1 1])

for i=1:length(colors)
    hh(i) = plot(0,0,'-','Color',colors(i,:),'LineWidth',10);
end

%set boundaries to not show hack
xlim([0 100]);
ylim([0 12]);
legend([h1,h2,h3,h4,h5,h6,hh(1),hh(2),hh(3),hh(4),hh(5),hh(6),hh(7),hh(8)],{'Timing:','C2007','C2012','O2012','O2015','Tests: ', 'test1','test2','test3', 'test4', 'test5','test6','test7', 'test8'})