Matlab:结合阴影误差和实线均值的图例 [英] Matlab: Combine the legends of shaded error and solid line mean

查看:309
本文介绍了Matlab:结合阴影误差和实线均值的图例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用此FEX项以绘制在X轴上绘制的变量的水平阴影误差线.此变量绘制在不同的区域/区域中,因此,对于3个区域,有3个带阴影的误差线. 我想将误差线的图例(阴影区域)以及任何区域的平均值(实线)组合成一个由实线(或面片内的实线)表示的图例)的颜色与区域相同.

I am using this FEX entry to plot the horizontal shaded error bars for a variable plotted on the X-axis. This variable is plotted in different regions/zones and, therefore, there are 3 shaded error bars for 3 zones. I would like to combine the legends of the error bars (shaded region) as well as the mean (solid line) of any zone into a single legend represented by a solid line (or solid line inside a patch) of the same color as the zone.

我的代码进行绘图的方式:下面显示了我的绘图方式的综合示例

THE WAY MY CODE WORKS FOR PLOTTING: A synthetic example of the way I am plotting is shown below

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页的/a>,在但是,在执行此操作时出现此错误:

However, on doing that I get this error:

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

The name 'Annotation' is not an accessible property for an instance of class 'root'.

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

The first two answers suggested accessing the legend handles of the patch and line which are returned as output the by the function boundedline. I tried that but the problem is still not solved as the legend entries are still not consistent with the zones.

推荐答案

有直接的通用方法来

There is a direct general way to control legends. You can simply choose not to display line entries by fetching their 'Annotation' property and setting the 'IconDisplayStyle' to 'off'.

此外,为了使用 chadsgilbert 的解决方案,您需要从每次循环迭代中收集每个补丁程序句柄.我对您的代码进行了一些重构,使其可以同时使用两种解决方案.

Also, in order to use chadsgilbert's solution, you need to collect each single patch handle from every loop iteration. I refactored your code a bit, such that it would work with both solutions.

% 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 所示:

The simplest method as shown by 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)

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

... or for general more complex manipulations, set the legend display properties of the graphic objects:

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

这篇关于Matlab:结合阴影误差和实线均值的图例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