The Wayback Machine - https://web.archive.org/web/20111120175700/http://www.mathworks.com:80/help/techdoc/creating_plots/braliom.html
Skip to Main Content Skip to Search
Product Documentation

Controlling Legends

Legend Control Options

Graphics objects that represent data, such as lines, surfaces, patches, etc., can be represented in figure legends (see legend for information on creating legends). By setting object properties, you can:

Properties for Controlling Legend Content

Graphics objects have two properties that control these options:

Accessing the Annotation Control Objects

Querying the Annotation property returns the handle of an hg.Annotation object. The hg.Annotation object has a property called LegendInformation, which contains an hg.LegendEntry object. The hg.LegendEntry object has a property called IconDisplayStyle that you can set to one of three values.

IconDisplayStyle ValueBehavior
onRepresent this object in a figure legend.
offDo not include this object in a figure legend .
childrenDisplay legend entries for this object's children and not the object itself (applies only to objects that have children, otherwise, the same as on).

For example, if object_handle is the handle of a graphics object, use the following statements to set the object's IconDisplayStyle. In this case, the graphics object, object_handle, is not included in the legend because its IconDisplayStyle property is off.

hAnnotation = get(object_handle,'Annotation');
hLegendEntry = get(hAnnotation','LegendInformation');
set(hLegendEntry,'IconDisplayStyle','off')

Updating a Legend

If a legend exists and you change its IconDisplayStyle setting, you must call legend to update the display. See the legend command for the options available.

Example — Excluding a Particular Object From a Legend

This example creates a graph of random data values and draws a line at the mean y value. The blue data line does not appear in the legend because that line object has its IconDisplayStyle property of the associated LegendEntry object set to off. See Properties for Controlling Legend Content for more information.

To execute the following code, copy it into a separate function file, save it as annotation_property_line.m, and run it from the Command Window.

function annotation_property_line
dat = rand(50,1);
hLine = plot(dat);
plotMean % Nested function draws a line at mean value
set(get(get(hLine,'Annotation'),'LegendInformation'),...
    'IconDisplayStyle','off'); % Exclude line from legend
legend('mean')
    function plotMean
    xlimits = get(gca,'XLim');
    meanValue = mean(dat);
    meanLine = line([xlimits(1) xlimits(2)],...
		 [meanValue meanValue],'Color','k','LineStyle','-.');
    end
end

Here is the resulting graph.

Example — One Legend Entry for a Group of Objects

You can group graphics objects in an hggroup or hgtransform object and represent the whole group as one item in a legend. This example creates two series of graphs (sines and cosines of the same data).

t = 0:.1:2*pi;
for k=1:5
    offset = k/7;
    m(:,k) = t+offset';
end
hSLines = plot(t,sin(m),'Color','b');hold on
hCLines = plot(t,cos(m),'Color','g');
hSGroup = hggroup;
hCGroup = hggroup;
set(hSLines,'Parent',hSGroup)
set(hCLines,'Parent',hCGroup)
% Include these hggroups in the legend:
set(get(get(hSGroup,'Annotation'),'LegendInformation'),...
    'IconDisplayStyle','on'); 
set(get(get(hCGroup,'Annotation'),'LegendInformation'),...
    'IconDisplayStyle','on'); 
legend('Sine','Cosine')

Example — Showing Children of Group Objects in Legend

You can include the children of a group in the legend by setting the group object's IconDisplayStyle to children. This step is useful when graphs contain plot objects, which are groups of core graphics objects. For example, consider the following contour graph:

[X,Y] = meshgrid(-2:.1:2);
Z = X.*exp(-X.^2-Y.^2);
[mC hC] = contour(X,Y,Z);
set(get(get(hC,'Annotation'),'LegendInformation'),...
    'IconDisplayStyle','Children');
%{
Assigns each line object's DisplayName property a string 
based on the value of the contour interval it represents
%}
k =1; ind = 1; hLines = get(hC,'Children');
while k < size(mC,2),
   set(hLines(ind),'DisplayName',num2str(mC(1,k)))
   k = k+mC(2,k)+1; ind = ind+1;
end 
% Display the legend using DisplayName labels
legend('show')

Example — Grouping Objects to Reduce the Legend Entries

Some functions that visualize large data sets create many objects to render graphs. For example, contourslice uses patch objects to generate contour slices of volume data. This example groups the 1829 patch objects into hggroup objects according to which plane the objects represent and sets corresponding values for the DisplayName property, resulting in a legend with only three items.

load mri
D = squeeze(D);
phandles = contourslice(D,[],[],[1,15,27],8);view(3)
gh(1) = hggroup; gh(2) = hggroup; gh(3) = hggroup;
%set(gh,'Parent',gca)
for k=1:length(phandles)
   zd = get(phandles(k),'ZData');
   plane = num2str(zd(1));
   switch plane
      case '1'
         set(phandles(k),'Parent',gh(1),'EdgeColor','r')
      case '15'
         set(phandles(k),'Parent',gh(2),'EdgeColor','g')
      case '27'
         set(phandles(k),'Parent',gh(3),'EdgeColor','b')
      otherwise
         disp('Don''t know what to do with it')
   end
end
hA = get(gh,'Annotation');
hLL = get([hA{:}],'LegendInformation');
set([hLL{:}],{'IconDisplayStyle'},...
   {'on','on','on'}')
set(gh,{'DisplayName'},{'Level=1','Level=15','Level=27'}')
legend show

  


Recommended Products

Includes the most popular MATLAB recorded presentations with Q&A; sessions led by MATLAB experts.

 © 1984-2011- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS