6146
|
1 ## Copyright (C) 2001 Laurent Mazet |
|
2 ## Copyright (C) 2006 John W. Eaton |
|
3 ## |
|
4 ## This program is free software; it is distributed in the hope that it |
|
5 ## will be useful, but WITHOUT ANY WARRANTY; without even the implied |
|
6 ## warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See |
|
7 ## the GNU General Public License for more details. |
|
8 ## |
|
9 ## You should have received a copy of the GNU General Public License |
|
10 ## along with this file; see the file COPYING. If not, write to the |
|
11 ## Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA |
|
12 ## 02111-1307, USA. |
|
13 |
|
14 ## -*- texinfo -*- |
|
15 ## @deftypefn {Function File} {} legend (@var{st1}, @var{st2}, @var{st3}, @var{...}) |
|
16 ## @deftypefnx {Function File} {} legend (@var{st1}, @var{st2}, @var{st3}, @var{...}, @var{pos}) |
|
17 ## @deftypefnx {Function File} {} legend (@var{matstr}) |
|
18 ## @deftypefnx {Function File} {} legend (@var{matstr}, @var{pos}) |
|
19 ## @deftypefnx {Function File} {} legend (@var{cell}) |
|
20 ## @deftypefnx {Function File} {} legend (@var{cell}, @var{pos}) |
|
21 ## @deftypefnx {Function File} {} legend ('@var{func}') |
|
22 ## |
|
23 ## Legend puts a legend on the current plot using the specified strings |
|
24 ## as labels. Use independant strings (@var{st1}, @var{st2}, @var{st3}...), a |
|
25 ## matrix of strings (@var{matstr}), or a cell array of strings (@var{cell}) to |
|
26 ## specify legends. Legend works on line graphs, bar graphs, etc... |
|
27 ## Be sure to call plot before calling legend. |
|
28 ## |
|
29 ## @var{pos} optionally places the legend in the specified location: |
|
30 ## |
|
31 ## @multitable @columnfractions 0.1 0.1 0.8 |
|
32 ## @item @tab 0 @tab |
|
33 ## Don't move the legend box (default) |
|
34 ## @item @tab 1 @tab |
|
35 ## Upper right-hand corner |
|
36 ## @item @tab 2 @tab |
|
37 ## Upper left-hand corner |
|
38 ## @item @tab 3 @tab |
|
39 ## Lower left-hand corner |
|
40 ## @item @tab 4 @tab |
|
41 ## Lower right-hand corner |
|
42 ## @item @tab -1 @tab |
|
43 ## To the top right of the plot |
|
44 ## @item @tab -2 @tab |
|
45 ## To the bottom right of the plot |
|
46 ## @item @tab -3 @tab |
|
47 ## To the bottom of the plot |
|
48 ## @item @tab [@var{x}, @var{y}] @tab |
|
49 ## To the arbitrary postion in plot [@var{x}, @var{y}] |
|
50 ## @end multitable |
|
51 ## |
|
52 ## Some specific functions are directely avaliable using @var{func}: |
|
53 ## |
|
54 ## @table @code |
|
55 ## @item show |
|
56 ## Show legends from the plot |
|
57 ## @item hide |
|
58 ## @itemx off |
|
59 ## Hide legends from the plot |
|
60 ## @item boxon |
|
61 ## Draw a box around legends |
|
62 ## @item boxoff |
|
63 ## Withdraw the box around legends |
|
64 ## @item left |
|
65 ## Text is to the left of the keys |
|
66 ## @item right |
|
67 ## Text is to the right of the keys |
|
68 ## @end table |
|
69 ## @end deftypefn |
|
70 |
|
71 ## PKG_ADD mark_as_command legend |
|
72 |
|
73 function legend (varargin) |
|
74 |
|
75 __plot_globals__; |
|
76 |
|
77 cf = __current_figure__; |
|
78 mxi = __multiplot_xi__; |
|
79 myi = __multiplot_yi__; |
|
80 |
|
81 nargs = nargin; |
|
82 |
|
83 if (nargs > 0) |
|
84 pos = varargin{nargs}; |
|
85 if (isnumeric (pos) && isscalar (pos) && round (pos) == pos) |
|
86 if (pos >= -3 && pos <= 4) |
|
87 __plot_key_properties__{cf}{mxi,myi}.position = pos; |
|
88 nargs--; |
|
89 else |
|
90 error ("legend: invalid position specified"); |
|
91 endif |
|
92 endif |
|
93 endif |
|
94 |
|
95 if (nargs == 1) |
|
96 arg = varargin{1}; |
|
97 if (ischar (arg)) |
|
98 if (rows (arg) == 1) |
|
99 str = tolower (deblank (arg)); |
|
100 switch (str) |
|
101 case {"off", "hide"} |
|
102 __plot_key_properties__{cf}{mxi,myi}.visible = false; |
|
103 case "show" |
|
104 __plot_key_properties__{cf}{mxi,myi}.visible = true; |
|
105 case "toggle" |
|
106 __plot_key_properties__{cf}{mxi,myi}.visible ... |
|
107 = ! __plot_key_properties__{cf}{mxi,myi}.visible; |
|
108 case "boxon" |
|
109 __plot_key_properties__{cf}{mxi,myi}.visible = true; |
|
110 __plot_key_properties__{cf}{mxi,myi}.box = true; |
|
111 case "boxoff" |
|
112 __plot_key_properties__{cf}{mxi,myi}.box = false; |
|
113 otherwise |
|
114 __plot_key_labels__{cf}{mxi,myi}{1} = arg; |
|
115 endswitch |
|
116 nargs--; |
|
117 else |
|
118 varargin = cellstr (arg); |
|
119 nargs = numel (vargin); |
|
120 endif |
|
121 elseif (iscellstr (arg)) |
|
122 varargin = arg; |
|
123 nargs = numel (varargin); |
|
124 else |
|
125 error ("legend: expecting argument to be a character string"); |
|
126 endif |
|
127 endif |
|
128 |
|
129 for i = 1:nargs |
|
130 arg = varargin{i}; |
|
131 if (ischar (arg)) |
|
132 __plot_key_labels__{cf}{mxi,myi}{i} = arg; |
|
133 else |
|
134 error ("legend: expecting argument to be a character string"); |
|
135 endif |
|
136 endfor |
|
137 |
|
138 if (automatic_replot) |
|
139 replot (); |
|
140 endif |
|
141 |
|
142 endfunction |
|
143 |
|
144 %!demo |
|
145 %! close all; |
|
146 %! plot(1:10, 1:10); |
|
147 %! title("a very long label can sometimes cause problems"); |
|
148 %! legend({"hello world"}, -1) |
|
149 |
|
150 %!demo |
|
151 %! close all; |
|
152 %! labels = {}; |
|
153 %! for i = 1:5 |
|
154 %! plot(1:100, i + rand(100,1)); hold on; |
|
155 %! labels = {labels{:}, strcat("Signal ", num2str(i))}; |
|
156 %! endfor; hold off; |
|
157 %! title("Signals with random offset and uniform noise") |
|
158 %! xlabel("Sample Nr [k]"); ylabel("Amplitude [V]"); |
|
159 %! legend(labels, -1) |
|
160 %! legend("boxon") |