File: __gnuplot_legend__.m

package info (click to toggle)
octave 9.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 144,300 kB
  • sloc: cpp: 332,784; ansic: 77,239; fortran: 20,963; objc: 9,396; sh: 8,213; yacc: 4,925; lex: 4,389; perl: 1,544; java: 1,366; awk: 1,259; makefile: 648; xml: 189
file content (1860 lines) | stat: -rw-r--r-- 64,758 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
########################################################################
##
## Copyright (C) 2010-2024 The Octave Project Developers
##
## See the file COPYRIGHT.md in the top-level directory of this
## distribution or <https://octave.org/copyright/>.
##
## This file is part of Octave.
##
## Octave is free software: you can redistribute it and/or modify it
## under the terms of the GNU General Public License as published by
## the Free Software Foundation, either version 3 of the License, or
## (at your option) any later version.
##
## Octave is distributed in the hope that it will be useful, but
## WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with Octave; see the file COPYING.  If not, see
## <https://www.gnu.org/licenses/>.
##
########################################################################

## -*- texinfo -*-
## @deftypefn  {} {} legend ()
## @deftypefnx {} {} legend (@var{str1}, @var{str2}, @dots{})
## @deftypefnx {} {} legend (@var{charmat})
## @deftypefnx {} {} legend (@{@var{cellstr}@})
## @deftypefnx {} {} legend (@dots{}, "location", @var{pos})
## @deftypefnx {} {} legend (@dots{}, "orientation", @var{orient})
## @deftypefnx {} {} legend (@var{hax}, @dots{})
## @deftypefnx {} {} legend (@var{hobjs}, @dots{})
## @deftypefnx {} {} legend (@var{hax}, @var{hobjs}, @dots{})
## @deftypefnx {} {} legend ("@var{option}")
## @deftypefnx {} {} legend (@dots{}, @{@var{cellstr}@}, @var{property}, @var{value}, @dots{})
## @deftypefnx {} {[@var{hleg}, @var{hleg_obj}, @var{hplot}, @var{labels}] =} legend (@dots{})
##
## Display a legend for the current axes using the specified strings as labels.
##
## Legend entries may be specified as individual character string arguments,
## a character array, or a cell array of character strings.  When label names
## might be confused with options to @code{legend}, the labels should be
## protected by specifying them as a cell array of strings.
##
## If the first argument @var{hax} is an axes handle, then add a legend to this
## axes, rather than the current axes returned by @code{gca}.
##
## Legend labels are associated with the axes' children; The first label is
## assigned to the first object that was plotted in the axes, the second label
## to the next object plotted, etc.  To label specific data objects, without
## labeling all objects, provide their graphic handles in the input
## @var{hobjs}.
##
## The optional parameter @var{pos} specifies the location of the legend as
## follows:
##
## @multitable @columnfractions 0.06 0.14 0.80
## @headitem @tab pos @tab location of the legend
## @item @tab north @tab center top
## @item @tab south @tab center bottom
## @item @tab east @tab right center
## @item @tab west @tab left center
## @item @tab northeast @tab right top (default)
## @item @tab northwest @tab left top
## @item @tab southeast @tab right bottom
## @item @tab southwest @tab left bottom
## @sp 1
## @item @tab outside @tab can be appended to any location string @*
## @item @tab         @tab which will place the legend outside the axes
## @end multitable
##
## The optional parameter @var{orient} determines if the legend elements are
## placed vertically or horizontally.  The allowed values are
## @qcode{"vertical"} (default) or @qcode{"horizontal"}.
##
## The following customizations are available using @var{option}:
##
## @table @asis
## @item @qcode{"show"}
##   Show legend on the plot
##
## @item @qcode{"hide"}
##   Hide legend on the plot
##
## @item @qcode{"toggle"}
##   Toggle between @qcode{"hide"} and @qcode{"show"}
##
## @item @qcode{"boxon"}
##   Show a box around legend (default)
##
## @item @qcode{"boxoff"}
##   Hide the box around legend
##
## @item @qcode{"right"}
##   Place label text to the right of the keys (default)
##
## @item @qcode{"left"}
##   Place label text to the left of the keys
##
## @item @qcode{"off"}
##   Delete the legend object
## @end table
##
## The @code{legend} function creates a graphics object which has various
## properties that can be manipulated with @code{get}/@code{set}.
## Alternatively, properties can be set directly when calling @code{legend} by
## including @var{property}/@var{value} pairs.  If using this calling form, the
## labels must be specified as a cell array of strings.  Graphics object
## properties are documented in detail at @ref{Graphics Object Properties}.
##
## The optional output values are
##
## @table @var
## @item hleg
##   The graphics handle of the legend object.
##
## @item hleg_obj
##   Graphics handles to the text, patch, and line objects which form the
##   legend.
##
## @item hplot
##   Graphics handles to the plot objects which were used in making the legend.
##
## @item labels
##   A cell array of strings of the labels in the legend.
## @end table
##
## Implementation Note: The legend label text is either provided in the call to
## @code{legend} or is taken from the @code{DisplayName} property of the
## graphics objects.  Only data objects, such as line, patch, and surface, have
## this property whereas axes, figures, etc.@: do not so they are never present
## in a legend.  If no labels or @code{DisplayName} properties are available,
## then the label text is simply @qcode{"data1"}, @qcode{"data2"}, @dots{},
## @nospell{@qcode{"dataN"}}.  No more than 20 data labels will be
## automatically generated.  To label more, call @code{legend} explicitly and
## provide all labels.
##
## The legend @code{FontSize} property is initially set to 90% of the axes
## @code{FontSize} to which it is attached.  Use @code{set} to override this
## if necessary.
##
## A legend is implemented as an additional axes object with the @code{tag}
## property set to @qcode{"legend"}.  Properties of the legend object may be
## manipulated directly by using @code{set}.
## @end deftypefn

