time | Calls | line |
---|
| | 1 | function [leg,labelhandles,outH,outM] = legend(varargin)
|
| | 2 | % LEGEND Create legend
|
| | 3 | %
|
| | 4 | % LEGEND creates a legend with descriptive labels for each plotted data
|
| | 5 | % series. For the labels, the legend uses the text from the DisplayName
|
| | 6 | % properties of the data series. If the DisplayName property is empty, then
|
| | 7 | % the legend uses a label of the form 'dataN'. The legend automatically
|
| | 8 | % updates when you add or delete data series from the axes. This command
|
| | 9 | % creates a legend for the current axes or chart returned by gca. If the
|
| | 10 | % current axes is empty, then the legend is empty. If axes do not exist,
|
| | 11 | % then this command creates one.
|
| | 12 | %
|
| | 13 | % LEGEND(label1,...,labelN) sets the labels. Specify the labels as a
|
| | 14 | % list of character vectors, such as legend('Jan','Feb','Mar').
|
| | 15 | %
|
| | 16 | % LEGEND(labels) sets the labels using a cell array of character vectors or
|
| | 17 | % a character matrix, such as legend({'Jan','Feb','Mar'}).
|
| | 18 | %
|
| | 19 | % LEGEND(subset,__) only includes items in the legend for the data series
|
| | 20 | % listed in subset. Specify subset as a vector of graphics objects.
|
| | 21 | %
|
| | 22 | % LEGEND(target,__) uses the axes, polar axes, or chart specified by target
|
| | 23 | % instead of the current axes or chart. Specify the target as the first
|
| | 24 | % input argument.
|
| | 25 | %
|
| | 26 | % LEGEND(__,'Location',lcn) sets the legend location. For example,
|
| | 27 | % 'Location','northeast' positions the legend in the upper right corner of
|
| | 28 | % the axes. Specify the location after other input arguments.
|
| | 29 | %
|
| | 30 | % LEGEND(__,'Orientation',ornt), where ornt is 'horizontal', displays the
|
| | 31 | % legend items side-by-side. The default for ornt is 'vertical', which
|
| | 32 | % stacks the items vertically.
|
| | 33 | %
|
| | 34 | % LEGEND(__,Name,Value) sets legend properties using one or more name-value
|
| | 35 | % pair arguments. When setting properties, include the labels in a cell
|
| | 36 | % array, such as legend({'A','B'},'FontSize',12). If you do not want to
|
| | 37 | % specify labels, then include an empty cell array, such as
|
| | 38 | % legend({},'FontSize',12). Reissuing the legend command retains
|
| | 39 | % modifications that you previously specified.
|
| | 40 | %
|
| | 41 | % LEGEND(bkgd), where bkgd is 'boxoff', removes the legend background and
|
| | 42 | % outline. The default for bkgd is 'boxon', which displays the legend
|
| | 43 | % background and outline.
|
| | 44 | %
|
| | 45 | % lgd = LEGEND(__) returns the Legend object. Use lgd to query and set
|
| | 46 | % properties of the legend after it is created. For a list of properties,
|
| | 47 | % see Legend.
|
| | 48 | %
|
| | 49 | % [lgd,icons,plots,txt] = LEGEND(__) additionally returns the objects used
|
| | 50 | % to create the legend icons, the objects plotted in the graph, and an
|
| | 51 | % array of the label text. This syntax is not recommended. Some
|
| | 52 | % functionality is not supported. Instead, use the lgd = legend(__) syntax
|
| | 53 | % to return the Legend object and set Legend Properties.
|
| | 54 | %
|
| | 55 | % LEGEND(vsbl) controls the visibility of the legend, where vsbl is 'hide',
|
| | 56 | % 'show', or 'toggle'.
|
| | 57 | %
|
| | 58 | % LEGEND('off') deletes the legend.
|
| | 59 | %
|
| | 60 | %
|
| | 61 | % Examples:
|
| | 62 | % x = 0:.2:12;
|
| | 63 | % Y = [besselj(1,x); besselj(2,x); besselj(3,x)];
|
| | 64 | % plot(x,Y);
|
| | 65 | % legend('First','Second','Third','Location','NorthEastOutside')
|
| | 66 | %
|
| | 67 | % b = bar(rand(10,5),'stacked');
|
| | 68 | % hold on
|
| | 69 | % ln = plot(1:10,5*rand(10,1),'-o');
|
| | 70 | % hold off
|
| | 71 | % legend([b,ln],'Carrots','Peas','Peppers','Green Beans',...
|
| | 72 | % 'Cucumbers','Eggplant')
|
| | 73 |
|
| | 74 | % Unsupported APIs for internal use:
|
| | 75 | %
|
| | 76 | % LOC strings can be abbreviated NE, SO, etc or lower case.
|
| | 77 | %
|
| | 78 | % LEGEND(LI,string1,string2,string3) creates a legend for legendinfo
|
| | 79 | % objects LI with strings string1, etc.
|
| | 80 | % LEGEND(LI,M) creates a legend for legendinfo objects LI where M is a
|
| | 81 | % string matrix or cell array of strings corresponding to the legendinfo
|
| | 82 | % objects.
|
| | 83 |
|
| | 84 | % Copyright 1984-2019 The MathWorks, Inc.
|
| | 85 |
|
| | 86 | % Legend no longer supports more than one output argument
|
| | 87 | % Warn the user and ignore additional output arguments.
|
| | 88 |
|
< 0.001 | 8 | 89 | args = varargin;
|
| | 90 |
|
| | 91 | % Continue warning that the v6 form will go away in the future.
|
0.001 | 8 | 92 | if (nargin > 1 ...
|
| 8 | 93 | && istextscalar(args{1}) ...
|
| | 94 | && ~istextscalar(args{2}) ...
|
| | 95 | && strcmp(args{1},'v6'))
|
| | 96 | warning(message('MATLAB:legend:DeprecatedV6Argument'));
|
< 0.001 | 8 | 97 | end
|
| | 98 |
|
| | 99 | %--------------------------------------------------------
|
| | 100 | % Begin building the legend
|
| | 101 | %--------------------------------------------------------
|
< 0.001 | 8 | 102 | narg = nargin;
|
| | 103 |
|
| | 104 | % HANDLE FINDLEGEND CASES FIRST
|
< 0.001 | 8 | 105 | if narg==2 ...
|
| | 106 | && istextscalar(args{1}) ...
|
| | 107 | && strcmpi(args{1},'-find') ...
|
| | 108 | && ~isempty(args{2}) ...
|
| | 109 | && (isgraphics(args{2},'axes') || isgraphics(args{2},'polaraxes') || isgraphics(args{2},'geoaxes'))
|
| | 110 |
|
| | 111 | [leg,labelhandles,outH,outM] = setOutArgs(args{2});
|
| | 112 |
|
| | 113 | return;
|
< 0.001 | 8 | 114 | end
|
| | 115 |
|
| | 116 | % add flag to create compatible legend
|
< 0.001 | 8 | 117 | version = 'off';
|
< 0.001 | 8 | 118 | if nargout > 1
|
| | 119 | version = 'on';
|
| | 120 |
|
| | 121 | % Compatible Legend not supported in UIAxes
|
| | 122 | if narg > 0 && ...
|
| | 123 | ~isempty(args{1}) && ...
|
| | 124 | length(args{1})==1 && ...
|
| | 125 | ishghandle(args{1}) && ...
|
| | 126 | isa(handle(args{1}),'matlab.ui.control.UIAxes')
|
| | 127 |
|
| | 128 | error(message('MATLAB:legend:MultipleOutputsWithUIAxes'));
|
| | 129 |
|
| | 130 | end
|
< 0.001 | 8 | 131 | end
|
| | 132 |
|
< 0.001 | 8 | 133 | arg = 1;
|
| | 134 |
|
| | 135 | % GET AXES FROM INPUTS
|
0.002 | 8 | 136 | ha = matlab.graphics.Graphics.empty;
|
| | 137 |
|
| | 138 | % if the user passes in an axes that is not the current axes we want to be
|
| | 139 | % sure to restore the current axes before exiting.
|
0.001 | 8 | 140 | old_currfig = get(0,'CurrentFigure');
|
< 0.001 | 8 | 141 | if ~isempty(old_currfig) && ishghandle(old_currfig)
|
< 0.001 | 8 | 142 | old_currax = get(old_currfig,'CurrentAxes');
|
< 0.001 | 8 | 143 | if ~isempty(old_currax)
|
< 0.001 | 8 | 144 | ha = old_currax;
|
< 0.001 | 8 | 145 | end
|
< 0.001 | 8 | 146 | end
|
< 0.001 | 8 | 147 | limitMaxLegendEntries = false;
|
| | 148 | % Check if first input is a matrix of graphics object instead of a vector.
|
< 0.001 | 8 | 149 | if narg > 0
|
< 0.001 | 8 | 150 | graphicsInput = all(isgraphics(args{1}));
|
< 0.001 | 8 | 151 | if all(isgraphics(args{1}), 'all');
|
| | 152 | if isa(args{1}, 'matlab.graphics.axis.Axes')
|
| | 153 | limitMaxLegendEntries = true;
|
| | 154 | end
|
| | 155 |
|
| | 156 | if ~isvector(args{1}) && ~isempty(args{1})
|
| | 157 | error(message('MATLAB:legend:NonVectorInputOfGraphics'));
|
| | 158 | end
|
< 0.001 | 8 | 159 | end
|
| | 160 |
|
| | 161 | else
|
| | 162 | limitMaxLegendEntries = true;
|
< 0.001 | 8 | 163 | end
|
| | 164 |
|
| | 165 | % determine peer axes from inputs
|
< 0.001 | 8 | 166 | if narg > 0 && ...
|
| 8 | 167 | ~isempty(args{1}) && ...
|
| 8 | 168 | length(args{1})==1 && ...
|
| | 169 | ishghandle(args{1}) && ...
|
| | 170 | (isa(handle(args{1}),'matlab.graphics.mixin.LegendTarget') || ...
|
| | 171 | isa(handle(args{1}),'matlab.ui.control.UIAxes'))
|
| | 172 | % legend(ax,...)
|
| | 173 | % ha is an Axes or UIAxes at this point
|
| | 174 | ha = handle(args{1});
|
| | 175 | arg = arg + 1;
|
< 0.001 | 8 | 176 | elseif narg > 0 && ...
|
| 8 | 177 | ~istextscalar(args{1}) && ...
|
| 8 | 178 | ~isempty(args{1}) && ...
|
| 8 | 179 | graphicsInput % legend(children,strings,...)
|
| | 180 | ha = [ancestor(args{1}(1),'axes') ancestor(args{1}(1),'polaraxes') ancestor(args{1}(1),'geoaxes')];
|
| | 181 | if isempty(ha)
|
| | 182 | obj = args{1}(1);
|
| | 183 | if ~isempty(obj) % Provide a better error message if we can.
|
| | 184 | error(message('MATLAB:legend:InvalidPeerHandle', getClassName(obj)));
|
| | 185 | else
|
| | 186 | error(message('MATLAB:legend:InvalidPeerParameter'));
|
| | 187 | end
|
| | 188 | end
|
< 0.001 | 8 | 189 | else
|
| | 190 | % if no axes has been identified thus far, create one using GCA.
|
< 0.001 | 8 | 191 | if isempty(ha)
|
| | 192 | if strcmp(version,'on')
|
| | 193 | % Legacy behavior for compatible legend
|
| | 194 | warning(message('MATLAB:legend:NoCurrentAxes'));
|
| | 195 | [leg,labelhandles,outH,outM] = setOutArgs(ha);
|
| | 196 | return
|
| | 197 | else
|
| | 198 | % if ha is empty, use GCA, which will create an axes if necessary.
|
| | 199 | ha = gca;
|
| | 200 | end
|
< 0.001 | 8 | 201 | else
|
| | 202 | % Chart subclass support
|
| | 203 | % Invoke legend method with same number of outputs to defer output arg
|
| | 204 | % error handling to the method.
|
0.001 | 8 | 205 | if isa(ha,'matlab.graphics.chart.Chart')
|
| | 206 | try
|
| | 207 | [varargout{1:nargout}] = legend(ha,args{:}); %#ok<NASGU>
|
| | 208 | catch e
|
| | 209 | throw(e)
|
| | 210 | end
|
| | 211 | return
|
< 0.001 | 8 | 212 | end
|
< 0.001 | 8 | 213 | end
|
< 0.001 | 8 | 214 | end
|
| | 215 |
|
| | 216 | % cast double to MCOS handle
|
< 0.001 | 8 | 217 | if ~isobject(ha)
|
| | 218 | ha = handle(ha);
|
< 0.001 | 8 | 219 | end
|
| | 220 |
|
| | 221 | % PROCESS REMAINING INPUTS
|
< 0.001 | 8 | 222 | if narg < arg % legend or legend(ax)
|
| | 223 | if ~isempty(find_legend(ha)) || strcmp(version,'on')
|
| | 224 | if nargout > 0
|
| | 225 | [leg,labelhandles,outH,outM] = setOutArgs(ha);
|
| | 226 | end
|
| | 227 | return;
|
| | 228 | end
|
< 0.001 | 8 | 229 | end
|
| | 230 |
|
< 0.001 | 8 | 231 | if narg >= arg && ...
|
| 8 | 232 | istextscalar(args{arg}) && ...
|
| | 233 | all(ismember(char(lower(args{arg})),{'off','deletelegend',...
|
| | 234 | 'resizelegend',...
|
| | 235 | 'toggle','show','hide',...
|
| | 236 | 'boxon','boxoff'}))
|
| | 237 |
|
| | 238 |
|
| | 239 | switch char(lower(args{arg}))
|
| | 240 | case {'off', 'deletelegend'}
|
| | 241 | delete_legend(find_legend(ha));
|
| | 242 | case 'resizelegend'
|
| | 243 | % pass
|
| | 244 | case 'toggle'
|
| | 245 | l = find_legend(ha);
|
| | 246 | if isempty(l) || strcmpi(get(l, 'Visible'), 'off')
|
| | 247 | legend(ha, 'show');
|
| | 248 | else
|
| | 249 | legend(ha, 'hide');
|
| | 250 | end
|
| | 251 | case 'show'
|
| | 252 | l = find_legend(ha);
|
| | 253 | if isempty(l)
|
| | 254 | make_legend(ha, args(arg+1:end), version, limitMaxLegendEntries);
|
| | 255 | else
|
| | 256 | set(l, 'Visible', 'on');
|
| | 257 | end
|
| | 258 | case 'hide'
|
| | 259 | set(legend(ha), 'Visible', 'off');
|
| | 260 | case 'boxon'
|
| | 261 | set(legend(ha), 'Box', 'on');
|
| | 262 | case 'boxoff'
|
| | 263 | set(legend(ha), 'Box', 'off');
|
| | 264 | otherwise
|
| | 265 | assert(false,'make sure there is a case for each item in the ismember check above')
|
| | 266 | end
|
< 0.001 | 8 | 267 | else % narg > 1
|
| | 268 | % legend(<string>,...)
|
| | 269 | % legend(<charMatrix>,...)
|
| | 270 | % legend(<cellStr>,...)
|
| | 271 | % legend(p,...)
|
| | 272 | % legend(p,<string>,...)
|
| | 273 | % legend(p,<charMatrix>,...)
|
| | 274 | % legend(p,<cellStr>,...)
|
| | 275 | % legend('Location',<value>)
|
| | 276 | % legend('Orientation',<value>)
|
| | 277 | % legend('AutoUdpate',<value>)
|
0.757 | 8 | 278 | make_legend(ha,args(arg:end),version, limitMaxLegendEntries);
|
< 0.001 | 8 | 279 | end
|
| | 280 |
|
| | 281 | % PROCESS OUTPUTS
|
< 0.001 | 8 | 282 | if nargout>0
|
| | 283 | [leg,labelhandles,outH,outM] = setOutArgs(ha);
|
< 0.001 | 8 | 284 | end
|
| | 285 |
|
| | 286 | % before going, be sure to reset current figure and axes
|
0.002 | 8 | 287 | if ~isempty(old_currfig) && ishghandle(old_currfig) && ~strcmpi(get(old_currfig,'beingdeleted'),'on')
|
0.001 | 8 | 288 | set(0,'CurrentFigure',old_currfig);
|
0.001 | 8 | 289 | if ~isempty(old_currax) && ishghandle(old_currax) && ~strcmpi(get(old_currax,'beingdeleted'),'on')
|
0.001 | 8 | 290 | set(old_currfig,'CurrentAxes',old_currax);
|
< 0.001 | 8 | 291 | end
|
< 0.001 | 8 | 292 | end
|
Other subfunctions in this file are not included in this listing.