trugle的个人博客分享 http://blog.sciencenet.cn/u/trugle

博文

Matlab如何真正自如的控制legend

已有 67336 次阅读 2014-2-5 00:06 |个人分类:Matlab点滴积累|系统分类:科研笔记| MATLAB, legend, 自由定制

最近遇到一个问题,用matlab绘制一堆线和点之后,想添加一下图例,但是遇到两个新问题

第一个是,需要绘制的图例比较多,用一个句子表达起来很长很长,除了不美观之外,致命的问题是这种方法太笨;

第二个问题是,在绘图的开始,无法提前获知究竟需要画多少个图例。


网上搜了一大堆,无非是翻来覆去的控制legend 的属性或者选择性的绘制部分图例。

最终总算解决,卖个关子先,看看一些基本操作。

引用博客网址http://fanlizi04.blog.163.com/blog/static/176729245201272735430969/,内容如下:

高级用法2:指定显示某几条曲线的legend:

方法1:复杂到吐血

例如你有25条曲线,想显示其中1,6,11,16,21的legend,则

for i = [2:5 7:10 12:15 17:20 22:25]
    set(get(get(H(i),'Annotation'),'LegendInformation'),'IconDisplayStyle','off');
end
legend('1','6','11','16','21');

方法2:简单到郁闷

