Mercurial > hg > octave-nkf
annotate scripts/plot/legend.m @ 8529:774b44619c5c
Fix legend order for both horizontal and vertical string cell.
author | Daniel J Sebald <daniel.sebald@ieee.org> |
---|---|
date | Fri, 16 Jan 2009 08:27:46 +0100 |
parents | 9f34f7636fe0 |
children | 5dd06f19e9be |
rev | line source |
---|---|
7017 | 1 ## Copyright (C) 2001, 2006, 2007 Laurent Mazet |
6146 | 2 ## Copyright (C) 2006 John W. Eaton |
3 ## | |
6440 | 4 ## This file is part of Octave. |
5 ## | |
6 ## Octave is free software; you can redistribute it and/or modify it | |
7 ## under the terms of the GNU General Public License as published by | |
7016 | 8 ## the Free Software Foundation; either version 3 of the License, or (at |
9 ## your option) any later version. | |
6440 | 10 ## |
11 ## Octave is distributed in the hope that it will be useful, but | |
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 ## General Public License for more details. | |
6146 | 15 ## |
16 ## You should have received a copy of the GNU General Public License | |
7016 | 17 ## along with Octave; see the file COPYING. If not, see |
18 ## <http://www.gnu.org/licenses/>. | |
6146 | 19 |
20 ## -*- texinfo -*- | |
6450 | 21 ## @deftypefn {Function File} {} legend (@var{st1}, @var{st2}, @dots{}) |
6977 | 22 ## @deftypefnx {Function File} {} legend (@var{st1}, @var{st2}, @dots{}, "location", @var{pos}) |
6146 | 23 ## @deftypefnx {Function File} {} legend (@var{matstr}) |
6977 | 24 ## @deftypefnx {Function File} {} legend (@var{matstr}, "location", @var{pos}) |
6146 | 25 ## @deftypefnx {Function File} {} legend (@var{cell}) |
6977 | 26 ## @deftypefnx {Function File} {} legend (@var{cell}, "location", @var{pos}) |
6146 | 27 ## @deftypefnx {Function File} {} legend ('@var{func}') |
28 ## | |
6895 | 29 ## Display a legend for the current axes using the specified strings |
30 ## as labels. Legend entries may be specified as individual character | |
31 ## string arguments, a character array, or a cell array of character | |
32 ## strings. Legend works on line graphs, bar graphs, etc. A plot must | |
33 ## exist before legend is called. | |
6146 | 34 ## |
6895 | 35 ## The optional parameter @var{pos} specifies the location of the legend |
36 ## as follows: | |
6146 | 37 ## |
6977 | 38 ## @multitable @columnfractions 0.06 0.14 0.80 |
39 ## @item @tab north @tab | |
40 ## center top | |
41 ## @item @tab south @tab | |
42 ## center bottom | |
43 ## @item @tab east @tab | |
44 ## right center | |
45 ## @item @tab west @tab | |
46 ## left center | |
47 ## @item @tab northeast @tab | |
48 ## right top (default) | |
49 ## @item @tab northwest @tab | |
50 ## left top | |
51 ## @item @tab southeast @tab | |
52 ## right bottom | |
53 ## @item @tab southwest @tab | |
54 ## left bottom | |
55 ## @item | |
56 ## @item @tab outside @tab | |
57 ## can be appended to any location string | |
6146 | 58 ## @end multitable |
59 ## | |
7001 | 60 ## Some specific functions are directly available using @var{func}: |
6146 | 61 ## |
7148 | 62 ## @table @asis |
6895 | 63 ## @item "show" |
6146 | 64 ## Show legends from the plot |
6895 | 65 ## @item "hide" |
7148 | 66 ## @itemx "off" |
6146 | 67 ## Hide legends from the plot |
6895 | 68 ## @item "boxon" |
6146 | 69 ## Draw a box around legends |
6895 | 70 ## @item "boxoff" |
6146 | 71 ## Withdraw the box around legends |
6895 | 72 ## @item "left" |
6146 | 73 ## Text is to the left of the keys |
6895 | 74 ## @item "right" |
6146 | 75 ## Text is to the right of the keys |
76 ## @end table | |
77 ## @end deftypefn | |
78 | |
79 ## PKG_ADD mark_as_command legend | |
80 | |
81 function legend (varargin) | |
82 | |
8208 | 83 [ca, varargin, nargin] = __plt_get_axis_arg__ ("legend", varargin{:}); |
6257 | 84 nargs = nargin; |
6146 | 85 |
86 if (nargs > 0) | |
87 pos = varargin{nargs}; | |
88 if (isnumeric (pos) && isscalar (pos) && round (pos) == pos) | |
6395 | 89 if (pos >= -1 && pos <= 4) |
6257 | 90 set (ca, "keypos", pos); |
6146 | 91 nargs--; |
92 else | |
93 error ("legend: invalid position specified"); | |
94 endif | |
95 endif | |
96 endif | |
6977 | 97 |
98 if (nargs > 1) | |
99 pos = varargin{nargs-1}; | |
7054 | 100 str = varargin{nargs}; |
6977 | 101 if (strcmpi (pos, "location") && ischar (str)) |
102 set (ca, "keypos", str); | |
103 nargs -= 2; | |
104 endif | |
105 endif | |
6146 | 106 |
6257 | 107 kids = get (ca, "children"); |
108 nkids = numel (kids); | |
109 k = 1; | |
110 turn_on_legend = false; | |
111 | |
6146 | 112 if (nargs == 1) |
113 arg = varargin{1}; | |
114 if (ischar (arg)) | |
115 if (rows (arg) == 1) | |
116 str = tolower (deblank (arg)); | |
117 switch (str) | |
118 case {"off", "hide"} | |
6257 | 119 set (ca, "key", "off"); |
6146 | 120 case "show" |
6257 | 121 set (ca, "key", "on"); |
6146 | 122 case "toggle" |
6257 | 123 val = get (ca, "key"); |
8190
73d6b71788c0
use case-insensitive comparison for graphics properties; misc style fixes
John W. Eaton <jwe@octave.org>
parents:
8102
diff
changeset
|
124 if (strcmpi (val, "on")) |
6257 | 125 set (ca, "key", "off"); |
126 else | |
127 set (ca, "key", "on"); | |
128 endif | |
6146 | 129 case "boxon" |
6257 | 130 set (ca, "key", "on", "keybox", "on"); |
6146 | 131 case "boxoff" |
6257 | 132 set (ca, "keybox", "off"); |
8291
53f35799b235
Add support for left/right argument to the legend function
David Bateman <dbateman@free.fr>
parents:
8257
diff
changeset
|
133 case "left" |
53f35799b235
Add support for left/right argument to the legend function
David Bateman <dbateman@free.fr>
parents:
8257
diff
changeset
|
134 set (ca, "keyreverse", "off") |
53f35799b235
Add support for left/right argument to the legend function
David Bateman <dbateman@free.fr>
parents:
8257
diff
changeset
|
135 case "right" |
53f35799b235
Add support for left/right argument to the legend function
David Bateman <dbateman@free.fr>
parents:
8257
diff
changeset
|
136 set (ca, "keyreverse", "on") |
6146 | 137 otherwise |
8208 | 138 typ = get (kids (k), "type"); |
139 while (k <= nkids && ! strcmp (typ, "line") && | |
140 ! strcmp (typ, "hggroup")) | |
6257 | 141 k++; |
8257 | 142 typ = get (kids (k), "type"); |
6257 | 143 endwhile |
144 if (k <= nkids) | |
145 turn_on_legend = true; | |
8208 | 146 if (strcmp (typ, "hggroup")) |
147 hgkids = get (kids(k), "children"); | |
148 for j = 1 : length (hgkids) | |
149 hgobj = get (hgkids (j)); | |
150 if (isfield (hgobj, "keylabel")) | |
151 set (hgkids(j), "keylabel", arg); | |
152 break; | |
153 endif | |
154 endfor | |
155 else | |
156 set (kids(k), "keylabel", arg); | |
157 endif | |
6257 | 158 else |
159 warning ("legend: ignoring extra labels"); | |
160 endif | |
6146 | 161 endswitch |
162 nargs--; | |
163 else | |
164 varargin = cellstr (arg); | |
6740 | 165 nargs = numel (varargin); |
6146 | 166 endif |
167 elseif (iscellstr (arg)) | |
8529
774b44619c5c
Fix legend order for both horizontal and vertical string cell.
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
8343
diff
changeset
|
168 varargin = arg; |
6146 | 169 nargs = numel (varargin); |
170 else | |
171 error ("legend: expecting argument to be a character string"); | |
172 endif | |
173 endif | |
174 | |
6272 | 175 if (nargs > 0) |
176 have_data = false; | |
177 for i = 1:nkids | |
7148 | 178 if (strcmp (get (kids(k), "type"), "line") |
179 || strcmp (get (kids(k), "type"), "surface") | |
8208 | 180 || strcmp (get (kids(k), "type"), "patch") |
181 || strcmp (get (kids(k), "type"), "hggroup")) | |
6272 | 182 have_data = true; |
183 break; | |
184 endif | |
185 endfor | |
186 if (! have_data) | |
187 warning ("legend: plot data is empty; setting key labels has no effect"); | |
188 endif | |
6147 | 189 endif |
190 | |
6575 | 191 warned = false; |
8529
774b44619c5c
Fix legend order for both horizontal and vertical string cell.
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
8343
diff
changeset
|
192 for i = nargs:-1:1 |
6146 | 193 arg = varargin{i}; |
194 if (ischar (arg)) | |
7148 | 195 while (k <= nkids |
196 && ! (strcmp (get (kids(k), "type"), "line") | |
197 || strcmp (get (kids(k), "type"), "surface") | |
8208 | 198 || strcmp (get (kids(k), "type"), "patch") |
199 || strcmp (get (kids(k), "type"), "hggroup"))) | |
6257 | 200 k++; |
201 endwhile | |
202 if (k <= nkids) | |
8208 | 203 if (strcmp (get (kids(k), "type"), "hggroup")) |
204 hgkids = get (kids(k), "children"); | |
205 for j = 1 : length (hgkids) | |
206 hgobj = get (hgkids (j)); | |
207 if (isfield (hgobj, "keylabel")) | |
208 set (hgkids(j), "keylabel", arg); | |
209 break; | |
210 endif | |
211 endfor | |
212 else | |
213 set (kids(k), "keylabel", arg); | |
214 endif | |
6257 | 215 turn_on_legend = true; |
6450 | 216 k++; |
6257 | 217 elseif (! warned) |
218 warned = true; | |
219 warning ("legend: ignoring extra labels"); | |
220 endif | |
6146 | 221 else |
222 error ("legend: expecting argument to be a character string"); | |
223 endif | |
224 endfor | |
225 | |
6257 | 226 if (turn_on_legend) |
227 set (ca, "key", "on"); | |
6146 | 228 endif |
229 | |
230 endfunction | |
231 | |
232 %!demo | |
233 %! close all; | |
8343
9f34f7636fe0
legend.m: Correct ording of legend labels.
Ben Abbott <bpabbott@mac.com>
parents:
8291
diff
changeset
|
234 %! plot(1:10, 1:10, 1:10, fliplr(1:10)); |
9f34f7636fe0
legend.m: Correct ording of legend labels.
Ben Abbott <bpabbott@mac.com>
parents:
8291
diff
changeset
|
235 %! title("incline is blue and decline is green"); |
9f34f7636fe0
legend.m: Correct ording of legend labels.
Ben Abbott <bpabbott@mac.com>
parents:
8291
diff
changeset
|
236 %! legend({"I'm blue", "I'm green"}, "location", "east") |
9f34f7636fe0
legend.m: Correct ording of legend labels.
Ben Abbott <bpabbott@mac.com>
parents:
8291
diff
changeset
|
237 |
9f34f7636fe0
legend.m: Correct ording of legend labels.
Ben Abbott <bpabbott@mac.com>
parents:
8291
diff
changeset
|
238 %!demo |
9f34f7636fe0
legend.m: Correct ording of legend labels.
Ben Abbott <bpabbott@mac.com>
parents:
8291
diff
changeset
|
239 %! close all; |
6146 | 240 %! plot(1:10, 1:10); |
241 %! title("a very long label can sometimes cause problems"); | |
6977 | 242 %! legend({"hello world"}, "location", "northeastoutside") |
6146 | 243 |
244 %!demo | |
245 %! close all; | |
246 %! labels = {}; | |
247 %! for i = 1:5 | |
248 %! plot(1:100, i + rand(100,1)); hold on; | |
7540
3422f39573b1
strcat.m: Matlab compatibility, with cstrcat.m replacing conventional strcat.m.
Ben Abbott <bpabbott@mac.com>
parents:
7148
diff
changeset
|
249 %! labels = {labels{:}, cstrcat("Signal ", num2str(i))}; |
6146 | 250 %! endfor; hold off; |
251 %! title("Signals with random offset and uniform noise") | |
252 %! xlabel("Sample Nr [k]"); ylabel("Amplitude [V]"); | |
6977 | 253 %! legend(labels, "location", "southoutside") |
6146 | 254 %! legend("boxon") |