Mercurial > hg > octave-lyh
annotate scripts/plot/legend.m @ 10727:570f30a48732
split printing between backends
author | Shai Ayal <shaiay@users.sourceforge.net> |
---|---|
date | Thu, 24 Jun 2010 07:06:11 +0300 |
parents | 7a43499f074a |
children | 693e22af08ae |
rev | line source |
---|---|
8920 | 1 ## Copyright (C) 2001, 2006, 2007, 2008, 2009 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 -*- | |
10666
7a43499f074a
legend.m: Improve documentation string
Rik <octave@nomad.inbox5.com>
parents:
10662
diff
changeset
|
21 ## @deftypefn {Function File} {} legend (@var{str1}, @var{str2}, @dots{}) |
6146 | 22 ## @deftypefnx {Function File} {} legend (@var{matstr}) |
23 ## @deftypefnx {Function File} {} legend (@var{cell}) | |
10666
7a43499f074a
legend.m: Improve documentation string
Rik <octave@nomad.inbox5.com>
parents:
10662
diff
changeset
|
24 ## @deftypefnx {Function File} {} legend (@dots{}, "location", @var{pos}) |
10662
3afcd24ced61
legend.m: Key labels for specified objects.
Ben Abbott <bpabbott@mac.com>
parents:
10549
diff
changeset
|
25 ## @deftypefnx {Function File} {} legend (@var{hax}, @dots{}) |
3afcd24ced61
legend.m: Key labels for specified objects.
Ben Abbott <bpabbott@mac.com>
parents:
10549
diff
changeset
|
26 ## @deftypefnx {Function File} {} legend (@var{hobjs}, @dots{}) |
3afcd24ced61
legend.m: Key labels for specified objects.
Ben Abbott <bpabbott@mac.com>
parents:
10549
diff
changeset
|
27 ## @deftypefnx {Function File} {} legend (@var{hax}, @var{hobjs}, @dots{}) |
10666
7a43499f074a
legend.m: Improve documentation string
Rik <octave@nomad.inbox5.com>
parents:
10662
diff
changeset
|
28 ## @deftypefnx {Function File} {} legend ("@var{option}") |
6146 | 29 ## |
10662
3afcd24ced61
legend.m: Key labels for specified objects.
Ben Abbott <bpabbott@mac.com>
parents:
10549
diff
changeset
|
30 ## Display a legend for the axes with handle @var{hax}, or the current axes, |
3afcd24ced61
legend.m: Key labels for specified objects.
Ben Abbott <bpabbott@mac.com>
parents:
10549
diff
changeset
|
31 ## using the specified strings as labels. Legend entries may be specified |
3afcd24ced61
legend.m: Key labels for specified objects.
Ben Abbott <bpabbott@mac.com>
parents:
10549
diff
changeset
|
32 ## as individual character string arguments, a character array, or a cell |
10666
7a43499f074a
legend.m: Improve documentation string
Rik <octave@nomad.inbox5.com>
parents:
10662
diff
changeset
|
33 ## array of character strings. If the handles, @var{hobjs}, are not specified |
7a43499f074a
legend.m: Improve documentation string
Rik <octave@nomad.inbox5.com>
parents:
10662
diff
changeset
|
34 ## then the legend's strings will be associated with the axes' descendants. |
10662
3afcd24ced61
legend.m: Key labels for specified objects.
Ben Abbott <bpabbott@mac.com>
parents:
10549
diff
changeset
|
35 ## Legend works on line graphs, bar graphs, etc. |
3afcd24ced61
legend.m: Key labels for specified objects.
Ben Abbott <bpabbott@mac.com>
parents:
10549
diff
changeset
|
36 ## A plot must exist before legend is called. |
6146 | 37 ## |
6895 | 38 ## The optional parameter @var{pos} specifies the location of the legend |
39 ## as follows: | |
6146 | 40 ## |
6977 | 41 ## @multitable @columnfractions 0.06 0.14 0.80 |
42 ## @item @tab north @tab | |
43 ## center top | |
44 ## @item @tab south @tab | |
45 ## center bottom | |
46 ## @item @tab east @tab | |
47 ## right center | |
48 ## @item @tab west @tab | |
49 ## left center | |
50 ## @item @tab northeast @tab | |
51 ## right top (default) | |
52 ## @item @tab northwest @tab | |
53 ## left top | |
54 ## @item @tab southeast @tab | |
55 ## right bottom | |
56 ## @item @tab southwest @tab | |
57 ## left bottom | |
58 ## @item | |
59 ## @item @tab outside @tab | |
60 ## can be appended to any location string | |
6146 | 61 ## @end multitable |
62 ## | |
10666
7a43499f074a
legend.m: Improve documentation string
Rik <octave@nomad.inbox5.com>
parents:
10662
diff
changeset
|
63 ## The following customizations are available using @var{option}: |
6146 | 64 ## |
7148 | 65 ## @table @asis |
6895 | 66 ## @item "show" |
10666
7a43499f074a
legend.m: Improve documentation string
Rik <octave@nomad.inbox5.com>
parents:
10662
diff
changeset
|
67 ## Show legend on the plot |
6895 | 68 ## @item "hide" |
7148 | 69 ## @itemx "off" |
10666
7a43499f074a
legend.m: Improve documentation string
Rik <octave@nomad.inbox5.com>
parents:
10662
diff
changeset
|
70 ## Hide legend on the plot |
6895 | 71 ## @item "boxon" |
10666
7a43499f074a
legend.m: Improve documentation string
Rik <octave@nomad.inbox5.com>
parents:
10662
diff
changeset
|
72 ## Show a box around legend |
6895 | 73 ## @item "boxoff" |
10666
7a43499f074a
legend.m: Improve documentation string
Rik <octave@nomad.inbox5.com>
parents:
10662
diff
changeset
|
74 ## Hide the box around legend |
6895 | 75 ## @item "left" |
10666
7a43499f074a
legend.m: Improve documentation string
Rik <octave@nomad.inbox5.com>
parents:
10662
diff
changeset
|
76 ## Place text to the left of the keys |
6895 | 77 ## @item "right" |
10666
7a43499f074a
legend.m: Improve documentation string
Rik <octave@nomad.inbox5.com>
parents:
10662
diff
changeset
|
78 ## Place text to the right of the keys |
6146 | 79 ## @end table |
80 ## @end deftypefn | |
81 | |
82 function legend (varargin) | |
83 | |
8208 | 84 [ca, varargin, nargin] = __plt_get_axis_arg__ ("legend", varargin{:}); |
6257 | 85 nargs = nargin; |
6146 | 86 |
10662
3afcd24ced61
legend.m: Key labels for specified objects.
Ben Abbott <bpabbott@mac.com>
parents:
10549
diff
changeset
|
87 if (all (ishandle (varargin{1}))) |
3afcd24ced61
legend.m: Key labels for specified objects.
Ben Abbott <bpabbott@mac.com>
parents:
10549
diff
changeset
|
88 kids = flipud (varargin{1}(:)); |
3afcd24ced61
legend.m: Key labels for specified objects.
Ben Abbott <bpabbott@mac.com>
parents:
10549
diff
changeset
|
89 varargin(1) = []; |
3afcd24ced61
legend.m: Key labels for specified objects.
Ben Abbott <bpabbott@mac.com>
parents:
10549
diff
changeset
|
90 nargs = numel (varargin); |
3afcd24ced61
legend.m: Key labels for specified objects.
Ben Abbott <bpabbott@mac.com>
parents:
10549
diff
changeset
|
91 else |
3afcd24ced61
legend.m: Key labels for specified objects.
Ben Abbott <bpabbott@mac.com>
parents:
10549
diff
changeset
|
92 kids = get (ca, "children"); |
3afcd24ced61
legend.m: Key labels for specified objects.
Ben Abbott <bpabbott@mac.com>
parents:
10549
diff
changeset
|
93 endif |
3afcd24ced61
legend.m: Key labels for specified objects.
Ben Abbott <bpabbott@mac.com>
parents:
10549
diff
changeset
|
94 nkids = numel (kids); |
3afcd24ced61
legend.m: Key labels for specified objects.
Ben Abbott <bpabbott@mac.com>
parents:
10549
diff
changeset
|
95 |
6146 | 96 if (nargs > 0) |
97 pos = varargin{nargs}; | |
98 if (isnumeric (pos) && isscalar (pos) && round (pos) == pos) | |
6395 | 99 if (pos >= -1 && pos <= 4) |
10549 | 100 set (ca, "keypos", pos); |
101 nargs--; | |
6146 | 102 else |
10549 | 103 error ("legend: invalid position specified"); |
6146 | 104 endif |
105 endif | |
106 endif | |
6977 | 107 |
108 if (nargs > 1) | |
109 pos = varargin{nargs-1}; | |
7054 | 110 str = varargin{nargs}; |
6977 | 111 if (strcmpi (pos, "location") && ischar (str)) |
112 set (ca, "keypos", str); | |
113 nargs -= 2; | |
114 endif | |
115 endif | |
6146 | 116 |
6257 | 117 k = 1; |
118 turn_on_legend = false; | |
119 | |
6146 | 120 if (nargs == 1) |
121 arg = varargin{1}; | |
122 if (ischar (arg)) | |
123 if (rows (arg) == 1) | |
10549 | 124 str = tolower (deblank (arg)); |
125 switch (str) | |
126 case {"off", "hide"} | |
127 set (ca, "key", "off"); | |
128 nargs--; | |
129 case "show" | |
130 set (ca, "key", "on"); | |
131 nargs--; | |
132 case "toggle" | |
133 val = get (ca, "key"); | |
134 if (strcmpi (val, "on")) | |
135 set (ca, "key", "off"); | |
136 else | |
137 set (ca, "key", "on"); | |
138 endif | |
139 nargs--; | |
140 case "boxon" | |
141 set (ca, "key", "on", "keybox", "on"); | |
142 nargs--; | |
143 case "boxoff" | |
144 set (ca, "keybox", "off"); | |
145 nargs--; | |
146 case "left" | |
147 set (ca, "keyreverse", "off") | |
148 nargs--; | |
149 case "right" | |
150 set (ca, "keyreverse", "on") | |
151 nargs--; | |
152 otherwise | |
153 endswitch | |
6146 | 154 else |
10549 | 155 varargin = cellstr (arg); |
156 nargs = numel (varargin); | |
6146 | 157 endif |
158 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
|
159 varargin = arg; |
6146 | 160 nargs = numel (varargin); |
161 else | |
162 error ("legend: expecting argument to be a character string"); | |
163 endif | |
164 endif | |
165 | |
6272 | 166 if (nargs > 0) |
167 have_data = false; | |
9451 | 168 for k = 1:nkids |
169 typ = get (kids(k), "type"); | |
170 if (strcmp (typ, "line") || strcmp (typ, "surface") | |
10549 | 171 || strcmp (typ, "patch") || strcmp (typ, "hggroup")) |
172 have_data = true; | |
173 break; | |
6272 | 174 endif |
175 endfor | |
176 if (! have_data) | |
177 warning ("legend: plot data is empty; setting key labels has no effect"); | |
178 endif | |
6147 | 179 endif |
180 | |
6575 | 181 warned = false; |
9451 | 182 k = nkids; |
183 for i = 1:nargs | |
6146 | 184 arg = varargin{i}; |
185 if (ischar (arg)) | |
9451 | 186 typ = get (kids(k), "type"); |
187 while (k > 1 | |
10549 | 188 && ! (strcmp (typ, "line") || strcmp (typ, "surface") |
189 || strcmp (typ, "patch") || strcmp (typ, "hggroup"))) | |
190 typ = get (kids(--k), "type"); | |
6257 | 191 endwhile |
9451 | 192 if (k > 0) |
10549 | 193 if (strcmp (get (kids(k), "type"), "hggroup")) |
194 hgkids = get (kids(k), "children"); | |
195 for j = 1 : length (hgkids) | |
196 hgobj = get (hgkids (j)); | |
197 if (isfield (hgobj, "keylabel")) | |
198 set (hgkids(j), "keylabel", arg); | |
199 break; | |
200 endif | |
201 endfor | |
202 else | |
203 set (kids(k), "keylabel", arg); | |
204 endif | |
205 turn_on_legend = true; | |
206 if (--k == 0) | |
207 break; | |
208 endif | |
6257 | 209 elseif (! warned) |
10549 | 210 warned = true; |
211 warning ("legend: ignoring extra labels"); | |
6257 | 212 endif |
6146 | 213 else |
214 error ("legend: expecting argument to be a character string"); | |
215 endif | |
216 endfor | |
217 | |
6257 | 218 if (turn_on_legend) |
219 set (ca, "key", "on"); | |
6146 | 220 endif |
221 | |
222 endfunction | |
223 | |
224 %!demo | |
8790
a013ff655ca4
Trivial changes to demos to produce a more pleasant output for octave+gnuplot+aquaterm.
Ben Abbott <bpabbott@mac.com>
parents:
8746
diff
changeset
|
225 %! clf |
8343
9f34f7636fe0
legend.m: Correct ording of legend labels.
Ben Abbott <bpabbott@mac.com>
parents:
8291
diff
changeset
|
226 %! 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
|
227 %! 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
|
228 %! 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
|
229 |
9f34f7636fe0
legend.m: Correct ording of legend labels.
Ben Abbott <bpabbott@mac.com>
parents:
8291
diff
changeset
|
230 %!demo |
8790
a013ff655ca4
Trivial changes to demos to produce a more pleasant output for octave+gnuplot+aquaterm.
Ben Abbott <bpabbott@mac.com>
parents:
8746
diff
changeset
|
231 %! clf |
9451 | 232 %! plot(1:10, 1:10, 1:10, fliplr(1:10)); |
233 %! title("incline is blue and decline is green"); | |
234 %! legend("I'm blue", "I'm green", "location", "east") | |
235 | |
236 %!demo | |
237 %! clf | |
6146 | 238 %! plot(1:10, 1:10); |
239 %! title("a very long label can sometimes cause problems"); | |
6977 | 240 %! legend({"hello world"}, "location", "northeastoutside") |
6146 | 241 |
242 %!demo | |
8790
a013ff655ca4
Trivial changes to demos to produce a more pleasant output for octave+gnuplot+aquaterm.
Ben Abbott <bpabbott@mac.com>
parents:
8746
diff
changeset
|
243 %! clf |
9451 | 244 %! plot(1:10, 1:10); |
245 %! title("a very long label can sometimes cause problems"); | |
246 %! legend("hello world", "location", "northeastoutside") | |
247 | |
248 %!demo | |
249 %! clf | |
6146 | 250 %! labels = {}; |
251 %! for i = 1:5 | |
8915
485eabc0cfec
__go_draw_axes__.m: Preserve the order of axes' children when pruning the handles for the {x,y,z}labels and title.
Ben Abbott <bpabbott@mac.com>
parents:
8790
diff
changeset
|
252 %! h = plot(1:100, i + rand(100,1)); hold on; |
485eabc0cfec
__go_draw_axes__.m: Preserve the order of axes' children when pruning the handles for the {x,y,z}labels and title.
Ben Abbott <bpabbott@mac.com>
parents:
8790
diff
changeset
|
253 %! set (h, "color", get (gca, "colororder")(i,:)) |
7540
3422f39573b1
strcat.m: Matlab compatibility, with cstrcat.m replacing conventional strcat.m.
Ben Abbott <bpabbott@mac.com>
parents:
7148
diff
changeset
|
254 %! labels = {labels{:}, cstrcat("Signal ", num2str(i))}; |
6146 | 255 %! endfor; hold off; |
256 %! title("Signals with random offset and uniform noise") | |
257 %! xlabel("Sample Nr [k]"); ylabel("Amplitude [V]"); | |
6977 | 258 %! legend(labels, "location", "southoutside") |
6146 | 259 %! legend("boxon") |
9451 | 260 |
261 %!demo | |
262 %! clf | |
263 %! labels = {}; | |
264 %! for i = 1:5 | |
265 %! h = plot(1:100, i + rand(100,1)); hold on; | |
266 %! set (h, "color", get (gca, "colororder")(i,:)) | |
267 %! labels = {labels{:}, cstrcat("Signal ", num2str(i))}; | |
268 %! endfor; hold off; | |
269 %! title("Signals with random offset and uniform noise") | |
270 %! xlabel("Sample Nr [k]"); ylabel("Amplitude [V]"); | |
271 %! legend(labels{:}, "location", "southoutside") | |
272 %! legend("boxon") | |
273 | |
274 %!demo | |
9457
f9fb8c1a8e45
legend.m: Add an hggroup demo using bar(). Include clf() in each demo.
Ben Abbott <bpabbott@mac.com>
parents:
9451
diff
changeset
|
275 %! clf |
9451 | 276 %! x = linspace (0, 10); |
277 %! plot (x, x); | |
278 %! hold ("on"); | |
279 %! stem (x, x.^2, 'g') | |
280 %! legend ("linear"); | |
281 %! hold ("off"); | |
282 | |
283 %!demo | |
9457
f9fb8c1a8e45
legend.m: Add an hggroup demo using bar(). Include clf() in each demo.
Ben Abbott <bpabbott@mac.com>
parents:
9451
diff
changeset
|
284 %! clf |
9451 | 285 %! x = linspace (0, 10); |
286 %! plot (x, x, x, x.^2); | |
287 %! legend ("linear"); | |
288 | |
289 %!demo | |
9457
f9fb8c1a8e45
legend.m: Add an hggroup demo using bar(). Include clf() in each demo.
Ben Abbott <bpabbott@mac.com>
parents:
9451
diff
changeset
|
290 %! clf |
9451 | 291 %! x = linspace (0, 10); |
292 %! plot (x, x, x, x.^2); | |
293 %! legend ("linear", "quadratic"); | |
9457
f9fb8c1a8e45
legend.m: Add an hggroup demo using bar(). Include clf() in each demo.
Ben Abbott <bpabbott@mac.com>
parents:
9451
diff
changeset
|
294 |
f9fb8c1a8e45
legend.m: Add an hggroup demo using bar(). Include clf() in each demo.
Ben Abbott <bpabbott@mac.com>
parents:
9451
diff
changeset
|
295 %!demo |
f9fb8c1a8e45
legend.m: Add an hggroup demo using bar(). Include clf() in each demo.
Ben Abbott <bpabbott@mac.com>
parents:
9451
diff
changeset
|
296 %! clf |
f9fb8c1a8e45
legend.m: Add an hggroup demo using bar(). Include clf() in each demo.
Ben Abbott <bpabbott@mac.com>
parents:
9451
diff
changeset
|
297 %! bar (rand (2, 3)) |
f9fb8c1a8e45
legend.m: Add an hggroup demo using bar(). Include clf() in each demo.
Ben Abbott <bpabbott@mac.com>
parents:
9451
diff
changeset
|
298 %! ylim ([0 1.2]) |
f9fb8c1a8e45
legend.m: Add an hggroup demo using bar(). Include clf() in each demo.
Ben Abbott <bpabbott@mac.com>
parents:
9451
diff
changeset
|
299 %! legend ({"1st Bar", "2nd Bar", "3rd Bar"}) |
f9fb8c1a8e45
legend.m: Add an hggroup demo using bar(). Include clf() in each demo.
Ben Abbott <bpabbott@mac.com>
parents:
9451
diff
changeset
|
300 |
f9fb8c1a8e45
legend.m: Add an hggroup demo using bar(). Include clf() in each demo.
Ben Abbott <bpabbott@mac.com>
parents:
9451
diff
changeset
|
301 %!demo |
f9fb8c1a8e45
legend.m: Add an hggroup demo using bar(). Include clf() in each demo.
Ben Abbott <bpabbott@mac.com>
parents:
9451
diff
changeset
|
302 %! clf |
f9fb8c1a8e45
legend.m: Add an hggroup demo using bar(). Include clf() in each demo.
Ben Abbott <bpabbott@mac.com>
parents:
9451
diff
changeset
|
303 %! bar (rand (2, 3)) |
f9fb8c1a8e45
legend.m: Add an hggroup demo using bar(). Include clf() in each demo.
Ben Abbott <bpabbott@mac.com>
parents:
9451
diff
changeset
|
304 %! ylim ([0 1.2]) |
f9fb8c1a8e45
legend.m: Add an hggroup demo using bar(). Include clf() in each demo.
Ben Abbott <bpabbott@mac.com>
parents:
9451
diff
changeset
|
305 %! legend ("1st Bar", "2nd Bar", "3rd Bar") |
10662
3afcd24ced61
legend.m: Key labels for specified objects.
Ben Abbott <bpabbott@mac.com>
parents:
10549
diff
changeset
|
306 |
3afcd24ced61
legend.m: Key labels for specified objects.
Ben Abbott <bpabbott@mac.com>
parents:
10549
diff
changeset
|
307 %!demo |
3afcd24ced61
legend.m: Key labels for specified objects.
Ben Abbott <bpabbott@mac.com>
parents:
10549
diff
changeset
|
308 %! clf |
3afcd24ced61
legend.m: Key labels for specified objects.
Ben Abbott <bpabbott@mac.com>
parents:
10549
diff
changeset
|
309 %! x = 0:0.1:7; |
3afcd24ced61
legend.m: Key labels for specified objects.
Ben Abbott <bpabbott@mac.com>
parents:
10549
diff
changeset
|
310 %! h = plot (x, sin(x), x, cos(x), x, sin(x.^2/10), x, cos(x.^2/10)); |
3afcd24ced61
legend.m: Key labels for specified objects.
Ben Abbott <bpabbott@mac.com>
parents:
10549
diff
changeset
|
311 %! title ("Only the sin() objects have keylabels") |
3afcd24ced61
legend.m: Key labels for specified objects.
Ben Abbott <bpabbott@mac.com>
parents:
10549
diff
changeset
|
312 %! legend (h([1, 3]), {"sin(x)", "sin(x^2/10)"}, "location", "southwest") |