comparison scripts/plot/legend.m @ 15442:015cc3d1f389

legend.m: Overhaul function and add support for automatic data labels. * legend.m: Add support for automatic data labels (data1, data2,...) when no labels are specified and no DisplayName properties exist. Overhaul documentation. Add more comments to code about what is happening. Re-order demos and add a title to each plot which describes what is being shown.
author Rik <rik@octave.org>
date Tue, 25 Sep 2012 13:28:53 -0700
parents e5a07d7aafcc
children cf191298a287
comparison
equal deleted inserted replaced
15441:e5a07d7aafcc 15442:015cc3d1f389
24 ## @deftypefnx {Function File} {} legend (@dots{}, "orientation", @var{orient}) 24 ## @deftypefnx {Function File} {} legend (@dots{}, "orientation", @var{orient})
25 ## @deftypefnx {Function File} {} legend (@var{hax}, @dots{}) 25 ## @deftypefnx {Function File} {} legend (@var{hax}, @dots{})
26 ## @deftypefnx {Function File} {} legend (@var{hobjs}, @dots{}) 26 ## @deftypefnx {Function File} {} legend (@var{hobjs}, @dots{})
27 ## @deftypefnx {Function File} {} legend (@var{hax}, @var{hobjs}, @dots{}) 27 ## @deftypefnx {Function File} {} legend (@var{hax}, @var{hobjs}, @dots{})
28 ## @deftypefnx {Function File} {} legend ("@var{option}") 28 ## @deftypefnx {Function File} {} legend ("@var{option}")
29 ## @deftypefnx {Function File} {[@var{hleg}, @var{hleg_obj}, @var{hplot}, @var{labels}] =} legend (@dots{})
29 ## 30 ##
30 ## Display a legend for the axes with handle @var{hax}, or the current axes, 31 ## Display a legend for the axes with handle @var{hax}, or the current axes,
31 ## using the specified strings as labels. Legend entries may be specified 32 ## using the specified strings as labels. Legend entries may be specified
32 ## as individual character string arguments, a character array, or a cell 33 ## as individual character string arguments, a character array, or a cell
33 ## array of character strings. If the handles, @var{hobjs}, are not specified 34 ## array of character strings. If the handles, @var{hobjs}, are not specified
34 ## then the legend's strings will be associated with the axes' descendants. 35 ## then the legend's strings will be associated with the axes' descendants.
35 ## Legend works on line graphs, bar graphs, etc. 36 ## @code{legend} works on line graphs, bar graphs, etc.
36 ## A plot must exist before legend is called. 37 ## A plot must exist before legend is called.
37 ## 38 ##
38 ## The optional parameter @var{pos} specifies the location of the legend 39 ## The optional parameter @var{pos} specifies the location of the legend
39 ## as follows: 40 ## as follows:
40 ## 41 ##
72 ## @item @tab outside @tab 73 ## @item @tab outside @tab
73 ## can be appended to any location string 74 ## can be appended to any location string
74 ## @end multitable 75 ## @end multitable
75 ## 76 ##
76 ## The optional parameter @var{orient} determines if the key elements 77 ## The optional parameter @var{orient} determines if the key elements
77 ## are placed vertically or horizontally. The allowed values are "vertical" 78 ## are placed vertically or horizontally. The allowed values are
78 ## or "horizontal" with the default being "vertical". 79 ## "vertical" (default) or "horizontal".
79 ## 80 ##
80 ## The following customizations are available using @var{option}: 81 ## The following customizations are available using @var{option}:
81 ## 82 ##
82 ## @table @asis 83 ## @table @asis
83 ## @item "show" 84 ## @item "show"
94 ## 95 ##
95 ## @item "boxoff" 96 ## @item "boxoff"
96 ## Hide the box around legend 97 ## Hide the box around legend
97 ## 98 ##
98 ## @item "left" 99 ## @item "left"
99 ## Place text to the left of the keys 100 ## Place label text to the left of the keys
100 ## 101 ##
101 ## @item "right" 102 ## @item "right"
102 ## Place text to the right of the keys 103 ## Place label text to the right of the keys
103 ## 104 ##
104 ## @itemx "off" 105 ## @itemx "off"
105 ## Delete the legend object 106 ## Delete the legend object
106 ## @end table 107 ## @end table
108 ##
109 ## The optional output values are
110 ##
111 ## @table @var
112 ## @item hleg
113 ## The graphics handle of the legend object.
114 ##
115 ## @item hleg_obj
116 ## Graphics handles to the text and line objects which make up the legend.
117 ##
118 ## @item hplot
119 ## Graphics handles to the plot objects which were used in making the legend.
120 ##
121 ## @item labels
122 ## A cell array of strings of the labels in the legend.
123 ## @end table
124 ##
125 ## The legend label text is either provided in the call to @code{legend} or
126 ## is taken from the DisplayName property of graphics objects. If no
127 ## labels or DisplayNames are available, then the label text is simply
128 ## "data1", "data2", @dots{}, "dataN".
107 ## @end deftypefn 129 ## @end deftypefn
108 130
109 function [hlegend2, hobjects2, hplot2, text_strings2] = legend (varargin) 131 function [hlegend2, hobjects2, hplot2, text_strings2] = legend (varargin)
110 132
111 if (nargin > 0 133 if (nargin > 0
120 fig = gcf (); 142 fig = gcf ();
121 endif 143 endif
122 ca = gca (); 144 ca = gca ();
123 endif 145 endif
124 146
147 ## Special handling for plotyy which has two axes objects
125 if (ishandle (ca) && isprop (ca, "__plotyy_axes__")) 148 if (ishandle (ca) && isprop (ca, "__plotyy_axes__"))
126 plty = get (ca, "__plotyy_axes__"); 149 plty = get (ca, "__plotyy_axes__");
127 if (isscalar (plty) && ishandle (plty)) 150 if (isscalar (plty) && ishandle (plty))
128 ca = [ca, plty]; 151 ca = [ca, plty];
129 elseif (iscell (plty)) 152 elseif (iscell (plty))
157 position = "default"; 180 position = "default";
158 show = "create"; 181 show = "create";
159 textpos = "default"; 182 textpos = "default";
160 box = "default"; 183 box = "default";
161 184
185 ## Process old way of specifying position with a number rather than a string.
162 if (nargs > 0) 186 if (nargs > 0)
163 pos = varargin{nargs}; 187 pos = varargin{nargs};
164 if (isnumeric (pos) && isscalar (pos) && pos == fix (pos)) 188 if (isnumeric (pos) && isscalar (pos) && pos == fix (pos))
165 if (pos >= -1 && pos <= 4) 189 if (pos >= -1 && pos <= 4)
166 position = [{"northeastoutside", "best", "northeast", 190 position = [{"northeastoutside", "best", "northeast",
170 error ("legend: invalid position specified"); 194 error ("legend: invalid position specified");
171 endif 195 endif
172 endif 196 endif
173 endif 197 endif
174 198
199 ## Find position and orientation property/value pairs
175 while (nargs > 1) 200 while (nargs > 1)
176 pos = varargin{nargs-1}; 201 pos = varargin{nargs-1};
177 str = varargin{nargs}; 202 str = varargin{nargs};
178 if (strcmpi (pos, "location") && ischar (str)) 203 if (strcmpi (pos, "location") && ischar (str))
179 position = lower (str); 204 position = lower (str);
180 nargs -= 2; 205 nargs -= 2;
181 elseif (strcmpi (pos, "orientation") && ischar (str)) 206 elseif (strcmpi (pos, "orientation") && ischar (str))
182 orientation = lower (str); 207 orientation = lower (str);
183 nargs -= 2; 208 nargs -= 2;
184 else 209 else
185 break; 210 break;
186 endif 211 endif
191 case {"vertical", "horizontal", "default"} 216 case {"vertical", "horizontal", "default"}
192 otherwise 217 otherwise
193 error ("legend: unrecognized legend orientation"); 218 error ("legend: unrecognized legend orientation");
194 endswitch 219 endswitch
195 220
196 ## Validate the position type is valid 221 ## Validate the position type
197 outside = false; 222 outside = false;
198 inout = strfind (position, "outside"); 223 inout = strfind (position, "outside");
199 if (! isempty (inout)) 224 if (! isempty (inout))
200 outside = true; 225 outside = true;
201 position = position(1:inout-1); 226 position = position(1:inout-1);
211 position = "northeast"; 236 position = "northeast";
212 otherwise 237 otherwise
213 error ("legend: unrecognized legend position"); 238 error ("legend: unrecognized legend position");
214 endswitch 239 endswitch
215 240
241 ## Find any existing legend object on figure
216 hlegend = []; 242 hlegend = [];
217 fkids = get (fig, "children"); 243 fkids = get (fig, "children");
218 for i = 1 : numel (fkids) 244 for i = 1 : numel (fkids)
219 if (ishandle (fkids(i)) && strcmp (get (fkids(i), "type"), "axes") 245 if (ishandle (fkids(i)) && strcmp (get (fkids(i), "type"), "axes")
220 && (strcmp (get (fkids(i), "tag"), "legend"))) 246 && (strcmp (get (fkids(i), "tag"), "legend")))
268 case "right" 294 case "right"
269 textpos = "right"; 295 textpos = "right";
270 nargs--; 296 nargs--;
271 endswitch 297 endswitch
272 else 298 else
299 ## Character matrix of labels
273 varargin = cellstr (arg); 300 varargin = cellstr (arg);
274 nargs = numel (varargin); 301 nargs = numel (varargin);
275 endif 302 endif
276 elseif (iscellstr (arg)) 303 elseif (iscellstr (arg))
304 ## Cell array of labels
277 varargin = arg; 305 varargin = arg;
278 nargs = numel (varargin); 306 nargs = numel (varargin);
279 else 307 else
280 error ("legend: expecting argument to be a character string"); 308 error ("legend: expecting argument to be a character string");
281 endif 309 endif
282 elseif (nargs > 1 && iscellstr (varargin{1})) 310 elseif (nargs > 1 && iscellstr (varargin{1}))
311 ## Cell array of labels followed by property/value pairs
283 varargin = {varargin{1}{:}, varargin{2:end}}; 312 varargin = {varargin{1}{:}, varargin{2:end}};
284 nargs = numel (varargin); 313 nargs = numel (varargin);
285 endif 314 endif
315
316 have_labels = (nargs > 0);
286 317
287 if (strcmp (show, "off")) 318 if (strcmp (show, "off"))
288 if (! isempty (hlegend)) 319 if (! isempty (hlegend))
289 set (findobj (hlegend), "visible", "off"); 320 set (findobj (hlegend), "visible", "off");
290 hlegend = []; 321 hlegend = [];
308 endif 339 endif
309 elseif (strcmp (box, "off")) 340 elseif (strcmp (box, "off"))
310 if (! isempty (hlegend)) 341 if (! isempty (hlegend))
311 set (hlegend, "box", "off", "visible", "off"); 342 set (hlegend, "box", "off", "visible", "off");
312 endif 343 endif
313 elseif (nargs == 0 && !(strcmp (position, "default") && 344 elseif (! have_labels && !(strcmp (position, "default") &&
314 strcmp (orientation, "default"))) 345 strcmp (orientation, "default")))
346 ## Changing location or orientation of existing legend
315 if (! isempty (hlegend)) 347 if (! isempty (hlegend))
316 hax = getfield (get (hlegend, "userdata"), "handle"); 348 hax = getfield (get (hlegend, "userdata"), "handle");
317 [hplots, text_strings] = __getlegenddata__ (hlegend); 349 [hplots, text_strings] = __getlegenddata__ (hlegend);
318 350
319 if (strcmp (position, "default")) 351 if (strcmp (position, "default"))
334 "orientation", orientation); 366 "orientation", orientation);
335 endif 367 endif
336 endif 368 endif
337 endif 369 endif
338 else 370 else
371 ## Create new legend
339 hobjects = []; 372 hobjects = [];
340 hplots = []; 373 hplots = [];
341 text_strings = {}; 374 text_strings = {};
342 375
343 if (nargs > 0) 376 if (have_labels)
377 ## Check for valid data that can be labeled.
344 have_data = false; 378 have_data = false;
379 have_dname = false;
345 for k = 1 : nkids 380 for k = 1 : nkids
346 typ = get (kids(k), "type"); 381 typ = get (kids(k), "type");
347 if (strcmp (typ, "line") || strcmp (typ, "surface") 382 if (strcmp (typ, "line") || strcmp (typ, "surface")
348 || strcmp (typ, "patch") || strcmp (typ, "hggroup")) 383 || strcmp (typ, "patch") || strcmp (typ, "hggroup"))
349 have_data = true; 384 have_data = true;
352 endfor 387 endfor
353 388
354 if (! have_data) 389 if (! have_data)
355 warning ("legend: plot data is empty; setting key labels has no effect"); 390 warning ("legend: plot data is empty; setting key labels has no effect");
356 endif 391 endif
357 endif 392 else
358 393 ## No labels. Search for DisplayName property.
359 if (strcmp (textpos, "default")) 394 have_dname = false;
395 for k = 1 : nkids
396 hkid = kids(k);
397 typ = get (hkid, "type");
398 if (strcmp (typ, "line") || strcmp (typ, "surface")
399 || strcmp (typ, "patch"))
400 if (! isempty (get (hkid, "displayname")))
401 have_dname = true;
402 break;
403 endif
404 elseif (strcmp (typ, "hggroup"))
405 hgkids = get (hkid, "children");
406 for j = 1 : length (hgkids)
407 hgobj = get (hgkids(j));
408 if (isfield (hgobj, "displayname") && ! isempty (hgobj.displayname))
409 have_dname = true;
410 break; # break from j-loop over hgkids
411 endif
412 endfor
413 if (have_dname)
414 break; # break from k loop over nkids
415 endif
416 endif # elseif hggroup
417 endfor # for loop k = 1 : nkids
418 endif # else branch of if (have_labels)
419
420 if (have_labels || ! have_dname)
360 k = nkids; 421 k = nkids;
422 if (! have_labels)
423 varargin = arrayfun (@(x) sprintf ("data%d", x), [1:nkids]', "uniformoutput", false);
424 nargs = nkids;
425 endif
361 for i = 1 : nargs 426 for i = 1 : nargs
362 arg = varargin{i}; 427 arg = varargin{i};
363 if (ischar (arg)) 428 if (ischar (arg))
364 typ = get (kids(k), "type"); 429 typ = get (kids(k), "type");
365 while (k > 0 430 while (k > 0
371 if (strcmp (get (kids(k), "type"), "hggroup")) 436 if (strcmp (get (kids(k), "type"), "hggroup"))
372 hgkids = get (kids(k), "children"); 437 hgkids = get (kids(k), "children");
373 for j = 1 : length (hgkids) 438 for j = 1 : length (hgkids)
374 hgobj = get (hgkids(j)); 439 hgobj = get (hgkids(j));
375 if (isfield (hgobj, "displayname")) 440 if (isfield (hgobj, "displayname"))
376 set (hgkids(j), "displayname", arg); 441 if (have_labels)
442 set (hgkids(j), "displayname", arg);
443 endif
377 hplots = [hplots, hgkids(j)]; 444 hplots = [hplots, hgkids(j)];
378 text_strings = {text_strings{:}, arg}; 445 text_strings = {text_strings{:}, arg};
379 break; 446 break;
380 endif 447 endif
381 endfor 448 endfor
382 else 449 else
383 set (kids(k), "displayname", arg); 450 if (have_labels)
451 set (kids(k), "displayname", arg);
452 endif
384 hplots = [hplots, kids(k)]; 453 hplots = [hplots, kids(k)];
385 text_strings = {text_strings{:}, arg}; 454 text_strings = {text_strings{:}, arg};
386 endif 455 endif
387 456
388 if (--k == 0) 457 if (--k == 0)
393 endif 462 endif
394 else 463 else
395 error ("legend: expecting argument to be a character string"); 464 error ("legend: expecting argument to be a character string");
396 endif 465 endif
397 endfor 466 endfor
398 if (i < nargs) 467 if (have_labels && i < nargs)
399 warning ("legend: ignoring extra labels"); 468 warning ("legend: ignoring extra labels");
400 endif 469 endif
401 else 470 else
471 ## No labels specified but objects have DisplayName property set.
402 k = nkids; 472 k = nkids;
403 while (k > 0) 473 while (k > 0)
404 typ = get (kids(k), "type"); 474 typ = get (kids(k), "type");
405 while (k > 1 475 while (k > 1
406 && ! (strcmp (typ, "line") || strcmp (typ, "surface") 476 && ! (strcmp (typ, "line") || strcmp (typ, "surface")
992 endif 1062 endif
993 endfunction 1063 endfunction
994 1064
995 1065
996 %!demo 1066 %!demo
997 %! plot (rand (2)) 1067 %! plot (rand (2));
998 %! legend ({'foo'}, 'bar', 'boxoff') 1068 %! title ('legend called with cellstr and string inputs for labels');
999 %! title ('legend() should warn about an extra label') 1069 %! legend ({'foo'}, 'bar');
1000 1070
1001 %!demo 1071 %!demo
1002 %! plot (rand (2,2)) ; 1072 %! plot (rand (3));
1003 %! h = legend ('a', 'b') ; 1073 %! title ('legend() without inputs creates default labels');
1004 %! legend ('right') ; 1074 %! legend ();
1005 %! set (h, 'textposition', 'left') 1075
1006 %! set (h, 'textposition', 'right') 1076 %!demo
1007 %! set (h, 'textcolor', [1 0 1]) 1077 %! clf;
1078 %! x = 0:1;
1079 %! plot (x,x,';I am Blue;', x,2*x, x,3*x,';I am Red;');
1080 %! title ('Blue and Red keys, with Green missing');
1081
1082 %!demo
1083 %! clf;
1084 %! plot (1:10, 1:10, 1:10, fliplr (1:10));
1085 %! title ('incline is blue and decline is green');
1086 %! legend ({'I am blue', 'I am green'}, 'location', 'east');
1087
1088 %!demo
1089 %! clf;
1090 %! plot (1:10, 1:10, 1:10, fliplr (1:10));
1091 %! title ('Legend with keys in horizontal orientation');
1092 %! legend ({'I am blue', 'I am green'}, 'location', 'east', 'orientation', 'horizontal');
1093 %! legend boxoff;
1094
1095 %!demo
1096 %! clf;
1097 %! plot (1:10, 1:10, 1:10, fliplr (1:10));
1098 %! title ('Legend with box off');
1099 %! legend ({'I am blue', 'I am green'}, 'location', 'east');
1100 %! legend boxoff;
1101
1102 %!demo
1103 %! clf;
1104 %! plot (1:10, 1:10, 1:10, fliplr (1:10));
1105 %! title ('Legend with text to the right of key');
1106 %! legend ({'I am blue', 'I am green'}, 'location', 'east');
1107 %! legend right;
1108
1109 %!demo
1110 %! clf;
1111 %! plot (1:10, 1:10, 1:10, fliplr (1:10));
1112 %! title ('Using properties to have legend text shown to the right of key');
1113 %! h = legend ({'I am blue', 'I am green'}, 'location', 'east');
1114 %! legend ('left');
1115 %! set (h, 'textposition', 'right');
1116 %! set (h, 'textcolor', [1 0 1]);
1117
1118 %!demo
1119 %! clf;
1120 %! plot (1:10, 1:10, 1:10, fliplr (1:10));
1121 %! title ('Legend is hidden')
1122 %! legend ({'I am blue', 'I am green'}, 'location', 'east');
1123 %! legend hide;
1008 1124
1009 %!demo 1125 %!demo
1010 %! clf; 1126 %! clf;
1011 %! x = 0:1; 1127 %! x = 0:1;
1012 %! plot (x,x,';I am Blue;', x,2*x,';I am Green;', x,3*x,';I am Red;'); 1128 %! plot (x,x,';I am Blue;', x,2*x,';I am Green;', x,3*x,';I am Red;');
1129 %! title ('labels embedded in call to plot');
1013 %! legend boxon 1130 %! legend boxon
1014 %! legend hide 1131 %! legend hide
1015 %! legend show 1132 %! legend show
1016 1133
1017 %!demo 1134 %!demo
1018 %! clf; 1135 %! clf;
1019 %! x = 0:1; 1136 %! x = 0:1;
1020 %! plot (x, x, ';\alpha;', ... 1137 %! plot (x, x, ';\alpha;', ...
1021 %! x, 2*x, ';\beta=2\alpha;', ... 1138 %! x, 2*x, ';\beta=2\alpha;', ...
1022 %! x, 3*x, ';\gamma=3\alpha;'); 1139 %! x, 3*x, ';\gamma=3\alpha;');
1023 1140 %! title ('labels with interpreted Greek text');
1024 %!demo 1141
1025 %! clf; 1142 %!demo
1026 %! x = 0:1; 1143 %! clf;
1027 %! plot (x,x,';I am Blue;', x,2*x, x,3*x,';I am Red;'); 1144 %! plot (rand (2));
1028 %! title ('Blue and Green keys, with Green missing'); 1145 %! title ('Labels with TeX interpreter turned off');
1029 1146 %! h = legend ('Hello_World', 'foo^bar');
1030 %!demo 1147 %! set (h, 'interpreter', 'none');
1031 %! clf;
1032 %! plot (1:10, 1:10, 1:10, fliplr (1:10));
1033 %! title ('incline is blue and decline is green');
1034 %! legend ({'I am blue', 'I am green'}, 'location', 'east');
1035
1036 %!demo
1037 %! clf;
1038 %! plot (1:10, 1:10, 1:10, fliplr (1:10));
1039 %! title ('Legend is hidden')
1040 %! legend ({'I am blue', 'I am green'}, 'location', 'east');
1041 %! legend hide;
1042
1043 %!demo
1044 %! clf;
1045 %! plot (1:10, 1:10, 1:10, fliplr (1:10));
1046 %! title ('Legend with box off');
1047 %! legend ({'I am blue', 'I am green'}, 'location', 'east');
1048 %! legend boxoff;
1049
1050 %!demo
1051 %! clf;
1052 %! plot (1:10, 1:10, 1:10, fliplr (1:10));
1053 %! title ('Legend with text to the right');
1054 %! legend ({'I am blue', 'I am green'}, 'location', 'east');
1055 %! legend right;
1056 1148
1057 %!demo 1149 %!demo
1058 %! clf; 1150 %! clf;
1059 %! plot (1:10, 1:10); 1151 %! plot (1:10, 1:10);
1060 %! title ('a very long label can sometimes cause problems'); 1152 %! title ('a very long label can sometimes cause problems');
1061 %! legend ('hello world', 'location', 'northeastoutside'); 1153 %! legend ('hello very big world', 'location', 'northeastoutside');
1062 1154
1063 %!demo 1155 %!demo
1064 %! clf; 1156 %! clf;
1065 %! labels = {}; 1157 %! labels = {};
1066 %! colororder = get (gca, 'colororder'); 1158 %! colororder = get (gca, 'colororder');
1068 %! h = plot (1:100, i + rand (100,1)); hold on; 1160 %! h = plot (1:100, i + rand (100,1)); hold on;
1069 %! set (h, 'color', colororder(i,:)); 1161 %! set (h, 'color', colororder(i,:));
1070 %! labels = {labels{:}, ['Signal ', num2str(i)]}; 1162 %! labels = {labels{:}, ['Signal ', num2str(i)]};
1071 %! end 1163 %! end
1072 %! hold off; 1164 %! hold off;
1073 %! title ('Signals with random offset and uniform noise'); 1165 %! title ({'Signals with random offset and uniform noise';
1166 %! 'Legend shown below and outside of plot'});
1074 %! xlabel ('Sample Nr [k]'); ylabel ('Amplitude [V]'); 1167 %! xlabel ('Sample Nr [k]'); ylabel ('Amplitude [V]');
1075 %! legend (labels, 'location', 'southoutside'); 1168 %! legend (labels, 'location', 'southoutside');
1076 1169
1077 %!demo 1170 %!demo
1078 %! clf; 1171 %! clf;
1079 %! x = linspace (0, 10); 1172 %! x = linspace (0, 10);
1080 %! plot (x, x); 1173 %! plot (x, x);
1081 %! hold on; 1174 %! hold on;
1082 %! stem (x, x.^2, 'g'); 1175 %! stem (x, x.^2, 'g');
1176 %! title ('First created object gets first label');
1083 %! legend ('linear'); 1177 %! legend ('linear');
1084 %! hold off; 1178 %! hold off;
1085 1179
1086 %!demo 1180 %!demo
1087 %! clf; 1181 %! clf;
1088 %! x = linspace (0, 10); 1182 %! x = linspace (0, 10);
1089 %! plot (x, x, x, x.^2); 1183 %! plot (x, x, x, x.^2);
1184 %! title ('First created object gets first label');
1090 %! legend ('linear'); 1185 %! legend ('linear');
1091 1186
1092 %!demo 1187 %!demo
1093 %! clf; 1188 %! clf;
1094 %! x = linspace (0, 10); 1189 %! x = linspace (0, 10);
1095 %! plot (x, x, x, x.^2); 1190 %! plot (x, x, x, x.^2);
1191 %! title ('Labels are applied in order of object creation');
1096 %! legend ('linear', 'quadratic'); 1192 %! legend ('linear', 'quadratic');
1097 1193
1098 %!demo 1194 %!demo
1099 %! clf; 1195 %! clf;
1100 %! rand_2x3_data1 = [0.341447, 0.171220, 0.284370; 0.039773, 0.731725, 0.779382]; 1196 %! rand_2x3_data1 = [0.341447, 0.171220, 0.284370; 0.039773, 0.731725, 0.779382];
1101 %! bar (rand_2x3_data1); 1197 %! bar (rand_2x3_data1);
1102 %! ylim ([0 1.0]); 1198 %! ylim ([0 1.0]);
1199 %! title ('legend() works for bar graphs (hgobjects)');
1103 %! legend ({'1st Bar', '2nd Bar', '3rd Bar'}); 1200 %! legend ({'1st Bar', '2nd Bar', '3rd Bar'});
1104 1201
1105 %!demo 1202 %!demo
1106 %! clf; 1203 %! clf;
1107 %! rand_2x3_data2 = [0.44804, 0.84368, 0.23012; 0.72311, 0.58335, 0.90531]; 1204 %! rand_2x3_data2 = [0.44804, 0.84368, 0.23012; 0.72311, 0.58335, 0.90531];
1108 %! bar (rand_2x3_data2); 1205 %! bar (rand_2x3_data2);
1109 %! ylim ([0 1.2]); 1206 %! ylim ([0 1.2]);
1207 %! title ('legend() works for bar graphs (hgobjects)');
1110 %! legend ('1st Bar', '2nd Bar', '3rd Bar'); 1208 %! legend ('1st Bar', '2nd Bar', '3rd Bar');
1111 %! legend right; 1209 %! legend right;
1112 1210
1113 %!demo 1211 %!demo
1114 %! clf; 1212 %! clf;
1122 %! x = 0:0.1:10; 1220 %! x = 0:0.1:10;
1123 %! plot (x, sin (x), ';sin (x);'); 1221 %! plot (x, sin (x), ';sin (x);');
1124 %! hold all; 1222 %! hold all;
1125 %! plot (x, cos (x), ';cos (x);'); 1223 %! plot (x, cos (x), ';cos (x);');
1126 %! hold off; 1224 %! hold off;
1225 %! title ('legend constructed from multiple plot calls');
1127 1226
1128 %!demo 1227 %!demo
1129 %! clf; 1228 %! clf;
1130 %! x = 0:0.1:10; 1229 %! x = 0:0.1:10;
1131 %! plot (x, sin (x), ';sin (x);'); 1230 %! plot (x, sin (x), ';sin (x);');
1132 %! hold all; 1231 %! hold all;
1133 %! plot (x, cos (x), ';cos (x);'); 1232 %! plot (x, cos (x), ';cos (x);');
1134 %! hold off; 1233 %! hold off;
1135 %! legend ({'sin (x)', 'cos (x)'}, 'location', 'northeastoutside'); 1234 %! title ('Specified label text overrides previous labels');
1235 %! legend ({'Sine', 'Cosine'}, 'location', 'northeastoutside');
1136 1236
1137 %!demo 1237 %!demo
1138 %! clf; 1238 %! clf;
1139 %! x = 0:10; 1239 %! x = 0:10;
1140 %! plot (x, rand (11)); 1240 %! plot (x, rand (11));
1141 %! xlabel ('Indices'); 1241 %! xlabel ('Indices');
1142 %! ylabel ('Random Values'); 1242 %! ylabel ('Random Values');
1143 %! title ('Legend ''off'' should delete the legend'); 1243 %! title ('Legend ''off'' deletes the legend');
1144 %! legend (cellstr (num2str ((1:10)')), 'location', 'northeastoutside'); 1244 %! legend (cellstr (num2str ((1:10)')), 'location', 'northeastoutside');
1145 %! legend off; 1245 %! legend off;
1146 %! axis ([0, 10, 0 1]); 1246 %! axis ([0, 10, 0 1]);
1147 1247
1148 %!demo 1248 %!demo
1162 %! legend (cellstr (num2str (x)), 'location', 'southeastoutside'); 1262 %! legend (cellstr (num2str (x)), 'location', 'southeastoutside');
1163 1263
1164 %!demo 1264 %!demo
1165 %! clf; 1265 %! clf;
1166 %! plot (rand (2)); 1266 %! plot (rand (2));
1167 %! title ('Warn of extra labels'); 1267 %! title ('legend() will warn if extra labels are specified');
1168 %! legend ('Hello', 'World', 'interpreter', 'foobar'); 1268 %! legend ('Hello', 'World', 'interpreter', 'foobar');
1169
1170 %!demo
1171 %! clf;
1172 %! plot (rand (2));
1173 %! title ('Turn off TeX interpreter');
1174 %! h = legend ('Hello_World', 'foo^bar');
1175 %! set (h, 'interpreter', 'none');
1176 1269
1177 %!demo 1270 %!demo
1178 %! x = 0:10; 1271 %! x = 0:10;
1179 %! y1 = rand (size (x)); 1272 %! y1 = rand (size (x));
1180 %! y2 = rand (size (x)); 1273 %! y2 = rand (size (x));
1181 %! [ax, h1, h2] = plotyy (x, y1, x, y2); 1274 %! [ax, h1, h2] = plotyy (x, y1, x, y2);
1275 %! title ('plotyy legend test #1: Blue and Green labels');
1182 %! legend ([h1, h2], {'Blue', 'Green'}, 'location', 'south'); 1276 %! legend ([h1, h2], {'Blue', 'Green'}, 'location', 'south');
1183 1277
1184 %!demo 1278 %!demo
1185 %! x = 0:10; 1279 %! x = 0:10;
1186 %! y1 = rand (size (x)); 1280 %! y1 = rand (size (x));
1187 %! y2 = rand (size (x)); 1281 %! y2 = rand (size (x));
1188 %! [ax, h1, h2] = plotyy (x, y1, x, y2); 1282 %! [ax, h1, h2] = plotyy (x, y1, x, y2);
1283 %! title ('plotyy legend test #2: Blue and Green labels');
1189 %! legend ({'Blue', 'Green'}, 'location', 'south'); 1284 %! legend ({'Blue', 'Green'}, 'location', 'south');
1190 1285
1191 %!demo 1286 %!demo
1192 %! x = 0:10; 1287 %! x = 0:10;
1193 %! y1 = rand (size (x)); 1288 %! y1 = rand (size (x));
1194 %! y2 = rand (size (x)); 1289 %! y2 = rand (size (x));
1195 %! [ax, h1, h2] = plotyy (x, y1, x, y2); 1290 %! [ax, h1, h2] = plotyy (x, y1, x, y2);
1291 %! title ('plotyy legend test #3: Blue and Green labels');
1196 %! legend ('Blue', 'Green', 'location', 'south'); 1292 %! legend ('Blue', 'Green', 'location', 'south');
1197 1293