Mercurial > hg > octave-nkf
annotate scripts/plot/legend.m @ 8343:9f34f7636fe0
legend.m: Correct ording of legend labels.
author | Ben Abbott <bpabbott@mac.com> |
---|---|
date | Mon, 24 Nov 2008 12:27:03 -0500 |
parents | 53f35799b235 |
children | 774b44619c5c |
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)) | |
8343
9f34f7636fe0
legend.m: Correct ording of legend labels.
Ben Abbott <bpabbott@mac.com>
parents:
8291
diff
changeset
|
168 varargin = fliplr (arg); |
6146 | 169 nargs = numel (varargin); |
170 else | |
171 error ("legend: expecting argument to be a character string"); | |
172 endif | |
8343
9f34f7636fe0
legend.m: Correct ording of legend labels.
Ben Abbott <bpabbott@mac.com>
parents:
8291
diff
changeset
|
173 else |
9f34f7636fe0
legend.m: Correct ording of legend labels.
Ben Abbott <bpabbott@mac.com>
parents:
8291
diff
changeset
|
174 varargin(1:nargs) = fliplr (varargin(1:nargs)); |
6146 | 175 endif |
176 | |
6272 | 177 if (nargs > 0) |
178 have_data = false; | |
179 for i = 1:nkids | |
7148 | 180 if (strcmp (get (kids(k), "type"), "line") |
181 || strcmp (get (kids(k), "type"), "surface") | |
8208 | 182 || strcmp (get (kids(k), "type"), "patch") |
183 || strcmp (get (kids(k), "type"), "hggroup")) | |
6272 | 184 have_data = true; |
185 break; | |
186 endif | |
187 endfor | |
188 if (! have_data) | |
189 warning ("legend: plot data is empty; setting key labels has no effect"); | |
190 endif | |
6147 | 191 endif |
192 | |
6575 | 193 warned = false; |
6146 | 194 for i = 1:nargs |
195 arg = varargin{i}; | |
196 if (ischar (arg)) | |
7148 | 197 while (k <= nkids |
198 && ! (strcmp (get (kids(k), "type"), "line") | |
199 || strcmp (get (kids(k), "type"), "surface") | |
8208 | 200 || strcmp (get (kids(k), "type"), "patch") |
201 || strcmp (get (kids(k), "type"), "hggroup"))) | |
6257 | 202 k++; |
203 endwhile | |
204 if (k <= nkids) | |
8208 | 205 if (strcmp (get (kids(k), "type"), "hggroup")) |
206 hgkids = get (kids(k), "children"); | |
207 for j = 1 : length (hgkids) | |
208 hgobj = get (hgkids (j)); | |
209 if (isfield (hgobj, "keylabel")) | |
210 set (hgkids(j), "keylabel", arg); | |
211 break; | |
212 endif | |
213 endfor | |
214 else | |
215 set (kids(k), "keylabel", arg); | |
216 endif | |
6257 | 217 turn_on_legend = true; |
6450 | 218 k++; |
6257 | 219 elseif (! warned) |
220 warned = true; | |
221 warning ("legend: ignoring extra labels"); | |
222 endif | |
6146 | 223 else |
224 error ("legend: expecting argument to be a character string"); | |
225 endif | |
226 endfor | |
227 | |
6257 | 228 if (turn_on_legend) |
229 set (ca, "key", "on"); | |
6146 | 230 endif |
231 | |
232 endfunction | |
233 | |
234 %!demo | |
235 %! close all; | |
8343
9f34f7636fe0
legend.m: Correct ording of legend labels.
Ben Abbott <bpabbott@mac.com>
parents:
8291
diff
changeset
|
236 %! 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
|
237 %! 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
|
238 %! 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
|
239 |
9f34f7636fe0
legend.m: Correct ording of legend labels.
Ben Abbott <bpabbott@mac.com>
parents:
8291
diff
changeset
|
240 %!demo |
9f34f7636fe0
legend.m: Correct ording of legend labels.
Ben Abbott <bpabbott@mac.com>
parents:
8291
diff
changeset
|
241 %! close all; |
6146 | 242 %! plot(1:10, 1:10); |
243 %! title("a very long label can sometimes cause problems"); | |
6977 | 244 %! legend({"hello world"}, "location", "northeastoutside") |
6146 | 245 |
246 %!demo | |
247 %! close all; | |
248 %! labels = {}; | |
249 %! for i = 1:5 | |
250 %! 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
|
251 %! labels = {labels{:}, cstrcat("Signal ", num2str(i))}; |
6146 | 252 %! endfor; hold off; |
253 %! title("Signals with random offset and uniform noise") | |
254 %! xlabel("Sample Nr [k]"); ylabel("Amplitude [V]"); | |
6977 | 255 %! legend(labels, "location", "southoutside") |
6146 | 256 %! legend("boxon") |