Mercurial > hg > octave-nkf
annotate scripts/plot/legend.m @ 8190:73d6b71788c0
use case-insensitive comparison for graphics properties; misc style fixes
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Mon, 06 Oct 2008 21:06:05 -0400 |
parents | c066714ee5d5 |
children | f6ca8ff51818 |
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 | |
6257 | 83 nargs = nargin; |
6146 | 84 |
8102 | 85 ca = gca (); |
86 | |
6146 | 87 if (nargs > 0) |
88 pos = varargin{nargs}; | |
89 if (isnumeric (pos) && isscalar (pos) && round (pos) == pos) | |
6395 | 90 if (pos >= -1 && pos <= 4) |
6257 | 91 set (ca, "keypos", pos); |
6146 | 92 nargs--; |
93 else | |
94 error ("legend: invalid position specified"); | |
95 endif | |
96 endif | |
97 endif | |
6977 | 98 |
99 if (nargs > 1) | |
100 pos = varargin{nargs-1}; | |
7054 | 101 str = varargin{nargs}; |
6977 | 102 if (strcmpi (pos, "location") && ischar (str)) |
103 set (ca, "keypos", str); | |
104 nargs -= 2; | |
105 endif | |
106 endif | |
6146 | 107 |
6257 | 108 kids = get (ca, "children"); |
109 nkids = numel (kids); | |
110 k = 1; | |
111 turn_on_legend = false; | |
112 | |
6146 | 113 if (nargs == 1) |
114 arg = varargin{1}; | |
115 if (ischar (arg)) | |
116 if (rows (arg) == 1) | |
117 str = tolower (deblank (arg)); | |
118 switch (str) | |
119 case {"off", "hide"} | |
6257 | 120 set (ca, "key", "off"); |
6146 | 121 case "show" |
6257 | 122 set (ca, "key", "on"); |
6146 | 123 case "toggle" |
6257 | 124 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
|
125 if (strcmpi (val, "on")) |
6257 | 126 set (ca, "key", "off"); |
127 else | |
128 set (ca, "key", "on"); | |
129 endif | |
6146 | 130 case "boxon" |
6257 | 131 set (ca, "key", "on", "keybox", "on"); |
6146 | 132 case "boxoff" |
6257 | 133 set (ca, "keybox", "off"); |
6146 | 134 otherwise |
8102 | 135 while (k <= nkids && ! strcmp (get (kids(k), "type"), "line")) |
6257 | 136 k++; |
137 endwhile | |
138 if (k <= nkids) | |
139 turn_on_legend = true; | |
8102 | 140 set (kids(k), "keylabel", arg); |
6257 | 141 else |
142 warning ("legend: ignoring extra labels"); | |
143 endif | |
6146 | 144 endswitch |
145 nargs--; | |
146 else | |
147 varargin = cellstr (arg); | |
6740 | 148 nargs = numel (varargin); |
6146 | 149 endif |
150 elseif (iscellstr (arg)) | |
151 varargin = arg; | |
152 nargs = numel (varargin); | |
153 else | |
154 error ("legend: expecting argument to be a character string"); | |
155 endif | |
156 endif | |
157 | |
6272 | 158 if (nargs > 0) |
159 have_data = false; | |
160 for i = 1:nkids | |
7148 | 161 if (strcmp (get (kids(k), "type"), "line") |
162 || strcmp (get (kids(k), "type"), "surface") | |
8102 | 163 || strcmp (get (kids(k), "type"), "patch")) |
6272 | 164 have_data = true; |
165 break; | |
166 endif | |
167 endfor | |
168 if (! have_data) | |
169 warning ("legend: plot data is empty; setting key labels has no effect"); | |
170 endif | |
6147 | 171 endif |
172 | |
6575 | 173 warned = false; |
6146 | 174 for i = 1:nargs |
175 arg = varargin{i}; | |
176 if (ischar (arg)) | |
7148 | 177 while (k <= nkids |
178 && ! (strcmp (get (kids(k), "type"), "line") | |
179 || strcmp (get (kids(k), "type"), "surface") | |
8102 | 180 || strcmp (get (kids(k), "type"), "patch"))) |
6257 | 181 k++; |
182 endwhile | |
183 if (k <= nkids) | |
8102 | 184 set (kids(k), "keylabel", arg); |
6257 | 185 turn_on_legend = true; |
6450 | 186 k++; |
6257 | 187 elseif (! warned) |
188 warned = true; | |
189 warning ("legend: ignoring extra labels"); | |
190 endif | |
6146 | 191 else |
192 error ("legend: expecting argument to be a character string"); | |
193 endif | |
194 endfor | |
195 | |
6257 | 196 if (turn_on_legend) |
197 set (ca, "key", "on"); | |
6146 | 198 endif |
199 | |
200 endfunction | |
201 | |
202 %!demo | |
203 %! close all; | |
204 %! plot(1:10, 1:10); | |
205 %! title("a very long label can sometimes cause problems"); | |
6977 | 206 %! legend({"hello world"}, "location", "northeastoutside") |
6146 | 207 |
208 %!demo | |
209 %! close all; | |
210 %! labels = {}; | |
211 %! for i = 1:5 | |
212 %! 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
|
213 %! labels = {labels{:}, cstrcat("Signal ", num2str(i))}; |
6146 | 214 %! endfor; hold off; |
215 %! title("Signals with random offset and uniform noise") | |
216 %! xlabel("Sample Nr [k]"); ylabel("Amplitude [V]"); | |
6977 | 217 %! legend(labels, "location", "southoutside") |
6146 | 218 %! legend("boxon") |