Matlab:结合阴影误差和实线平均值的传说

5 matlab legend

我正在使用此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
Run Code Online (Sandbox Code Playgroud)

当前的方式正在显示:

在此输入图像描述

我做了什么: 正如有人在该文件的FEX页面的评论部分中建议在有线代码中的第314行之后添加以下代码.

set(get(get(hp(iln),'Annotation'),'LegendInformation'),'IconDisplayStyle','off');
Run Code Online (Sandbox Code Playgroud)

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

名称"Annotation"不是"root"类实例的可访问属性.

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

Ole*_*leg 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
Run Code Online (Sandbox Code Playgroud)

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)
Run Code Online (Sandbox Code Playgroud)

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

% 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);
Run Code Online (Sandbox Code Playgroud)