5

我正在使用这个 FEX 条目来绘制在 X 轴上绘制的变量的水平阴影误差条。该变量绘制在不同的区域/区域中,因此,3 个区域有 3 个阴影误差条。我想将误差线(阴影区域)的图例以及任何区域的平均值(实线)组合成一个单一的图例,由与颜色相同的实线(或补丁内的实线)表示区。

我的代码用于绘图的方式:我绘图方式的综合示例如下所示

fh = figure();
axesh = axes('Parent', fh);
nZones = 4;
nPts = 10;
X = nan*ones(nPts, nZones);
Y = nan*ones(nPts, nZones);
XError = nan*ones(10, 4);
clr = {'r', 'b', 'g', 'm', 'y', 'c'};
for iZone = 1:nZones
    X(:, iZone) = randi(10, nPts, 1);
    Y(:, iZone) = randi(10, nPts, 1);
    XError(:, iZone) = rand(nPts, 1);
    % Append Legend Entries/Tags
    if iZone == 1
        TagAx = {['Zone # ', num2str(iZone)]};
    else
        TagAx = [TagAx, {['Zone # ', num2str(iZone)]}];
    end
    hold(axesh, 'on')
    [hLine, hPatch] = boundedline(X(:, iZone), Y(:, iZone), XError(:, iZone),...
        strcat('-', clr{iZone}), axesh, 'transparency', 0.15,...
        'orientation', 'horiz');
    legend(TagAx);
    xlabel(axesh, 'X', 'Fontweight', 'Bold');
    ylabel(axesh, 'Y', 'Fontweight', 'Bold');
    title(axesh, 'Error bars in X', 'Fontweight', 'Bold');
end

传奇人物目前的表现方式

在此处输入图像描述

我尝试了什么:正如有人在该文件的 FEX 页面的评论部分中建议的那样,在boundedline code 的第 314 行之后添加以下代码

set(get(get(hp(iln),'Annotation'),'LegendInformation'),'IconDisplayStyle','off');

但是,这样做我得到这个错误:

名称“注释”不是“根”类实例的可访问属性。

编辑:前两个答案建议访问由函数作为输出返回的补丁和行的图例句柄boundedline。我试过了,但问题仍然没有解决,因为图例条目仍然与区域不一致。

4

3 回答 3

4

有一种直接的通用方法可以控制图例'Annotation'您可以通过获取它们的属性并将其设置为'IconDisplayStyle'来简单地选择不显示行条目'off'

此外,为了使用chadsgilbert的解决方案,您需要从每个循环迭代中收集每个补丁句柄。我稍微重构了您的代码,以便它可以与两种解决方案一起使用。

% Refactoring you example (can be fully vectorized)
figure
axesh  = axes('next','add'); % equivalent to hold on
nZones = 4;
nPts   = 10;
clr    = {'r', 'b', 'g', 'm', 'y', 'c'};
X      = randi(10, nPts, nZones);
Y      = randi(10, nPts, nZones);
XError = rand(nPts, nZones);

% Preallocate handles 
hl = zeros(nZones,1);
hp = zeros(nZones,1);
% LOOP
for ii = 1:nZones
    [hl(ii), hp(ii)] = boundedline(X(:, ii), Y(:, ii), XError(:, ii),...
        ['-', clr{ii}], 'transparency', 0.15, 'orientation', 'horiz');
end

最简单的方法如chadsgilbert所示:

% Create legend entries as a nZones x 8 char array of the format 'Zone #01',...
TagAx  = reshape(sprintf('Zone #%02d',1:nZones),8,nZones)'
legend(hp,TagAx)

...或者对于一般更复杂的操作,设置图形对象的图例显示属性:

% Do not display lines in the legend
hAnn   = cell2mat(get(hl,'Annotation'));
hLegEn = cell2mat(get(hAnn,'LegendInformation'));
set(hLegEn,'IconDisplayStyle','off')

% Add legend
legend(TagAx);
于 2013-07-17T20:59:54.390 回答
4

没有一个简单的方法可以做到这一点,你必须进入传说并改变事情:

下面制作了一个示例图,其中平均线没有叠加在绑定的补丁上。

N = 10;
x = 1:N;
y = sin(x);
b = ones([N,2,1]);

[hl, hp] = boundedline(x, y, b);
lh = legend('hi','');

我们得到一个g与图例句柄关联的结构lh。如果您查看孩子的类型,您会发现这g2是平均线并且g4是补丁。所以我得到了补丁的顶点并用它来移动平均线。

g = get(lh);
g2 = get(g.Children(2));
g4 = get(g.Children(4));

v = g4.Vertices;
vx = unique(v(:,1));
vy = diff(unique(v(:,2)))/2;
vy = [vy vy] + min(v(:,2));

set(g.Children(2), 'XData', vx);
set(g.Children(2), 'YData', vy);

在此处输入图像描述

这不是一个简单的答案,并且绝对需要您为具有多个平均线/补丁对的绘图进行大量格式化,特别是因为它会在最后一个区域的平均线所在的图例中留下空白。

根据您的评论,如果您只想要阴影错误栏来标记图例,那么这很容易:

x = 0:0.1:10;
N = length(x);
y1 = sin(x);
y2 = cos(x);
y3 = cos(2*x);

b1 = ones([N,2,1]);
b2 = 0.5*ones([N,2,1]);
b3 = 0.1*ones([N,2,1]);

hold on
[hl, hp] = boundedline(x, y1, b1, 'r',  x, y2, b2, 'b', x, y3, b3,'c')

lh = legend('one','two','three');

在此处输入图像描述

于 2013-07-12T15:46:43.510 回答
1

这是一个不错的 FEX 条目。您可以将一组特定的句柄与图例相关联。幸运的是,boundedline已经将句柄与补丁的句柄分开返回。

>> [hl,hp] = boundedline([1 2 3],[4 5 6], [1 2 1],'b',[1 2 3],-[4 5 6],[2 1 1],'r');
>> %legend(hl,{'blue line' 'red line'}); % If you want narrow lines in the legend
>> legend(hp,{'blue patch' 'red patch'});  % If you want patches in the legend.
于 2013-07-12T15:09:26.370 回答