2017-04-13 22 views
0

我想知道是否可以将图例项与图例项分离为两种不同类型的行。例如:假设您有4条曲线:纯黑,纯红,虚线黑,红色虚线。黑色曲线描述黑色现象,红色曲线描述现象红色。实线决定我们是否添加除实体之外的其他贡献,虚线表示我们为它添加一些虚线贡献。在我的情节的传说中,我只想要两个条目:现象黑色或现象红色。但我希望每个条目的传奇线分成两部分:上半部分是坚实的,后半部分是破灭。以同样的方式,是否有可能以相反的方式做到这一点(一半是纯黑色,另一半是纯红色,另一半是半黑色半红色)。是否可以在Matlab中将图例输入行分成两部分

对于4条曲线,这并没有多大意义。但我有时不得不把6点或8的曲线和传说是那么太大,可以把它放在图中的...

目前我使用这行添加我的传说: legend({str1,str2},'Interpreter','latex') 但我不不知道这是否有意义。

我张贴的图片来说明我想什么(请注意,这可能是周围的其他方法,有两种风格的一条线,而不是两种颜色):enter image description here

回答

0

这不正是你要的,但它的另一种方法:

styles = {'-','--'}; 
colors = {'r','g','b'}; 
colorNames = {'red','green','blue'}; 
styleNames = {'normal','dashed'}; 
hold on 
% plot many lines 
for ii = 1:numel(styles) 
    for jj = 1:numel(colors) 
     plot((1:10) + jj + ii*numel(colors),'Color',colors{jj},'LineStyle',styles{ii}) 
    end 
end 
% generate handles for the legend 
h = []; 
for ii = numel(colors):-1:1 
    h(numel(styles)+ ii) = plot(0,0,'Color',colors{ii},'LineStyle','-'); 
end 
for ii = numel(styles):-1:1 
    h(ii) = plot(0,0,'Color','k','LineStyle',styles{ii}); 
end 
hold off 
legend(h,[styleNames colorNames]); 

enter image description here

0

没有在为Matlab的传说做这个内置的能力。您可以通过手动绘制线条来实现类似的效果。这使用了annotation arrow功能为一个图:

% plot some dummy data (not connected to the manual legend! 
x = linspace(-1,1); 
clf; hold on; grid on 
% Set up linestyles and linecolors here so that they can be (at least 
% slightly) linked between the plot and the manual legend. 
linestyles = {'-', '--'}; 
linecolors = {'k', 'r'}; 
% plots 
plot(x,x.^2,'linestyle',linestyles{1},'color',linecolors{1}); 
plot(x,x.^3,'linestyle',linestyles{1},'color',linecolors{2});  
plot(x,x.^4,'linestyle',linestyles{2},'color',linecolors{1}); 
plot(x,x.^5,'linestyle',linestyles{2},'color',linecolors{2}); 
% scale the plot within the figure to leave room for legend 
plotsize = [0.06, 0.20, 0.9, 0.75]; 
set(gca,'position', plotsize) 
% x and y are original positions for the lines 
x = 0.4; y = 0.1; 
% dx and dy are length and vertical spacing of lines respectively 
dx = 0.1; dy = 0.05; 

% The main event: drawing (headless) text arrows, so that one of them can have 
% a string properly which is your legend entry label. Use x,y,dx,dy for positioning 
annotation('textarrow', [x,x+dx], [y,y], ... 
    'linestyle', linestyles{1}, 'color', linecolors{1}, 'textcolor', 'k', 'headstyle', 'none', ... 
    'string', 'Even functions ') 
annotation('textarrow', [x+dx + 0.005,x+2*dx + 0.005], [y,y], ... 
    'linestyle', linestyles{2}, 'color', linecolors{1}, 'textcolor', 'k', 'headstyle', 'none') 
annotation('textarrow', [x,x+dx], [y-dy,y-dy], ... 
    'linestyle', linestyles{1}, 'color', linecolors{2}, 'textcolor', 'k', 'headstyle', 'none', ... 
    'string', 'Odd functions ') 
annotation('textarrow', [x+dx + 0.005,x+2*dx + 0.005], [y-dy,y-dy], ... 
    'linestyle', linestyles{2}, 'color', linecolors{2}, 'textcolor', 'k', 'headstyle', 'none') 

结果:

plot

注意该定位与归一化的完成的值(0和1之间),因此它们与舒展图。如果您的绘图具有固定大小,处理像素可以更容易可视化,这可以通过在调整大小时更改各种图形对象的参数来完成(请参阅上面关于注释箭头链接的文档)。

+0

有没有办法知道图例本身的属性?例如,图例中绘制的线的大小,图例的周围框与线条本身之间的空间,......类似的东西,以便我至少可以获得一个位置,从中可以绘制出像您这样的注释没有。我无法自己找到它,但我想我有很多我不知道matlab,必须有一种方法来找到这些参数。 – mwoua

+0

总之,我不这么认为。 – Wolfie

相关问题