H = plot(data);
legend(H([1 6 11 16 21],'1,'6','11’,'16','21');


如果一个图中我们画了n条曲线,但是我们只想加图例说明(legend)的只有m条 (m<n)。网上可以搜索很到资料,但是涉及到版本兼容问题,有些比较新的句柄属性在老版本Matlab中就用不起来,比如lineseries中的Annotation属性在我使用的R14SP1中就无法使用。



1. 最简单,最超级无敌的方法:把想要标注的图形命令给个变量名,然后再legend命令中指定。

x = -3.14:0.1:3.14;
y1 = sin(x);
y2 = cos(x);
y3 = .1*exp(x);
y4 = y1.*y3;
hold on
h1 = plot(x, y1, 'r');
h2 = plot(x, y2, 'g');
h3 = plot(x, y3, 'k');
h4 = plot(x, y4, 'm');
THETA=linspace(0,2*pi,1000);
RHO=ones(1,1000)*2;
[X,Y] = pol2cart(THETA,RHO);
X=X+10;
Y=Y+10;
h5=plot(X,Y,'r--');

axis square;
hold off
xlim auto
legend([h1,h3,h5],'sin', 'exp','circle');

 

 

转 MATLAB中Legend的一些控制方法 - tree - 清
 

 

2.通过控制Annotation属性来实现,详细控制方法参见http://www.mathworks.cn/help/techdoc/creating_plots/braliom.html;jsessionid=HPs0TNGQxP2TXMcZgQv4zkMvmSsZYbhG6Lwjd3JT271PLqXnHxhY!-1484299157。但是需要注意的是在稍微低版本的MATLAB中,并不提供Annotation的控制(至少我的R14SP1不支持)。

x = -3.14:0.1:3.14;
y1 = sin(x);
y2 = cos(x);
y3 = .1*exp(x);
y4 = y1.*y3;
hold on
h1 = plot(x, y1, 'r');
h2 = plot(x, y2, 'g');
h3 = plot(x, y3, 'k');
h4 = plot(x, y4, 'm');
hold off
xlim auto
set(get(get(h2, 'Annotation'), 'LegendInformation'), 'IconDisplayStyle', 'off');
set(get(get(h4, 'Annotation'), 'LegendInformation'), 'IconDisplayStyle', 'off');
legend('sin', 'exp');

3.多个legend以及标注部分图列构成多列图列

t=0:pi/100:2*pi;
y1=sin(t);
y2=cos(t);
y3=y1.*y2;
y4=0.5*(y1+y2);
hold on
h1=plot(t,y1,'-r')
%h11 = plot(t(1:10:end),y1(1:10:end),'o','MarkerFaceColor','r','MarkerEdgeColor','r');
h11 = plot(t(1:20:end),y1(1:20:end),'ro');
h2=plot(t,y2,'b-');
%h22 = plot(t(1:10:end),y2(1:10:end),'^','MarkerFaceColor','b','MarkerEdgeColor','b');
h22 = plot(t(1:20:end),y2(1:20:end),'b^')
h3=plot(t,y3,'c');
h4=plot(t,y4,'g');
hold off
[legh,objh,outh,outm]=legend([h1,h2],'y1','y2',1);
legend boxoff

% matlab 6.5.1
%set(objh(3),'marker','*');
%set(objh(5),'marker','.');

% matlab7
set(objh(4),'marker','o');
set(objh(6),'marker','^');


legh2=copyobj(legh,gcf);
[legh2,objh2]=legend([h3,h4],'y3','y4',2);
legend boxoff

这样画好后,只有第二个legend可拖动,而第一个legend不可拖动,原因不明。

4.Matlab提供的legend函数,给出的legend经常覆盖了某些曲线,这样就需要把legend分成几个,相对独立,这样可以使用鼠标随意移动,确保不遮挡曲线。

a=linspace(0,2*pi,100);
y1=100*sin(a);
y2=50*cos(a);
y3=tan(a);
y4=log(a);
y=[y1;y2;y3;y4];
figure
p=plot(a,y)
legend(p(1:2),'sin','cos');
ah=axes('position',get(gca,'position'),...
           'visible','off');
legend(ah,p(3:4),'tan','log','location','west');

5.用plot函数对两个长度为30的向量分别绘制曲线的时候,在两条曲线上各画了一个marker(因为如果把所有的marker都放上去的话,感觉很拥挤,不是很好看),在对最终的画图效果做legend标注的时候,我希望将曲线及其上的marker一起标注

clc;
clear;
close all;

figure_handle = figure;
set(figure_handle, 'Color', 'w');
data=zeros(1, 100);
data(1) = 1;
data(2) = 1;
data(3) = 1;
t = [1 : 30] / 100 * 2 * pi;
cur1=real(fft(data) / 3);
cur1p = cur1(1 : 30);
h_1(1) = plot(t, cur1p, 'LineStyle', '-', 'LineWidth', 2, 'Color', 'r');
hold on
h_1(2) = plot(t(15), cur1p(15), 'LineStyle', '-', 'LineWidth', 2,  'Color', 'r', 'Marker', 's', 'MarkerSize', 10, 'MarkerEdgeColor', 'r', 'MarkerFaceColor', 'r');
set(h_1, 'Parent', hSGroup);
set(get(get(hSGroup, 'Annotation'), 'LegendInformation'), 'IconDisplayStyle', 'on');
cur2 = real(fft(data)) .* real(fft(data))/9;
cur2p = cur2(1 : 30);
h_2(1) = plot(t, cur2p, 'LineStyle', '-', 'LineWidth', 2, 'Color', 'g');
h_2(2) = plot(t(12), cur2p(12), 'LineStyle', '-', 'LineWidth', 2, 'Color', 'g', 'Marker', 'o', 'MarkerSize',10, 'MarkerEdgeColor', 'g', 'MarkerFaceColor', 'g');
set(h_2, 'Parent', hCGroup);
set(get(get(hCGroup, 'Annotation'), 'LegendInformation'), 'IconDisplayStyle', 'on');
legend('1 level', '2 levels');

还可以用LEGEND(...,'Location',LOC) 来指定图例标识框的位置

这些是Matlab help文件。后面一段是对应的翻译和说明

       'North'              inside plot box near top
       'South'              inside bottom
       'East'               inside right
       'West'               inside left
       'NorthEast'          inside top right (default)
       'NorthWest           inside top left
       'SouthEast'          inside bottom right
       'SouthWest'          inside bottom left
       'NorthOutside'       outside plot box near top
       'SouthOutside'       outside bottom
       'EastOutside'        outside right
       'WestOutside'        outside left
       'NorthEastOutside'   outside top right
       'NorthWestOutside'   outside top left
       'SouthEastOutside'   outside bottom right
       'SouthWestOutside'   outside bottom left
       'Best'               least conflict with data in plot
       'BestOutside'        least unused space outside plot


       'North'             图例标识放在图顶端
       'South'            图例标识放在图底端
       'East'               图例标识放在图右方
       'West'              图例标识放在图左方
       'NorthEast'       图例标识放在图右上方(默认)
       'NorthWest      图例标识放在图左上方
       'SouthEast'      图例标识放在图右下角
       'SouthWest'     图例标识放在图左下角
(以上几个都是将图例标识放在框图内)
       'NorthOutside'          图例标识放在图框外侧上方
       'SouthOutside'         图例标识放在图框外侧下方
       'EastOutside'           图例标识放在图框外侧右方
       'WestOutside'          图例标识放在图框外侧左方
       'NorthEastOutside'   图例标识放在图框外侧右上方
       'NorthWestOutside' 图例标识放在图框外侧左上方
       'SouthEastOutside'   图例标识放在图框外侧右下方
       'SouthWestOutside' 图例标识放在图框外侧左下方
(以上几个将图例标识放在框图外)
       'Best'                      图标标识放在图框内不与图冲突的最佳位置
       'BestOutside'           图标标识放在图框外使用最小空间的最佳位置


上面的博客细致耐心,可见下了很多功夫,无奈对于我的还是没有多少帮助。找到matlab官方网址,搜了一堆资料,终于搞定,欣喜异常,分享一下。而且,以后或许会忘记,再回科学网找一下变轻松解决。

x=-1:.2:1;

y(:,1)=sin(x);

y(:,2)=cos(x);

y(:,3)=y1.*y2;

y(:,4)=log(x);

y(:,5)=-log(x);

......

for i=1:n

   mcolor=rand(1,3);

   plot(x, y(i),'--', 'color', mcolor);

   %重点解决方法在这里

   mylgd{i} = ['y', num2str(i)];

end

legend(mylgd);

至此完全搞定,是不是比上面引用的介绍使用起来简单方便呢?




https://blog.sciencenet.cn/blog-662112-764543.html

上一篇:踏踏实实,莫做爱国贼
下一篇:matlab绘图输出eps文件及边框
收藏 IP: 158.49.247.*| 热度|

0

该博文允许注册用户评论 请点击登录 评论 (2 个评论)

数据加载中...

Archiver|手机版|科学网 ( 京ICP备07017567号-12 )

GMT+8, 2024-3-29 23:23

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部