Mercurial > hg > octave-nkf
annotate scripts/plot/legend.m @ 8208:f6ca8ff51818
[mq]: graphics-backend
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Fri, 10 Oct 2008 11:07:53 -0400 |
parents | 73d6b71788c0 |
children | 79c874fe5100 |
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"); |
6146 | 133 otherwise |
8208 | 134 typ = get (kids (k), "type"); |
135 while (k <= nkids && ! strcmp (typ, "line") && | |
136 ! strcmp (typ, "hggroup")) | |
6257 | 137 k++; |
138 endwhile | |
139 if (k <= nkids) | |
140 turn_on_legend = true; | |
8208 | 141 if (strcmp (typ, "hggroup")) |
142 hgkids = get (kids(k), "children"); | |
143 for j = 1 : length (hgkids) | |
144 hgobj = get (hgkids (j)); | |
145 if (isfield (hgobj, "keylabel")) | |
146 set (hgkids(j), "keylabel", arg); | |
147 break; | |
148 endif | |
149 endfor | |
150 else | |
151 set (kids(k), "keylabel", arg); | |
152 endif | |
6257 | 153 else |
154 warning ("legend: ignoring extra labels"); | |
155 endif | |
6146 | 156 endswitch |
157 nargs--; | |
158 else | |
159 varargin = cellstr (arg); | |
6740 | 160 nargs = numel (varargin); |
6146 | 161 endif |
162 elseif (iscellstr (arg)) | |
163 varargin = arg; | |
164 nargs = numel (varargin); | |
165 else | |
166 error ("legend: expecting argument to be a character string"); | |
167 endif | |
168 endif | |
169 | |
6272 | 170 if (nargs > 0) |
171 have_data = false; | |
172 for i = 1:nkids | |
7148 | 173 if (strcmp (get (kids(k), "type"), "line") |
174 || strcmp (get (kids(k), "type"), "surface") | |
8208 | 175 || strcmp (get (kids(k), "type"), "patch") |
176 || strcmp (get (kids(k), "type"), "hggroup")) | |
6272 | 177 have_data = true; |
178 break; | |
179 endif | |
180 endfor | |
181 if (! have_data) | |
182 warning ("legend: plot data is empty; setting key labels has no effect"); | |
183 endif | |
6147 | 184 endif |
185 | |
6575 | 186 warned = false; |
6146 | 187 for i = 1:nargs |
188 arg = varargin{i}; | |
189 if (ischar (arg)) | |
7148 | 190 while (k <= nkids |
191 && ! (strcmp (get (kids(k), "type"), "line") | |
192 || strcmp (get (kids(k), "type"), "surface") | |
8208 | 193 || strcmp (get (kids(k), "type"), "patch") |
194 || strcmp (get (kids(k), "type"), "hggroup"))) | |
6257 | 195 k++; |
196 endwhile | |
197 if (k <= nkids) | |
8208 | 198 if (strcmp (get (kids(k), "type"), "hggroup")) |
199 hgkids = get (kids(k), "children"); | |
200 for j = 1 : length (hgkids) | |
201 hgobj = get (hgkids (j)); | |
202 if (isfield (hgobj, "keylabel")) | |
203 set (hgkids(j), "keylabel", arg); | |
204 break; | |
205 endif | |
206 endfor | |
207 else | |
208 set (kids(k), "keylabel", arg); | |
209 endif | |
6257 | 210 turn_on_legend = true; |
6450 | 211 k++; |
6257 | 212 elseif (! warned) |
213 warned = true; | |
214 warning ("legend: ignoring extra labels"); | |
215 endif | |
6146 | 216 else |
8208 | 217 arg |
218 get(kids(k),"type") | |
219 k | |
6146 | 220 error ("legend: expecting argument to be a character string"); |
221 endif | |
222 endfor | |
223 | |
6257 | 224 if (turn_on_legend) |
225 set (ca, "key", "on"); | |
6146 | 226 endif |
227 | |
228 endfunction | |
229 | |
230 %!demo | |
231 %! close all; | |
232 %! plot(1:10, 1:10); | |
233 %! title("a very long label can sometimes cause problems"); | |
6977 | 234 %! legend({"hello world"}, "location", "northeastoutside") |
6146 | 235 |
236 %!demo | |
237 %! close all; | |
238 %! labels = {}; | |
239 %! for i = 1:5 | |
240 %! 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
|
241 %! labels = {labels{:}, cstrcat("Signal ", num2str(i))}; |
6146 | 242 %! endfor; hold off; |
243 %! title("Signals with random offset and uniform noise") | |
244 %! xlabel("Sample Nr [k]"); ylabel("Amplitude [V]"); | |
6977 | 245 %! legend(labels, "location", "southoutside") |
6146 | 246 %! legend("boxon") |