此外,为了使用 chadsgilbert 的解决方案,您需要从每次循环迭代中收集每个补丁程序句柄.我对您的代码进行了一些重构,使其可以同时使用两种解决方案. %重构您的示例(可以完全向量化)数字axesh = axes('next','add');相当于保留的%nZones = 4;nPts = 10;clr = {'r','b','g','m','y','c'};X = 兰迪 (10, nPts, nZones);Y = 兰迪 (10, nPts, nZones);XError = rand(nPts,nZones);%预分配句柄hl =零(nZones,1);hp =零(nZones,1);% 环形对于ii = 1:nZones[hl(ii),hp(ii)] =界线(X(:, ii),Y(:, ii),XError(:, ii),...['-',clr {ii}],'transparency',0.15,'orientation','horiz');结尾 chadsgilbert 所示的最简单方法: %将图例条目创建为nZones x 8字符数组,格式为"Zone#01",....TagAx =重塑(sprintf('Zone#%02d',1:nZones),8,nZones)'图例(hp,TagAx) ...或对于一般更复杂的操作,设置图形对象的图例显示属性: %不显示图例中的行hAnn = cell2mat(get(hl,'Annotation'));hLegEn = cell2mat(get(hAnn,'LegendInformation'));set(hLegEn,'IconDisplayStyle','off')%添加图例图例(TagAx); 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 belowfh = 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');endTHE WAY LEGENDS ARE SHOWING-UP CURRENTLY:WHAT I HAVE TRIED:As someone suggested in the comment section of that file's FEX page to add the following code after line 314 in boundedline code.set(get(get(hp(iln),'Annotation'),'LegendInformation'),'IconDisplayStyle','off');However, on doing that I get this error: EDIT: 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'.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)figureaxesh = axes('next','add'); % equivalent to hold onnZones = 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 handleshl = zeros(nZones,1);hp = zeros(nZones,1);% LOOPfor ii = 1:nZones [hl(ii), hp(ii)] = boundedline(X(:, ii), Y(:, ii), XError(:, ii),... ['-', clr{ii}], 'transparency', 0.15, 'orientation', 'horiz');endThe 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 legendhAnn = cell2mat(get(hl,'Annotation'));hLegEn = cell2mat(get(hAnn,'LegendInformation'));set(hLegEn,'IconDisplayStyle','off')% Add legendlegend(TagAx); 这篇关于结合阴影误差和实线均值的图例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
06-10 19:09