MATLAB Plot - 多个数据行的图例条目 - getcolumn
MATLAB Plot - Legend entry for multiple data rows - getcolumn
考虑以下示例:
x = magic(3);
figure(1); clf(1);
plot( x, '-r', 'DisplayName', 'Magic' );
legend( 'show' );
在 MATLAB R2014a 中生成的图例条目是
getcolumn(魔术,1)
getcolumn(魔术,2)
getcolumn(魔法,3)
问题源于 legend.m
中的 function [leg,labelhandles,outH,outM] = legend(varargin)
(版权所有 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)的问题不再出现
如果你想为图例条目设置多个短语法显示名称,你只需要用它们准备一个元胞数组,假设它叫做leg_names
,然后使用set
一次将它们应用于所有对象:
set(p,{'DisplayName'},leg_names);
我们来看一个例子:
x = magic(3); % your data
p = plot( x,'-r'); % plot and get an array of handles to the lines
% create a list of the desired names for the legend entries:
leg_names = [repmat('Magic ',size(x,2),1) num2str((1:size(x,2)).')];
set(p,{'DisplayName'},cellstr(leg_names)); % set all display names
legend('show'); % show the legend
结果与问题末尾的示例完全相同。
此外,请注意语法:[lgd,icons,plots,txt] = legend(___)
不推荐 (from the docs):
Note: This syntax is not recommended. It creates a legend that does not support all graphics features. Instead, use the lgd = legend(__)
syntax to return the Legend object and set Legend Properties.
考虑以下示例:
x = magic(3);
figure(1); clf(1);
plot( x, '-r', 'DisplayName', 'Magic' );
legend( 'show' );
在 MATLAB R2014a 中生成的图例条目是
getcolumn(魔术,1)
getcolumn(魔术,2)
getcolumn(魔法,3)
问题源于 legend.m
中的 function [leg,labelhandles,outH,outM] = legend(varargin)
(版权所有 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)的问题不再出现
如果你想为图例条目设置多个短语法显示名称,你只需要用它们准备一个元胞数组,假设它叫做leg_names
,然后使用set
一次将它们应用于所有对象:
set(p,{'DisplayName'},leg_names);
我们来看一个例子:
x = magic(3); % your data
p = plot( x,'-r'); % plot and get an array of handles to the lines
% create a list of the desired names for the legend entries:
leg_names = [repmat('Magic ',size(x,2),1) num2str((1:size(x,2)).')];
set(p,{'DisplayName'},cellstr(leg_names)); % set all display names
legend('show'); % show the legend
结果与问题末尾的示例完全相同。
此外,请注意语法:[lgd,icons,plots,txt] = legend(___)
不推荐 (from the docs):
Note: This syntax is not recommended. It creates a legend that does not support all graphics features. Instead, use the
lgd = legend(__)
syntax to return the Legend object and set Legend Properties.