http://www.eetop.cn/blog/html/03/6503-23349.html

如果一个图中我们画了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');
hold off
xlim auto
legend([h1,h3],'sin', 'exp');

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

[ZZ] MATLAB中Legend的一些控制方法的更多相关文章

  1. [ZZ] matlab中小波变换函数dwt2和wavedec2 系数提取函数appcoef2和detcoef2

    https://zhidao.baidu.com/question/88038464.html DWT2是二维单尺度小波变换,其可以通过指定小波或者分解滤波器进行二维单尺度小波分解. 而WAVEDEC ...

  2. matlab的legend用法

    用Matlab画图时,有时候需要对各种图标进行标注,例如,用“+”代表A的运动情况,“*”代表B的运动情况. legend函数的基本用法是: LEGEND(string1,string2,string ...

  3. Matlab中给figure添加图例(legend),标题(title)和颜色(color)

    在Matlab绘图过程中,尤其是需要将多个图绘制在相同的坐标轴中时,通常需要将不同的曲线设置成为不同的颜色.此外,为了直观,还需要给这张图标增添标题和图例.这篇文章展示了在Matlab的绘图窗口(fi ...

  4. MATLAB中plot()画图的颜色线型和希腊字母参数设置

    y         黄色           ·             点线      m         粉红           ○             圈线      c          ...

  5. matlab 中txt文件(含字符及数值)处理

    matlab 中txt文件(含字符及数值)处理 (2008-08-02 09:45:12) 转载▼ 标签: 杂谈 分类: matlab及C学习 Matlab文件操作及读txt文件ZZ 2008-07- ...

  6. MATLAB中的微积分运算(数值&符号)

    显然这个函数是单词differential(微分)的简写,用于计算微分.实际上准确来说计算的是差商. 如果输入一个长度为n的一维向量,则该函数将会返回长度为n-1的向量,向量的值是原向量相邻元素的差, ...

  7. matlab中hold指令、figure指令及subplot指令的使用

    一.hold指令使用 正常情况下,plot指令显示figure时,以前的数据丢失了.使用hold on指令后,此后添加的一系列plot曲线将叠加在前一个图上当使用hold off后,恢复为默认状况,p ...

  8. Matlab中hold on与hold off的用法

    摘录自:https://blog.csdn.net/smf0504/article/details/51830963 https://www.cnblogs.com/shuqingstudy/p/48 ...

  9. Matlab中使用LaTeX

    Matlab作为数据计算和处理的数学语言(软件),而LaTex作为出版界的重要排版语言(软件),尤其是对数学公式的排版功能特别强.在Matlab中有两种方法使用LaTeX:1)对Matlab生成的图形 ...

随机推荐

  1. 帝国cms相关调用

    Loop用法:[!--temp.header--] [e:loop={6,6,0,1}] <!--标题连接/标题--> <a href="<?=$bqsr[title ...

  2. Ubuntu 改变workspace布局

    今天花了点时间找ubuntu的workspace布局.发现一个软件,tweak, 非常好用,可以随意调整布局. Ref: http://ubuntuhandbook.org/index.php/201 ...

  3. Page_Init 的执行过程

    网上有的资料在说Page_Init这个事件只会在第一次加载页面时触发,实际上并不是这样,我们会发现每次回发页面,Page_Init事件都会被执行, 举一个例子 private static int t ...

  4. jQuery关于Select的操作

    jQuery获取Select选择的Text和Value: 1. var checkText=jQuery("#select_id").find("option:selec ...

  5. Java-->发牌流程修改版

    --> 这一次要封装得狠一点... package com.xm.ddz; // 每一张牌的属性 public class Card { private String flowerColor; ...

  6. box2dweb 学习笔记--sample讲解

    前言: 之前博文"台球游戏的核心算法和AI(1)" 中, 提到过想用HTML5+Box2d来编写实现一个台球游戏. 以此来对比感慨一下游戏物理引擎的巨大威力. 做为H5+box2d ...

  7. Linux 线程--那一年, 我们一起忽视的pthread_join

    前言: 通过linux的pthread库, 相信大家对创建/销毁线程肯定很熟悉, 不过对pthread_join是否知道的更多呢?实验: 先编写一个常规的程序 #include <pthread ...

  8. 215. Kth Largest Element in an Array

    Find the kth largest element in an unsorted array. Note that it is the kth largest element in the so ...

  9. Codeforces Round #373 (Div. 2) A B C 水 贪心 模拟(四舍五入进位)

    A. Vitya in the Countryside time limit per test 1 second memory limit per test 256 megabytes input s ...

  10. 工作中遇到的问题--Hibernate注解添加在一方和多方的区别

    以Good和GoodStatus为例: 一.注解仅添加在一方: @Entity@Table(name = "GOOD")@Where(clause="enabled=1& ...