Matlab在一个情节2014b上的多个图例 [英] Matlab multiple legends on one plot 2014b

查看:258
本文介绍了Matlab在一个情节2014b上的多个图例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在一个地块上有多个图例.该解决方案在2014b版本之前可以完美运行.我试图弄清楚如何使用手柄优雅地进行此操作,但到目前为止还没有成功.任何想法都欢迎.

I want to have multiple legends on one plot. This solution works perfectly before 2014b version. I am trying to figure out how to make this elegantly using handles, but so far no success. Any ideas are welcome.

2013b中的示例:

Example in 2013b:

x = 1:50;
y1 = sin(x/2);
y2 = cos(x/2);

f = figure(1);
pl(1) = plot(x,y1,'g');hold on;
pl(2) = plot(x,y2,'r');

h1 = legend('eg1','eg2');    
set(h1,'Location','NorthEast')

tmp = copyobj(h1,f);

h2 = legend(pl,'sin','line');    
set(h2,'Location','SouthWest')

我确实使用

ax = gca;
tmp = copyobj([h1,ax],f);

但是当我再次设置图例时,先前的图例将位于情节下方.

but when I set the legend again, previous legend goes under the plot.

谢谢!

推荐答案

Matlab默认只允许每个轴使用一个图例,因此您要做的是创建一个假的/空的辅助轴以获取您的图例. Mathworks帮助就是一个很好的例子

Matlab by default only allows one legend per axes, so what you would have to do is create a fake/empty secondary axis in order to get your legend. Mathworks help has a good example of this

产生下面图表的代码在这里

Code to produce the below chart is here

x= 0:0.01:2*pi;
y = sin(x);
hl1 = line(x, y,'Color','k','LineStyle','--');
ax1 = gca;
set(ax1,'xlim',[0, 7],'ylim',[-1, 
1],'XColor','k','YColor','k');
legend_handle1 = legend(' sin');
ax2 = axes('Position',get(ax1,'Position'),...
           'xlim',[0, 7],'ylim',[-1,1],...
           'Visible','off','Color','none');
hl2 = line(pi/2, 1,'Color','r','Marker', 'o','Parent',ax2);
hl3 = line(pi, 0,'Color','g','Marker', 'x','Parent',ax2);
legend_handle2 = legend('peak', 'zero');
set(legend_handle2, 'Color', 'none');

这篇关于Matlab在一个情节2014b上的多个图例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