function [hleg, hleg_obj, hplot, labels] = __gnuplot_legend__ (varargin)

  if (nargin > 0
      && (! ishghandle (varargin{1})
          || (strcmp (get (varargin{1}, "type"), "axes")
              && ! strcmp (get (varargin{1}, "tag"), "legend"))))
    [ca, varargin, nargin] = __plt_get_axis_arg__ ("legend", varargin{:});
    if (isempty (ca))
      ca = gca ();
    endif
    hfig = ancestor (ca, "figure");
  else
    hfig = get (0, "currentfigure");
    if (isempty (hfig))
      hfig = gcf ();
    endif
    ca = gca ();
  endif

  ## Special handling for plotyy which has two axes objects
  if (isprop (ca, "__plotyy_axes__"))
    plty = get (ca, "__plotyy_axes__");
    ca = [ca, plty.'];
    ## Remove duplicates while preserving order
    [~, n] = unique (ca, "first");
    ca = ca(sort (n));
  endif

  if (nargin > 0 && all (ishghandle (varargin{1})))
    ## List of plot objects to label given as first argument
    kids = flipud (varargin{1}(:));
    varargin(1) = [];
  else
    ## Find list of plot objects from axes "children"
    kids = ca;
    kids(strcmp (get (ca, "tag"), "legend")) = [];
    if (isscalar (kids))
      kids = get (kids, "children")(:);
    else
      kids = vertcat (flipud (get (kids, "children")){:});
    endif
  endif
  nargs = numel (varargin);
  nkids = numel (kids);

  ## Find any existing legend object associated with axes
  hlegend = [];
  for hax = ca
    try
      hlegend = get (hax, "__legend_handle__");
      if (! isempty (hlegend))
        break;
      endif
    end_try_catch
  endfor

  orientation = "default";
  location = "default";
  show = "create";
  textpos = "default";
  box = "default";
  delete_leg = false;
  find_leg_hdl = (nargs == 0);  # possibly overridden
  propvals = {};

  ## Find "location", "orientation", "textposition" property/value pairs
  foundpos = foundorient = foundtextpos = false;
  i = nargs - 1;
  while (i > 0)
    pos = varargin{i};
    str = varargin{i+1};
    if (strcmpi (pos, "location") && ischar (str))
      if (! foundpos)
        location = lower (str);
        foundpos = true;
      endif
      varargin(i:i+1) = [];
      nargs -= 2;
    elseif (strcmpi (pos, "orientation") && ischar (str))
      if (! foundorient)
        orientation = lower (str);
        foundorient = true;
      endif
      varargin(i:i+1) = [];
      nargs -= 2;
    elseif (strcmpi (pos, "textposition") && ischar (str))
      if (! foundtextpos)
        textpos = lower (str);
        foundtextpos = true;
      endif
      varargin(i:i+1) = [];
      nargs -= 2;
    endif
    i -= 2;
  endwhile

  ## Validate the orientation
  if (! any (strcmp (orientation, {"vertical", "horizontal", "default"})))
    error ("legend: unrecognized legend orientation");
  endif

  ## Validate the texposition
  if (! any (strcmp (textpos, {"left", "right", "default"})))
    error ("legend: unrecognized legend textposition");
  endif

  ## Validate the location type
  outside = false;
  inout = strfind (location, "outside");
  if (! isempty (inout))
    outside = true;
    location = location(1:inout-1);
  else
    outside = false;
  endif

  switch (location)
    case {"north", "south", "east", "west", "northeast", "northwest", ...
          "southeast", "southwest", "default"}
      ## These are all valid locations, do nothing.

    case "best"
      if (outside)
        if (strcmp (orientation, "horizontal"))
          location = "south";
        else
          location = "northeast";
        endif
      else
        warning ("legend: 'best' not yet implemented for location specifier, using 'northeast' instead\n");
        location = "northeast";
      endif

    case "none"
      ## FIXME: Should there be any more error checking on this?

    otherwise
      error ("legend: unrecognized legend location");
  endswitch

  ## Finish input processing based on number of inputs
  if (nargs == 0)
    ## No labels given, create a new legend or return existing one
    if (isempty (hlegend))
      show = "create";
      textpos = "right";
      find_leg_hdl = false;
    endif

  elseif (nargs == 1)
    ## Either OPTION value, single string label, or cellstr of labels.
    arg = varargin{1};
    if (ischar (arg))
      if (rows (arg) == 1)
        str = lower (strtrim (arg));
        switch (str)
          case "off"
            delete_leg = true;
          case "hide"
            show = "off";
            nargs -= 1;
          case "show"
            if (! isempty (hlegend))
              show = "on";
            else
              show = "create";
              textpos = "right";
            endif
            nargs -= 1;
          case "toggle"
            if (isempty (hlegend))
              show = "create";
              textpos = "right";
            elseif (strcmp (get (hlegend, "visible"), "off"))
              show = "on";
            else
              show = "off";
            endif
            nargs -= 1;
          case "boxon"
            box = "on";
            nargs -= 1;
          case "boxoff"
            box = "off";
            nargs -= 1;
          case "left"
            textpos = "left";
            nargs -= 1;
          case "right"
            textpos = "right";
            nargs -= 1;
        endswitch
      else
        ## Character matrix of labels
        varargin = cellstr (arg);
        nargs = numel (varargin);
      endif
    elseif (iscellstr (arg))
      ## Cell array of labels
      varargin = arg;
      nargs = numel (varargin);
    else
      error ("Octave:invalid-fun-call",
             "legend: single argument must be a string or cellstr");
    endif

  elseif (nargs > 1 && iscellstr (varargin{1}))
    ## Cell array of labels followed by property/value pairs
    propvals = varargin(2:end);
    if (rem (numel (propvals), 2) != 0)
      error ("legend: PROPERTY/VALUE arguments must occur in pairs");
    endif
    varargin = {varargin{1}{:}};
    nargs = numel (varargin);
  endif

  have_labels = (nargs > 0);
  hobjects = [];
  hplots = [];
  text_strings = {};

  if (delete_leg)
    delete (hlegend);
    hlegend = [];
  elseif (find_leg_hdl)
    ## Don't change anything about legend.
    ## hleg output will be assigned hlegend value at end of function.
  elseif (strcmp (show, "off"))
    if (! isempty (hlegend))
      set (hlegend, "visible", "off");
      hlegend = [];
    endif
  elseif (strcmp (show, "on"))
    if (! isempty (hlegend))
      set (hlegend, "visible", "on");
      ## NOTE: Matlab sets both "visible" and "box" to "on" for "show on"
      ## set (hlegend, "box", "on");
    endif
  elseif (strcmp (box, "on"))
    if (! isempty (hlegend))
      set (hlegend, "box", "on");
    endif
  elseif (strcmp (box, "off"))
    if (! isempty (hlegend))
      set (hlegend, "box", "off");
    endif
  elseif (! have_labels && ! isempty (hlegend)
          && ! (strcmp (location, "default")
                && strcmp (orientation, "default")))
    ## Changing location or orientation of existing legend
    if (strcmp (location, "default"))
      set (hlegend, "orientation", orientation);
    elseif (strcmp (orientation, "default"))
      if (outside)
        set (hlegend, "location", [location "outside"]);
      else
        set (hlegend, "location", location);
      endif
    else
      if (outside)
        set (hlegend, "location", [location "outside"],
                      "orientation", orientation);
      else
        set (hlegend, "location", location,
                      "orientation", orientation);
      endif
    endif
  else
    ## Create or modify legend object

    if (! isempty (hlegend))
      ## Disable callbacks while modifying an existing legend
      setappdata (hlegend, "nocallbacks", true);
    endif

    if (have_labels)
      ## Check for valid data that can be labeled.
      have_data = false;
      have_dname = false;
      for hkid = kids.'
        typ = get (hkid, "type");
        if (any (strcmp (typ, {"line", "patch", "surface", "hggroup"})))
          have_data = true;
          break;
        endif
      endfor

      if (! have_data)
        warning ("legend: plot data is empty; setting key labels has no effect");
      endif
    else
      ## No labels.  Search for DisplayName property.
      have_dname = false;
      n_dname = 0;
      for hkid = kids.'
        typ = get (hkid, "type");
        if (any (strcmp (typ, {"line", "patch", "surface", "hggroup"})))
          n_dname += 1;  # count of objects which could be labeled
          if (! isempty (get (hkid, "displayname")))
            have_dname = true;
            break;
          endif
        endif
      endfor
      have_data = n_dname > 0;
    endif

    if (have_labels || ! have_dname)
      k = nkids;
      if (! have_labels)
        ## No labels or DisplayName.  Create set of "dataX" labels.
        if (n_dname > 20)
          warning ("legend: labeling only first 20 data objects");
          n_dname = 20;
        endif
        nargs = n_dname;
        varargin = arrayfun (@(x) sprintf ("data%d", x), [1:nargs]',
                             "uniformoutput", false);
        have_labels = true;
      endif
      for i = 1 : nargs
        label = varargin{i};
        if (! ischar (label))
          error ("Octave:invalid-fun-call",
                 "legend: expecting label to be a string");
        endif
        ## Locate an object which can be labeled
        while (k > 0)
          typ = get (kids(k), "type");
          if (any (strcmp (typ, {"line","patch","surface","hggroup"})))
            break;
          endif
          k--;
        endwhile
        if (k > 0)
          set (kids(k), "displayname", label);
          hplots(end+1) = kids(k);
          text_strings(end+1) = label;
          k--;
        else
          if (have_data)
            warning ("legend: ignoring extra labels");
          endif
          break;  # k = 0, no further handles to process
        endif
      endfor

    else
      ## No labels specified but objects have DisplayName property set.
      k = nkids;
      while (k > 0)
        ## Locate object to label
        while (k > 0)
          typ = get (kids(k), "type");
          if (any (strcmp (typ, {"line","patch","surface","hggroup"})))
            break;
          endif
          k--;
        endwhile
        if (k > 0)
          dname = get (kids(k), "displayname");
          if (! isempty (dname))
            hplots(end+1) = kids(k);
            text_strings(end+1) = dname;
          endif
          k--;
        endif
      endwhile
    endif

    if (isempty (hplots))
      ## Nothing to label
      if (! isempty (hlegend))
        delete (hlegend);
        hlegend = [];
        hobjects = [];
        hplots = [];
        text_strings = {};
      endif
    else
      ## Preserve the old legend if it exists
      if (! isempty (hlegend))
        if (strcmp (textpos, "default"))
          textpos = get (hlegend, "textposition");
        endif
        if (strcmp (location, "default"))
          location = get (hlegend, "location");
          inout = strfind (location, "outside");
          if (! isempty (inout))
            outside = true;
            location = location(1:inout-1);
          else
            outside = false;
          endif
        endif
        if (strcmp (orientation, "default"))
          orientation = get (hlegend, "orientation");
        endif
        box = get (hlegend, "box");
      else
        if (strcmp (textpos, "default"))
          textpos = "right";
        endif
        if (strcmp (location, "default"))
          location = "northeast";
        endif
        if (strcmp (orientation, "default"))
          orientation = "vertical";
        endif
        box = "on";
      endif

      ## Use axis which is appropriate for legend location.
      ## This is only necessary for plotyy figures where there are two axes.
      if (numel (ca) == 1)
        cax = ca(1);
      elseif (strfind (location, "east"))
        cax = ca(2);
      else
        cax = ca(1);
      endif
      ## Get axis size and fontsize in points.
      ## Rely on listener to handle conversion.
      units = get (cax, "units");
      unwind_protect
        set (cax, "units", "points", "fontunits", "points");
        if (isempty (hlegend) || ! isprop (hlegend, "unmodified_axes_position"))
          unmodified_axes_position = get (cax, "position");
          unmodified_axes_outerposition = get (cax, "outerposition");
        else
          unmodified_axes_position = get (hlegend, "unmodified_axes_position");
          unmodified_axes_outerposition = get (hlegend, ...
                                               "unmodified_axes_outerposition");
        endif
        ca_pos = unmodified_axes_position;
        ca_outpos = unmodified_axes_outerposition;
        tightinset = get (ca(1), "tightinset");
        for i = 2 : numel (ca)
          tightinset = max (tightinset, get (ca(i), "tightinset"));
        endfor
      unwind_protect_cleanup
        set (cax, "units", units);
      end_unwind_protect

      ## Padding between legend entries horizontally and vertically
      ## measured in points.
      ## FIXME: 3*xpad must be integer or strange off-by-1 pixel issues
      ##        with lines in OpenGL.
      xpad = 2 + 1/3;
      ypad = 4;

      bpad = 8;  # padding of legend box from surrounding axes

      linelength = 15;

      ## Preamble code to restore figure and axes after legend creation
      origfig = get (0, "currentfigure");
      if (origfig != hfig)
        set (0, "currentfigure", hfig);
      else
        origfig = [];
      endif
      origaxes = get (hfig, "currentaxes");
      unwind_protect
        ud = ancestor (hplots, "axes");
        if (! isscalar (ud))
          ud = unique ([ud{:}]);
        endif
        hpar = get (ud(1), "parent");

        if (isempty (hlegend))
          ## Create a legend object (axes + new properties)
          addprops = true;
          hlegend = axes ("parent", hpar, "tag", "legend",
                          "box", box,
                          "xtick", [], "ytick", [],
                          "xlim", [0, 1], "ylim", [0, 1],
                          "positionconstraint", "innerposition");
          setappdata (hlegend, "__axes_handle__", ud);
          try
            addproperty ("__legend_handle__", ud(1), "handle", hlegend);
          catch
            set (ud(1), "__legend_handle__", hlegend);
          end_try_catch

          ## Inherit fontsize from current axis
          ## "fontunits" should be first because it affects interpretation
          ## of "fontsize" property.
          [fontunits, fontsz] = get (ca(1), {"fontunits", "fontsize"}){:};
          fontsz *= 0.90;  # Reduce legend fontsize to 90% of axes fontsize
          set (hlegend, {"fontunits", "fontsize"}, {fontunits, fontsz});
          set (hlegend, "fontunits", "points");  # legend always works in pts.
          ## Also inherit colormap from axes if it is different than figure
          cax_cmap = get (cax, "colormap");
          if (! isequal (cax_cmap, get (hpar, "colormap")))
            set (hlegend, "colormap", cax_cmap);
          endif
          old_hplots = [];
        else
          ## Re-use existing legend.
          addprops = false;
          axes (hlegend);
          delete (get (hlegend, "children"));
          ## Hack: get list of hplots for which addlistener has been called.
          old_hplots = get (hlegend, "deletefcn"){6};
        endif

        if (addprops)
          ## Only required for a newly created legend object
          ## FIXME: "autoupdate" is not implemented.
          addproperty ("autoupdate", hlegend, "radio", "{on}|off");
          addproperty ("edgecolor", hlegend, "color", [0.15, 0.15, 0.15]);
          addproperty ("textcolor", hlegend, "color", [0, 0, 0]);
          locations = {"north", "south", "east", "west", ...
                       "{northeast}", "southeast", "northwest", "southwest", ...
                       "northoutside", "southoutside", ...
                       "eastoutside", "westoutside", ...
                       "northeastoutside", "southeastoutside", ...
                       "northwestoutside", "southwestoutside", "best", ...
                       "bestoutside", "none"};
          addproperty ("location", hlegend, "radio", strjoin (locations, "|"));
          addproperty ("orientation", hlegend, "radio",
                       "{vertical}|horizontal");
          addproperty ("string", hlegend, "any", text_strings);
          addproperty ("interpreter", hlegend, "textinterpreter");
          addproperty ("textposition", hlegend, "radio", "left|{right}");
        endif

        ## Apply any PROPERTY/VALUE pairs given as arguments
        if (! isempty (propvals))
          set (hlegend, propvals{:});
        endif

        ## Special case of PROPERTY "edgecolor" (bug #56968)
        ec_idx = find (strcmpi (propvals, "edgecolor"), 1, "last");
        if (! isempty (ec_idx))
          ec_color = propvals{ec_idx + 1};
          set (hlegend, "xcolor", ec_color, "ycolor", ec_color);
        endif

        ## Text objects in key inherit visual properties from legend object
        legprops = { "fontunits", "fontangle", "fontname", "fontsize", ...
                     "fontweight", "interpreter", "textcolor" };

        txtprops = { "fontunits", [], "fontangle", [] "fontname", [], ...
                     "fontsize", [], "fontweight", [] "interpreter", [], ...
                     "color", [] };
        propvals = get (hlegend, legprops);
        txtprops(2:2:end) = propvals;

        ## Add text labels to the axes first and check their extents
        nentries = numel (hplots);
        texthandle = [];
        maxwidth = maxheight = 0;
        for k = 1 : nentries
          halign = ifelse (strcmp (textpos, "right"), "left", "right");
          texthandle(k) = text (0, 0, text_strings{k},
                                "units", "points",
                                "horizontalalignment", halign,
                                txtprops{:});
          setappdata (texthandle(k), "handle", hplots(k));
          extents = get (texthandle(k), "extent");
          maxwidth = max (maxwidth, extents(3));
          maxheight = max (maxheight, extents(4));
        endfor
        ## Restore units which were forced to points
        set (texthandle, "units", get (0, "DefaultTextUnits"));

        num1 = nentries;
        if (strcmp (orientation, "vertical"))
          height = nentries * (ypad + maxheight);
          if (outside)
            if (height > ca_pos(4))
              ## Avoid shrinking the height of the axis to zero if outside
              num1 = ca_pos(4) / (maxheight + ypad) / 2;
            endif
          else
            if (height > 0.9 * ca_pos(4))
              num1 = 0.9 * ca_pos(4) / (maxheight + ypad);
            endif
          endif
        else
          width = nentries * (ypad + maxwidth);
          if (outside)
            if (width > ca_pos(3))
              ## Avoid shrinking the width of the axis to zero if outside
              num1 = ca_pos(3) / (maxwidth + ypad) / 2;
            endif
          else
            if (width > 0.9 * ca_pos(3))
              num1 = 0.9 * ca_pos(3) / (maxwidth + ypad);
            endif
          endif
        endif
        num2 = ceil (nentries / num1);

        ## Layout is [xpad, linelength, xpad, maxwidth, xpad]
        xstep = 3 * xpad + (maxwidth + linelength);
        if (strcmp (textpos, "right"))
          xoffset = xpad;
          txoffset = 2 * xpad + linelength;
        else
          xoffset = 2 * xpad + maxwidth;
          txoffset = xpad + maxwidth;
        endif
        ystep = (ypad + maxheight);
        yoffset = ystep / 2;

        ## Place the legend in the desired location
        if (strcmp (orientation, "vertical"))
          lpos = [0, 0, num2 * xstep, num1 * ystep];
        else
          lpos = [0, 0, num1 * xstep, num2 * ystep];
        endif

        gnuplot = strcmp (get (hfig, "__graphics_toolkit__"), "gnuplot");
        if (gnuplot)
          ## gnuplot places the key (legend) at edge of the figure window.
          ## OpenGL places the legend box at edge of the unmodified axes
          ## position.
          if (isempty (strfind (location, "east")))
            gnuplot_offset = unmodified_axes_outerposition(1) ...
                           + unmodified_axes_outerposition(3) ...
                           - unmodified_axes_position(1) ...
                           - unmodified_axes_position(3);
          else
            gnuplot_offset = unmodified_axes_position(1) ...
                           - unmodified_axes_outerposition(1);
          endif
          ## FIXME: The "fontsize" is added to match the behavior of OpenGL.
          ## This implies that a change in fontsize should trigger a listener
          ## to update the legend.  The "2" was determined using a long legend
          ## key in the absence of any subplots.
          gnuplot_offset -= 2 * get (hlegend, "fontsize");
        else
          gnuplot_offset = 0;
        endif

        ## For legend's outside the associated axes position,
        ## align their edge to the unmodified_axes_outerposition,
        ## and adjust the axes position accordingly.
        switch (location)
          case "north"
            if (outside)
              lpos = [ca_pos(1) + (ca_pos(3) - lpos(3)) / 2, ...
                      ca_outpos(2) + ca_outpos(4) - lpos(4) - bpad/2, ...
                      lpos(3), lpos(4)];

              new_pos = [ca_pos(1), ca_pos(2), ca_pos(3), ca_pos(4) - lpos(4)];
            else
              lpos = [ca_pos(1) + (ca_pos(3) - lpos(3)) / 2, ...
                      ca_pos(2) + ca_pos(4) - lpos(4) - bpad, ...
                      lpos(3), lpos(4)];
            endif
          case "south"
            if (outside)
              lpos = [ca_pos(1) + (ca_pos(3) - lpos(3)) / 2, ...
                      ca_outpos(2) + bpad/2, lpos(3), lpos(4)];
              new_pos = [ca_pos(1), ...
                         lpos(2) + lpos(4) + bpad/2 + tightinset(2), ...
                         ca_pos(3), ca_pos(4) - lpos(4)];
            else
              lpos = [ca_pos(1) + (ca_pos(3) - lpos(3)) / 2, ...
                      ca_pos(2) + bpad, lpos(3), lpos(4)];
            endif
          case "east"
            if (outside)
              lpos = [ca_outpos(1) + ca_outpos(3) - lpos(3) - bpad/2, ...
                      ca_pos(2) + (ca_pos(4) - lpos(4)) / 2, ...
                      lpos(3), lpos(4)];
              new_pos = [ca_pos(1), ca_pos(2), ...
                         lpos(1) - bpad - tightinset(3) - ca_pos(1), ...
                         ca_pos(4)];
              new_pos(3) += gnuplot_offset;
            else
              lpos = [ca_pos(1) + ca_pos(3) - lpos(3) - bpad, ...
                      ca_pos(2) + (ca_pos(4) - lpos(4)) / 2, lpos(3), lpos(4)];
            endif
          case "west"
            if (outside)
              lpos = [ca_outpos(1) + bpad/2, ...
                      ca_pos(2) + (ca_pos(4) - lpos(4)) / 2, ...
                      lpos(3), lpos(4)];
              new_pos = [lpos(1) + lpos(3) + bpad/2 + tightinset(1), ...
                         ca_pos(2), ca_pos(3) - lpos(3) - bpad/2, ca_pos(4)];
              new_pos([1, 3]) += [-gnuplot_offset, gnuplot_offset];
            else
              lpos = [ca_pos(1) + bpad, ...
                      ca_pos(2) + (ca_pos(4) - lpos(4)) / 2, lpos(3), lpos(4)];
            endif
          case "northeast"
            if (outside)
              lpos = [ca_outpos(1) + ca_outpos(3) - lpos(3) - bpad/2, ...
                      ca_pos(2) + ca_pos(4) - lpos(4), ...
                      lpos(3), lpos(4)];
              new_pos = [ca_pos(1), ca_pos(2), ...
                         lpos(1) - bpad - tightinset(3) - ca_pos(1), ...
                         ca_pos(4)];
              new_pos(3) += gnuplot_offset;
            else
              lpos = [ca_pos(1) + ca_pos(3) - lpos(3) - bpad, ...
                      ca_pos(2) + ca_pos(4) - lpos(4) - bpad, ...
                      lpos(3), lpos(4)];
            endif
          case "northwest"
            if (outside)
              lpos = [ca_outpos(1) + bpad/2, ...
                      ca_pos(2) + ca_pos(4) - lpos(4), ...
                      lpos(3), lpos(4)];
              new_pos = [lpos(1) + lpos(3) + bpad/2 + tightinset(1), ...
                         ca_pos(2), ca_pos(3) - lpos(3) - bpad/2, ca_pos(4)];
              new_pos([1, 3]) += [-gnuplot_offset, gnuplot_offset];
            else
              lpos = [ca_pos(1) + bpad, ...
                      ca_pos(2) + ca_pos(4) - lpos(4) - bpad, ...
                      lpos(3), lpos(4)];
            endif
          case "southeast"
            if (outside)
              lpos = [ca_outpos(1) + ca_outpos(3) - lpos(3) - bpad/2, ...
                      ca_pos(2), lpos(3), lpos(4)];
              new_pos = [ca_pos(1), ca_pos(2), ...
                         lpos(1) - bpad - ca_pos(1) - tightinset(3), ...
                         ca_pos(4)];
              new_pos(3) += gnuplot_offset;
            else
              lpos = [ca_pos(1) + ca_pos(3) - lpos(3) - bpad, ...
                      ca_pos(2) + bpad, lpos(3), lpos(4)];
            endif
          case "southwest"
            if (outside)
              lpos = [ca_outpos(1) + bpad/2, ca_pos(2), lpos(3), lpos(4)];
              new_pos = [lpos(1) + lpos(3) + bpad/2 + tightinset(1), ...
                         ca_pos(2), ca_pos(3) - lpos(3) - bpad/2, ca_pos(4)];
              new_pos([1, 3]) += [-gnuplot_offset, gnuplot_offset];
            else
              lpos = [ca_pos(1) + bpad, ca_pos(2) + bpad, lpos(3), lpos(4)];
            endif
        endswitch

        units = get (hlegend, "units");
        unwind_protect
          set (hlegend, "units", "points", "position", lpos);
        unwind_protect_cleanup
          set (hlegend, "units", units);
        end_unwind_protect

        ## Now write the line segments and place the text objects correctly
        xk = yk = 0;
        for k = 1 : numel (hplots)
          hobjects(end+1) = texthandle(k);
          hplt = hplots(k);
          typ = get (hplt, "type");
          ## For an hggroup, find an underlying primitive object
          if (strcmp (typ, "hggroup"))
            for hgkid = get (hplt, "children").'
              hgkid_type = get (hgkid, "type");
              if (any (strcmp (hgkid_type, {"line","patch","surface"})))
                typ = hgkid_type;
                hplt = hgkid;
                break;
              endif
            endfor
          endif

          switch (typ)

            case "line"
              color = get (hplt, "color");
              style = get (hplt, "linestyle");
              lwidth = min (get (hplt, "linewidth"), 5);
              if (! strcmp (style, "none"))
                l1 = __go_line__ (hlegend, ...
                       "xdata", ([xoffset, xoffset + linelength] + xk * xstep) / lpos(3), ...
                       "ydata", [1, 1] .* (lpos(4) - yoffset - yk * ystep) / lpos(4), ...
                       "color", color, "linestyle", style, ...
                       "linewidth", lwidth, "marker", "none");
                setappdata (l1, "handle", hplt);
                hobjects(end+1) = l1;
              endif
              marker = get (hplt, "marker");
              if (! strcmp (marker, "none"))
                l1 = __go_line__ (hlegend, ...
                       "xdata", (xoffset + 0.5 * linelength  + xk * xstep) / lpos(3), ...
                       "ydata", (lpos(4) - yoffset - yk * ystep) / lpos(4), ...
                       "color", color, "linestyle", "none", ...
                       "linewidth", lwidth, "marker", marker, ...
                       "markeredgecolor", get (hplt, "markeredgecolor"), ...
                       "markerfacecolor", get (hplt, "markerfacecolor"), ...
                       "markersize", min (get (hplt, "markersize"),10));
                setappdata (l1, "handle", hplt);
                hobjects(end+1) = l1;
              endif

              ## Newly labeled objects have listeners added
              if (! any (hplt == old_hplots))
                addlistener (hplt, "color",
                             {@cb_line_listener, hlegend, linelength, false});
                addlistener (hplt, "linestyle",
                             {@cb_line_listener, hlegend, linelength, false});
                addlistener (hplt, "linewidth",
                             {@cb_line_listener, hlegend, linelength, false});
                addlistener (hplt, "marker",
                             {@cb_line_listener, hlegend, linelength, false});
                addlistener (hplt, "markeredgecolor",
                             {@cb_line_listener, hlegend, linelength, false});
                addlistener (hplt, "markerfacecolor",
                             {@cb_line_listener, hlegend, linelength, false});
                addlistener (hplt, "markersize",
                             {@cb_line_listener, hlegend, linelength, false});
                addlistener (hplt, "displayname",
                             {@cb_line_listener, hlegend, linelength, true});
              endif

            case "patch"
              facecolor = get (hplt, "facecolor");
              edgecolor = get (hplt, "edgecolor");
              cdata = get (hplt, "cdata");
              if (! strcmp (facecolor, "none") || ! strcmp (edgecolor, "none"))
                p1 = patch ("xdata", ([0, linelength, linelength, 0] +
                                      xoffset + xk * xstep) / lpos(3),
                            "ydata", (lpos(4) - yoffset -
                                      [yk-0.3, yk-0.3, yk+0.3, yk+0.3] .* ystep) / lpos(4),
                            "facecolor", facecolor, "edgecolor", edgecolor,
                            "cdata", cdata);
                setappdata (p1, "handle", hplt);
              else
                ## non-standard patch only making use of marker styles
                ## such as scatter plot.
                p1 = patch ("xdata", (xoffset + 0.5 * linelength  + xk * xstep) / lpos(3),
                            "ydata", (lpos(4) - yoffset - yk * ystep) / lpos(4),
                            "marker", get (hplt, "marker"),
                            "markeredgecolor",get (hplt,"markeredgecolor"),
                            "markerfacecolor",get (hplt,"markerfacecolor"),
                            "markersize", min (get (hplt,"markersize"),10),
                            "cdata", cdata);
                setappdata (p1, "handle", hplt);
              endif
              hobjects(end+1) = p1;
              ## Copy clim from axes so that colors work out.
              set (hlegend, "clim", get (ca(1), "clim"));

              ## FIXME: Need listeners, as for line objects.
              ##        Changing clim, for example, won't update colors

            case "surface"
              facecolor = get (hplt, "facecolor");
              edgecolor = get (hplt, "edgecolor");
              cdata = sum (get (ca(1), "clim")) / 2;
              if (! strcmp (facecolor, "none") || ! strcmp (edgecolor, "none"))
                p1 = patch ("xdata", ([0, linelength, linelength, 0] +
                                      xoffset + xk * xstep) / lpos(3),
                            "ydata", (lpos(4) - yoffset -
                                      [yk-0.3, yk-0.3, yk+0.3, yk+0.3] .* ystep) / lpos(4),
                            "facecolor", facecolor, "edgecolor", edgecolor,
                            "cdata", cdata);
                setappdata (p1, "handle", hplt);
                hobjects(end+1) = p1;
              endif
              ## FIXME: Need listeners, as for line objects.

          endswitch

          set (texthandle(k), "position",
                              [(txoffset + xk * xstep) / lpos(3), ...
                               (lpos(4) - yoffset - yk * ystep) / lpos(4)]);
          if (strcmp (orientation, "vertical"))
            yk += 1;
            if (yk > num1)
              yk = 0;
              xk += 1;
            endif
          else
            xk += 1;
            if (xk > num1)
              xk = 0;
              yk += 1;
            endif
          endif
        endfor

        ## Add an invisible text object to original axis
        ## that, when it is destroyed, will remove the legend.
        htdel = findall (ca(1), "-depth", 1, "tag", "deletelegend",
                                "type", "text");
        if (isempty (htdel))
          htdel = text (0, 0, "", "parent", ca(1), "tag", "deletelegend",
                        "visible", "off", "handlevisibility", "off",
                        "xliminclude", "off", "yliminclude", "off",
                        "zliminclude", "off");
          set (htdel, "deletefcn", {@cb_axes_deleted, ca, hlegend});
        endif
        if (isprop (hlegend, "unmodified_axes_position"))
          set (hlegend, "unmodified_axes_position",
                         unmodified_axes_position,
                        "unmodified_axes_outerposition",
                         unmodified_axes_outerposition);
        else
          addproperty ("unmodified_axes_position", hlegend,
                       "data", unmodified_axes_position);
          addproperty ("unmodified_axes_outerposition", hlegend,
                       "data", unmodified_axes_outerposition);
        endif

        ## Resize the axis that the legend is attached to if the legend is
        ## "outside" the plot and create a listener to resize axis to original
        ## size if the legend is deleted, hidden, or shown.
        if (outside)
          for i = 1 : numel (ca)
            units = get (ca(i), "units");
            unwind_protect
              set (ca(i), "units", "points");
              if (gnuplot && numel (ca) == 1)
                ## Let gnuplot handle the positioning of the keybox.
                ## This violates strict Matlab compatibility, but reliably
                ## renders an aesthetic result.
                set (ca(i), "position",  unmodified_axes_position,
                            "positionconstraint", "outerposition");
              else
                ## numel (ca) > 1 for axes overlays (like plotyy)
                set (ca(i), "position", new_pos);
              endif
            unwind_protect_cleanup
              set (ca(i), "units", units);
            end_unwind_protect
          endfor

          set (hlegend, "deletefcn", {@cb_restore_axes, ca, ...
                                      unmodified_axes_position, ...
                                      unmodified_axes_outerposition, ...
                                      htdel, hplots});
          addlistener (hlegend, "visible", {@cb_legend_hideshow, ca, ...
                                            unmodified_axes_position, ...
                                            new_pos});
        else
          set (hlegend, "deletefcn", {@cb_restore_axes, ca, [], [], ...
                                      htdel, hplots});
        endif

        if (! addprops)
          ## Remove listeners on existing legend temporarily to stop recursion.
          dellistener (hlegend, "location");
          dellistener (hlegend, "orientation");
          dellistener (hlegend, "string");
          dellistener (hlegend, "textposition");
        endif

        if (! addprops)
          set (hlegend, "string", text_strings);
        endif

        if (outside)
          set (hlegend, "location", [location "outside"],
                        "orientation", orientation, "textposition", textpos);
        else
          set (hlegend, "location", location, "orientation", orientation,
                        "textposition", textpos);
        endif

        if (addprops)
          addlistener (cax, "colormap", {@cb_legend_colormap_update, hlegend});
          addlistener (hlegend, "edgecolor", @cb_legend_text_update);
          addlistener (hlegend, "fontangle", @cb_legend_text_update);
          addlistener (hlegend, "fontname", @cb_legend_text_update);
          addlistener (hlegend, "fontweight", @cb_legend_text_update);
          addlistener (hlegend, "textcolor", @cb_legend_text_update);
          ## Properties which could change size of box, such as fontsize,
          ## require legend to be redrawn.
          ## FIXME: fontsize is changed by print.m function during the
          ##        production of a plot for output.  This screws things up
          ##        because legend tries to return the axes size to what it
          ##        was when the figure was created, versus what it is now
          ##        when the figure is being printed.  Temporary hack is
          ##        good enough for generating the Octave manual which still
          ##        relies on gnuplot for generating images.  See bug #40333.
          if (! gnuplot)
            addlistener (hlegend, "fontsize", @cb_legend_update);
          endif
          addlistener (hlegend, "fontunits", @cb_legend_update);
          addlistener (hlegend, "interpreter", @cb_legend_update);
          addlistener (hlegend, "location", @cb_legend_location);
          addlistener (hlegend, "orientation", @cb_legend_update);
          addlistener (hlegend, "string", @cb_legend_update);
          addlistener (hlegend, "textposition", @cb_legend_update);
          ## FIXME: need to add listeners for tightinset and position
          ##        addlistener (ca, "tightinset", @update????);
          ##        addlistener (ca, "position", @update????);
        else
          ## Restore listeners temporarily disabled during reconstruction.
          addlistener (hlegend, "location", @cb_legend_update);
          addlistener (hlegend, "orientation", @cb_legend_update);
          addlistener (hlegend, "string", @cb_legend_update);
          addlistener (hlegend, "textposition", @cb_legend_update);
        endif

      unwind_protect_cleanup
        set (hfig, "currentaxes", origaxes);
        if (! isempty (origfig))
          set (0, "currentfigure", origfig);
        endif
      end_unwind_protect
    endif
  endif

  ## Restore operation of callbacks
  setappdata (hlegend, "nocallbacks", false);

  if (nargout > 0)
    hleg = hlegend;
    hleg_obj = hobjects;
    hplot = hplots;
    labels = text_strings;
  endif

endfunction

## Colormap of the base axes has changed.
function cb_legend_colormap_update (cax, ~, hlegend)
  set (hlegend, "colormap", get (cax, "colormap"));
endfunction

## A non-text property of legend has changed requiring an update.
function cb_legend_update (hleg, ~)
  persistent recursive = false;

  if (! recursive)
    recursive = true;
    unwind_protect
      hax = getappdata (hleg, "__axes_handle__");
      ## Hack.  Maybe store this somewhere else such as appdata.
      hplots = get (hleg, "deletefcn"){6};
      text_strings = get (hleg, "string");
      position = get (hleg, "unmodified_axes_position");
      outerposition = get (hleg, "unmodified_axes_outerposition");
      units = get (hax, "units");
      set (hax, "units", "points");
      switch (get (hax, "positionconstraint"))
        case "innerposition"
          set (hax, "outerposition", outerposition, "position", position);
        case "outerposition"
          set (hax, "position", position, "outerposition", outerposition);
      endswitch
      if (isscalar (hax))
        set (hax, "units", units);
      else
        set (hax, {"units"}, units);
      endif

      hleg = legend (hax(1), hplots, text_strings);
    unwind_protect_cleanup
      recursive = false;
    end_unwind_protect
  endif

endfunction

## A text property of legend, such as fontname, has changed.
function cb_legend_text_update (hleg, ~)

  kids = get (hleg, "children");
  htext = kids(strcmp (get (kids, "type"), "text"));

  tprops = {"fontangle", "fontname", "fontweight", "color"};
  lprops = {"fontangle", "fontname", "fontweight", "textcolor"};
  set (htext, tprops, get (hleg, lprops));

  ec = get (hleg, "edgecolor");
  set (hleg, "xcolor", ec, "ycolor", ec);

endfunction

## The legend "visible" property has changed.
function cb_legend_hideshow (hleg, ~, ca, orig_pos, new_pos)

  isvisible = strcmp (get (hleg, "visible"), "on");

  ## FIXME: Can't use a single set() call because of linked axes and
  ##        listeners on plotyy graphs.
  ca = ca(isaxes (ca));
  for cax = ca(:).'
    units = get (cax, "units");
    unwind_protect
      set (cax, "units", "points");
      if (isvisible)
        set (cax, "position", new_pos);
      else
        set (cax, "position", orig_pos);
      endif
    unwind_protect_cleanup
      set (cax, "units", units);
    end_unwind_protect
  endfor

endfunction

## The legend "location" property has changed.
function cb_legend_location (hleg, ~)

  ## If it isn't "none", which means manual positioning, then rebuild.
  if (! strcmp (get (hleg, "location"), "none"))
    cb_legend_update (hleg, []);
  endif

endfunction

## Axes to which legend was attached is being deleted/reset.  Delete legend.
function cb_axes_deleted (~, ~, ca, hlegend)

  if (isaxes (hlegend))
    if (strcmp (get (ca(1), "beingdeleted"), "on"))
      ## Axes are being deleted.  Disable call to cb_restore_axes.
      set (hlegend, "deletefcn", []);
    endif
    delete (hlegend);
  endif

endfunction

## Restore position of axes object when legend is deleted.
function cb_restore_axes (~, ~, ca, pos, outpos, htdel, hplots)

  hf = ancestor (ca(1), "figure");
  if (strcmp (get (hf, "beingdeleted"), "on")
      || strcmp (get (ca(1), "beingdeleted"), "on"))
    ## Skip restoring axes if entire figure or axes is being destroyed.
    return;
  endif

  ## Remove text object used to trigger legend delete when axes is deleted
  if (ishghandle (htdel))
    set (htdel, "deletefcn", []);
    delete (htdel);
  endif

  ## Restore original axes positions
  if (! isempty (pos))
    ## FIXME: can't use single call to set() because of weirdness w/plotyy
    for cax = ca(:).'
      if (isaxes (cax))
        units = get (cax, "units");
        unwind_protect
          set (cax, "units", "points", "position", pos);
        unwind_protect_cleanup
          set (cax, "units", units);
        end_unwind_protect
      endif
    endfor
  endif

  ## Remove listeners from plot objects
  for i = 1 : numel (hplots)
    if (isgraphics (hplots(i), "line"))
      dellistener (hplots(i), "color");
      dellistener (hplots(i), "linestyle");
      dellistener (hplots(i), "linewidth");
      dellistener (hplots(i), "marker");
      dellistener (hplots(i), "markeredgecolor");
      dellistener (hplots(i), "markerfacecolor");
      dellistener (hplots(i), "markersize");
      dellistener (hplots(i), "displayname");
    endif
  endfor

  ## Nullify legend link (can't delete properties yet)
  set (ca(1), "__legend_handle__", []);

endfunction

## Update legend item because underlying plot line object has changed.
function cb_line_listener (h, ~, hlegend, linelength, update_name)

  ## Don't execute callbacks when legend is under construction
  legdata = getappdata (hlegend);
  if (legdata.nocallbacks)
    return;
  endif

  if (update_name)
    ## When string changes, have to rebuild legend completely
    [hplots, text_strings] = __getlegenddata__ (hlegend);
    if (isempty (hplots))
      delete (hlegend);
    else
      legend (legdata.handle(1), hplots, text_strings);
    endif
  else
    kids = get (hlegend, "children");
    kids = kids([getappdata(kids, "handle"){:}] == h);
    kids = kids(strcmp (get (kids, "type"), "line"));
    idx = strcmp (get (kids, "marker"), "none");
    ll = kids (idx);
    lm = kids (! idx);

    [linestyle, marker, displayname] = ...
      get (h, {"linestyle", "marker", "displayname"}){:};

    if (! isempty (ll))
      [xpos1, ypos1] = get (ll, {"xdata", "ydata"}){:};
      xpos2 = sum (xpos1) / 2;
      ypos2 = ypos1(1);
      delete (ll);
      if (! isempty (lm))
        delete (lm);
      endif
    else
      [xpos2, ypos2] = get (lm, {"xdata", "ydata"}){:};
      xpos1 = xpos2 + [-0.5, 0.5] * linelength;
      ypos1 = [ypos2, ypos2];
      delete (lm);
    endif

    if (! strcmp (linestyle, "none"))
      hl = __go_line__ (hlegend, "xdata", xpos1, "ydata", ypos1,
                        "color", get (h, "color"),
                        "linestyle", get (h, "linestyle"),
                        "linewidth", min (get (h, "linewidth"), 5),
                        "marker", "none");
      setappdata (hl, "handle", h);
    endif
    if (! strcmp (marker, "none"))
      hl = __go_line__ (hlegend, "xdata", xpos2, "ydata", ypos2, ...
                        "color", get (h, "color"), ...
                        "marker", marker, ...
                        "markeredgecolor", get (h, "markeredgecolor"), ...
                        "markerfacecolor", get (h, "markerfacecolor"), ...
                        "markersize", min (get (h, "markersize"), 10), ...
                        "linestyle", "none", ...
                        "linewidth", min (get (h, "linewidth"), 5));
      setappdata (hl, "handle", h);
    endif
  endif

endfunction


%!demo
%! clf;
%! plot (rand (2));
%! title ("legend called with string inputs for labels");
%! h = legend ("foo", "bar");
%! legend (h, "location", "northeastoutside");
%! set (h, "fontsize", 20);

%!demo
%! clf;
%! plot (rand (2));
%! title ("legend called with cell array of strings");
%! h = legend ({"cellfoo", "cellbar"});
%! legend (h, "location", "northeast");
%! set (h, "fontsize", 20);

%!demo
%! clf;
%! plot (rand (3));
%! title ("legend () without inputs creates default labels");
%! h = legend ();

%!demo
%! clf;
%! x = 0:1;
%! plot (x,x,";I am Blue;", x,2*x, x,3*x,";I am yellow;");
%! h = legend ("location", "northeastoutside");
%! ## Placing legend inside returns axes to original size
%! legend (h, "location", "northeast");
%! title ("Blue and Yellow keys, with Orange missing");

%!demo
%! clf;
%! plot (1:10, 1:10, 1:10, fliplr (1:10));
%! title ("incline is blue and decline is orange");
%! legend ({"I am blue", "I am orange"}, "location", "east");
%! legend hide
%! legend show

%!demo
%! clf;
%! plot (1:10, 1:10, 1:10, fliplr (1:10));
%! title ("Legend with keys in horizontal orientation");
%! legend ({"I am blue", "I am orange"}, ...
%!         "location", "east", "orientation", "horizontal");
%! legend boxoff
%! legend boxon

%!demo
%! clf;
%! plot (1:10, 1:10, 1:10, fliplr (1:10));
%! title ("Legend with box off");
%! legend ({"I am blue", "I am orange"}, "location", "east");
%! legend boxoff

%!demo
%! clf;
%! plot (1:10, 1:10, 1:10, fliplr (1:10));
%! title ("Legend with text to the left of key");
%! legend ({"I am blue", "I am orange"}, "location", "east");
%! legend left

%!demo
%! clf;
%! plot (1:10, 1:10, 1:10, fliplr (1:10));
%! title ({"Use properties to place legend text to the left of key", ...
%!         "Legend text color is magenta"});
%! h = legend ({"I am blue", "I am orange"}, "location", "east");
%! legend ("right");
%! set (h, "textposition", "left");
%! set (h, "textcolor", [1 0 1]);

%!demo
%! clf;
%! plot (1:10, 1:10, 1:10, fliplr (1:10));
%! title ("Legend is hidden");
%! legend ({"I am blue", "I am orange"}, "location", "east");
%! legend hide

%!demo
%! clf;
%! x = 0:1;
%! plot (x,x,";I am Blue;", x,2*x,";I am Orange;", x,3*x,";I am Yellow;");
%! title ({"Labels are embedded in call to plot", ...
%!         "Legend is hidden and then shown"});
%! legend boxon
%! legend hide
%! legend show

%!demo
%! clf;
%! x = 0:1;
%! plot (x,x,  x,2*x, x,3*x);
%! title ("Labels with interpreted Greek text");
%! h = legend ('\alpha', '\beta=2\alpha', '\gamma=3\alpha');
%! set (h, "interpreter", "tex");

%!demo
%! clf;
%! plot (rand (2));
%! title ("Labels with TeX interpreter turned off");
%! h = legend ("Hello_World", "foo^bar");
%! set (h, "interpreter", "none");

%!demo
%! clf;
%! labels = {};
%! colororder = get (gca, "colororder");
%! for i = 1:5
%!   h = plot (1:100, i + rand (100,1)); hold on;
%!   set (h, "color", colororder(i,:));
%!   labels = {labels{:}, ["Signal ", num2str(i)]};
%! endfor
%! hold off;
%! title ({"Signals with random offset and uniform noise";
%!         "Legend shown below and outside of plot"});
%! xlabel ("Sample Nr [k]"); ylabel ("Amplitude [V]");
%! legend (labels, "location", "southoutside");

%!demo
%! clf;
%! x = linspace (0, 10);
%! plot (x, x);
%! hold on;
%! stem (x, x.^2, "g");
%! title ("First created object gets first label");
%! legend ("linear");
%! hold off;

%!demo
%! clf;
%! x = linspace (0, 10);
%! plot (x, x, x, x.^2);
%! title ("First created object gets first label");
%! legend ("linear");

%!demo
%! clf;
%! x = linspace (0, 10);
%! plot (x, x, x, x.^2);
%! title ("Labels are applied in order of object creation");
%! legend ("linear", "quadratic");

%!demo
%! clf;
%! subplot (2,1,1);
%! rand_2x3_data1 = [0.341447, 0.171220, 0.284370; 0.039773, 0.731725, 0.779382];
%! bar (rand_2x3_data1);
%! ylim ([0 1.0]);
%! title ("legend() works for bar graphs (hggroups)");
%! legend ({"1st Bar", "2nd Bar", "3rd Bar"}, "location", "northwest");
%! subplot (2,1,2);
%! x = linspace (0, 10, 20);
%! stem (x, 0.5+x.*rand (size (x))/max (x), "markeredgecolor", [0 0.7 0]);
%! hold on;
%! stem (x+10/(2*20), x.*(1.0+rand (size (x)))/max (x));
%! xlim ([0 10+10/(2*20)]);
%! title ("legend() works for stem plots (hggroups)");
%! legend ({"Multicolor", "Unicolor"}, "location", "northwest");

%!demo
%! clf;
%! colormap (cool (64));
%! surf (peaks ());
%! legend ("peaks()");
%! title ("legend() works for surface objects too");

%!demo
%! clf reset;  # needed to undo colormap assignment in previous demo
%! rand_2x3_data2 = [0.44804, 0.84368, 0.23012; 0.72311, 0.58335, 0.90531];
%! bar (rand_2x3_data2);
%! ylim ([0 1.2]);
%! title ('"left" option places colors to the left of text label');
%! legend ("1st Bar", "2nd Bar", "3rd Bar");
%! legend left;

%!demo
%! clf;
%! x = 0:0.1:7;
%! h = plot (x,sin (x), x,cos (x), x,sin (x.^2/10), x,cos (x.^2/10));
%! title ("Only the sin() objects have keylabels");
%! legend (h([1, 3]), {"sin (x)", "sin (x^2/10)"}, "location", "southwest");

%!demo
%! clf;
%! x = 0:0.1:10;
%! plot (x, sin (x), ";sin (x);");
%! hold on;
%! plot (x, cos (x), ";cos (x);");
%! hold off;
%! title ("legend constructed from multiple plot calls");

%!demo
%! clf;
%! x = 0:0.1:10;
%! plot (x, sin (x), ";sin (x);");
%! hold on;
%! plot (x, cos (x), ";cos (x);");
%! hold off;
%! title ("Specified label text overrides previous labels");
%! legend ({"Sine", "Cosine"}, "location", "northeastoutside");

%!demo
%! clf;
%! x = 0:10;
%! plot (x, rand (11));
%! axis ([0, 10, 0, 1]);
%! xlabel ("Indices");
%! ylabel ("Random Values");
%! title ('Legend "off" deletes the legend');
%! legend (cellstr (num2str ((0:10)')), "location", "northeastoutside");
%! pause (1);
%! legend off;

%!demo
%! clf;
%! x = (1:5)';
%! subplot (2,2,1);
%!  plot (x, rand (numel (x)));
%!  legend (cellstr (num2str (x)), "location", "northwestoutside");
%! subplot (2,2,2);
%!  plot (x, rand (numel (x)));
%!  legend (cellstr (num2str (x)), "location", "northeastoutside");
%! subplot (2,2,3);
%!  plot (x, rand (numel (x)));
%!  legend (cellstr (num2str (x)), "location", "southwestoutside");
%! subplot (2,2,4);
%!  plot (x, rand (numel (x)));
%!  legend (cellstr (num2str (x)), "location", "southeastoutside");
%! ## Legend works on a per axes basis for each subplot

%!demo
%! clf;
%! plot (rand (2));
%! title ("legend() will warn if extra labels are specified");
%! legend ("Hello", "World", "interpreter", "foobar");

%!demo
%! clf;
%! x = 0:10;
%! y1 = rand (size (x));
%! y2 = rand (size (x));
%! [ax, h1, h2] = plotyy (x, y1, x, y2);
%! title ({"plotyy legend test #1", "Blue label to left axis, Orange label to right axis"});
%! drawnow ();
%! legend ("Blue", "Orange", "location", "south");

%!demo
%! clf;
%! x = 0:10;
%! y1 = rand (size (x));
%! y2 = rand (size (x));
%! [ax, h1, h2] = plotyy (x, y1, x, y2);
%! ylabel (ax(1), {"Blue", "Y", "Axis"});
%! title ('plotyy legend test #2: "westoutside" adjusts to ylabel');
%! drawnow ();
%! legend ([h1, h2], {"Blue", "Orange"}, "location", "westoutside");

%!demo
%! clf;
%! x = 0:10;
%! y1 = rand (size (x));
%! y2 = rand (size (x));
%! [ax, h1, h2] = plotyy (x, y1, x, y2);
%! ylabel (ax(2), {"Orange", "Y", "Axis"});
%! title ('plotyy legend test #3: "eastoutside" adjusts to ylabel');
%! drawnow ();
%! legend ([h1, h2], {"Blue", "Orange"}, "location", "eastoutside");

%!demo
%! clf;
%! plot (1:10, 1:10);
%! title ("a very long label can sometimes cause problems");
%! legend ("hello very big world", "location", "northeastoutside");

%!demo  # bug 36408
%! clf;
%! option = "right";
%! subplot (3,1,1);
%!  plot (rand (1,4));
%!  xlabel xlabel;
%!  ylabel ylabel;
%!  title ("Subplots adjust to the legend placed outside");
%!  legend ({"1"}, "location", "northeastoutside");
%!  legend (option);
%! subplot (3,1,2);
%!  plot (rand (1,4));
%!  xlabel xlabel;
%!  ylabel ylabel;
%!  legend ({"1234567890"}, "location", "eastoutside");
%!  legend (option);
%! subplot (3,1,3);
%!  plot (rand (1,4));
%!  xlabel xlabel;
%!  ylabel ylabel;
%!  legend ({"12345678901234567890"}, "location", "southeastoutside");
%!  legend (option);

%!demo  # bug 36408
%! clf;
%! option = "right";
%! subplot (3,1,1);
%!  plot (rand (1,4));
%!  title ("Subplots adjust to the legend placed outside");
%!  legend ({"1"}, "location", "northwestoutside");
%!  legend (option);
%! subplot (3,1,2);
%!  plot (rand (1,4));
%!  legend ({"1234567890"}, "location", "westoutside");
%!  legend (option);
%! subplot (3,1,3);
%!  plot (rand (1,4));
%!  legend ({"12345678901234567890"}, "location", "southwestoutside");
%!  legend (option);

%!demo  # bug 36408
%! clf;
%! option = "right";
%! subplot (3,1,1);
%!  plot (rand (1,4));
%!  set (gca (), "yaxislocation", "right");
%!  xlabel ("xlabel");
%!  ylabel ("ylabel");
%!  title ("Subplots adjust to the legend placed outside");
%!  legend ({"1"}, "location", "northeastoutside");
%!  legend (option);
%! subplot (3,1,2);
%!  plot (rand (1,4));
%!  set (gca (), "yaxislocation", "right");
%!  xlabel ("xlabel");
%!  ylabel ("ylabel");
%!  legend ({"1234567890"}, "location", "eastoutside");
%!  legend (option);
%! subplot (3,1,3);
%!  plot (rand (1,4));
%!  set (gca (), "yaxislocation", "right");
%!  xlabel ("xlabel");
%!  ylabel ("ylabel");
%!  legend ({"12345678901234567890"}, "location", "southeastoutside");
%!  legend (option);

%!demo  # bug 36408
%! clf;
%! option = "right";
%! subplot (3,1,1);
%!  plot (rand (1,4));
%!  set (gca (), "yaxislocation", "right");
%!  xlabel ("xlabel");
%!  ylabel ("ylabel");
%!  title ("Subplots adjust to the legend placed outside");
%!  legend ({"1"}, "location", "northwestoutside");
%!  legend (option);
%! subplot (3,1,2);
%!  plot (rand (1,4));
%!  set (gca (), "yaxislocation", "right");
%!  xlabel ("xlabel");
%!  ylabel ("ylabel");
%!  legend ({"1234567890"}, "location", "westoutside");
%!  legend (option);
%! subplot (3,1,3);
%!  plot (rand (1,4));
%!  set (gca (), "yaxislocation", "right");
%!  xlabel ("xlabel");
%!  ylabel ("ylabel");
%!  legend ({"12345678901234567890"}, "location", "southwestoutside");
%!  legend (option);

%!demo  # bug 36408;
%! clf;
%! option = "right";
%! subplot (3,1,1);
%!  plot (rand (1,4));
%!  set (gca (), "xaxislocation", "top");
%!  xlabel ("xlabel");
%!  ylabel ("ylabel");
%!  title ("Subplots adjust to the legend placed outside");
%!  legend ({"1"}, "location", "northwestoutside");
%!  legend (option);
%! subplot (3,1,2);
%!  plot (rand (1,4));
%!  set (gca (), "xaxislocation", "top");
%!  xlabel ("xlabel");
%!  ylabel ("ylabel");
%!  legend ({"1234567890"}, "location", "westoutside");
%!  legend (option);
%! subplot (3,1,3);
%!  plot (rand (1,4));
%!  set (gca (), "xaxislocation", "top");
%!  xlabel ("xlabel");
%!  ylabel ("ylabel");
%!  legend ({"12345678901234567890"}, "location", "southwestoutside");
%!  legend (option);

%!demo  # bug 39697
%! clf;
%! plot (1:10);
%! legend ("Legend Text");
%! title ({"Multi-line", "titles", "are a", "problem", "See bug #39697"});

%!testif ; any (strcmp ("gnuplot", available_graphics_toolkits ()))
%! toolkit = graphics_toolkit ("gnuplot");
%! h = figure ("visible", "off");
%! unwind_protect
%!   position = get (h, "position");
%!   plot (rand (3));
%!   legend ();
%!   filename = sprintf ("%s.eps", tempname ());
%!   print (filename);
%!   unlink (filename);
%!   assert (get (h, "position"), position);
%! unwind_protect_cleanup
%!   close (h);
%!   graphics_toolkit (toolkit);
%! end_unwind_protect

%!test <*42035>
%! h = figure ("visible", "off");
%! unwind_protect
%!   hax1 = subplot (1,2,1);
%!   plot (1:10);
%!   hax2 = subplot (1,2,2);
%!   plot (1:10);
%!   hleg1 = legend (hax1, "foo");
%!   assert (getappdata (hleg1, "__axes_handle__"), hax1);
%!   assert (gca (), hax2);
%!   hleg2 = legend ("bar");
%!   assert (getappdata (hleg2, "__axes_handle__"), gca ());
%! unwind_protect_cleanup
%!   close (h);
%! end_unwind_protect

%!test
%! ## Difficult example from plotyy demo #1
%! hf = figure ("visible", "off");
%! unwind_protect
%!   x = 0:0.1:2*pi;
%!   y1 = sin (x);
%!   y2 = exp (x - 1);
%!   hax = plotyy (x,y1, x-1,y2, @plot, @semilogy);
%!   text (0.5, 0.5, "Left Axis", "parent", hax(1));
%!   text (4.5, 80, "Right Axis", "parent", hax(2));
%!   hleg = legend ("show");
%!   assert (get (hleg, "string"), {"data1", "data2"});
%!   fail ("legend ('foo', 'bar', 'baz')", "warning", "ignoring extra labels");
%! unwind_protect_cleanup
%!   close (hf);
%! end_unwind_protect

%!test
%! ## Test warnings about objects to label
%! hf = figure ("visible", "off");
%! unwind_protect
%!   hax = gca ();
%!   fail ("legend ('foobar')", "warning", "plot data is empty");
%!   ht = text (0.5, 0.5, "Hello World");
%!   fail ("legend ('foobar')", "warning", "plot data is empty");
%!   lastwarn ("");   # clear warning
%!   hleg = legend ();
%!   assert (isempty (hleg) && isempty (lastwarn ()));
%!   fail ("legend ('foobar')", "warning", "plot data is empty");
%!   hln = line ([0 1], [0 1]);
%!   fail ("legend ('foo', 'bar')", "warning", "ignoring extra labels");
%!   plot (rand (2, 21));
%!   fail ("legend ()", "warning", "labeling only first 20 data objects");
%! unwind_protect_cleanup
%!   close (hf);
%! end_unwind_protect

%!test
%! ## Test warnings about unsupported features
%! hf = figure ("visible", "off");
%! unwind_protect
%!   plot (1:10);
%!   fail ("legend ('location','best')", "warning", "'best' not yet implemented");
%! unwind_protect_cleanup
%!   close (hf);
%! end_unwind_protect