【发布时间】:2018-01-13 12:07:49
【问题描述】:
考虑以下示例:
x = magic(3);
figure(1); clf(1);
plot( x, '-r', 'DisplayName', 'Magic' );
legend( 'show' );
MATLAB R2014a 中生成的图例条目是
getcolumn(Magic,1)
getcolumn(Magic,2)
getcolumn(Magic,3)
问题源于legend.m 中的function [leg,labelhandles,outH,outM] = legend(varargin)(Copyright 1984-2012 The MathWorks, Inc.)第 628 行:str{k} = get(ch(k),'DisplayName');
更具体地说,函数get
- 前置
getcolumn(和 - 附加
, <Column Number>)。
对于以DisplayName 命名的多个具有相同视觉属性的数据行,是否有一种简单的方法可以只显示一个图例条目(或多个,但没有前置和附加的字符串)?
当然,另一种选择是通过绘图句柄(见下文)以编程方式创建多个(或一个)图例条目,但我想保持简短。
一个条目:
x = magic(3);
figure(1); clf(1);
h = plot( x, '-r' );
legend( h(1), 'Magic' );
多个条目:
x = magic(3);
figure(1); clf(1);
h = plot( x, '-r' );
strL = cell( 1, numel(h) );
for k = 1:numel(h)
strL{k} = sprintf( 'Magic %d', k );
end
legend( h, strL );
在 MATLAB R2014b 中,第一个代码示例不再出现 getcolumn(Name,Row) 的问题。
【问题讨论】:
标签: matlab matlab-figure legend