comparison scripts/plot/legend.m @ 12096:61c4f397499c release-3-2-x

legend.m: fix legend order
author John W. Eaton <jwe@octave.org>
date Fri, 20 Nov 2009 10:43:14 +0100
parents eb63fbe60fab
children f9fb8c1a8e45
comparison
equal deleted inserted replaced
12095:8d26637b1e19 12096:61c4f397499c
113 if (rows (arg) == 1) 113 if (rows (arg) == 1)
114 str = tolower (deblank (arg)); 114 str = tolower (deblank (arg));
115 switch (str) 115 switch (str)
116 case {"off", "hide"} 116 case {"off", "hide"}
117 set (ca, "key", "off"); 117 set (ca, "key", "off");
118 nargs--;
118 case "show" 119 case "show"
119 set (ca, "key", "on"); 120 set (ca, "key", "on");
121 nargs--;
120 case "toggle" 122 case "toggle"
121 val = get (ca, "key"); 123 val = get (ca, "key");
122 if (strcmpi (val, "on")) 124 if (strcmpi (val, "on"))
123 set (ca, "key", "off"); 125 set (ca, "key", "off");
124 else 126 else
125 set (ca, "key", "on"); 127 set (ca, "key", "on");
126 endif 128 endif
129 nargs--;
127 case "boxon" 130 case "boxon"
128 set (ca, "key", "on", "keybox", "on"); 131 set (ca, "key", "on", "keybox", "on");
132 nargs--;
129 case "boxoff" 133 case "boxoff"
130 set (ca, "keybox", "off"); 134 set (ca, "keybox", "off");
135 nargs--;
131 case "left" 136 case "left"
132 set (ca, "keyreverse", "off") 137 set (ca, "keyreverse", "off")
138 nargs--;
133 case "right" 139 case "right"
134 set (ca, "keyreverse", "on") 140 set (ca, "keyreverse", "on")
141 nargs--;
135 otherwise 142 otherwise
136 typ = get (kids (k), "type");
137 while (k <= nkids && ! strcmp (typ, "line") &&
138 ! strcmp (typ, "hggroup"))
139 k++;
140 typ = get (kids (k), "type");
141 endwhile
142 if (k <= nkids)
143 turn_on_legend = true;
144 if (strcmp (typ, "hggroup"))
145 hgkids = get (kids(k), "children");
146 for j = 1 : length (hgkids)
147 hgobj = get (hgkids (j));
148 if (isfield (hgobj, "keylabel"))
149 set (hgkids(j), "keylabel", arg);
150 break;
151 endif
152 endfor
153 else
154 set (kids(k), "keylabel", arg);
155 endif
156 else
157 warning ("legend: ignoring extra labels");
158 endif
159 endswitch 143 endswitch
160 nargs--;
161 else 144 else
162 varargin = cellstr (arg); 145 varargin = cellstr (arg);
163 nargs = numel (varargin); 146 nargs = numel (varargin);
164 endif 147 endif
165 elseif (iscellstr (arg)) 148 elseif (iscellstr (arg))
170 endif 153 endif
171 endif 154 endif
172 155
173 if (nargs > 0) 156 if (nargs > 0)
174 have_data = false; 157 have_data = false;
175 for i = 1:nkids 158 for k = 1:nkids
176 if (strcmp (get (kids(k), "type"), "line") 159 typ = get (kids(k), "type");
177 || strcmp (get (kids(k), "type"), "surface") 160 if (strcmp (typ, "line") || strcmp (typ, "surface")
178 || strcmp (get (kids(k), "type"), "patch") 161 || strcmp (typ, "patch") || strcmp (typ, "hggroup"))
179 || strcmp (get (kids(k), "type"), "hggroup"))
180 have_data = true; 162 have_data = true;
181 break; 163 break;
182 endif 164 endif
183 endfor 165 endfor
184 if (! have_data) 166 if (! have_data)
185 warning ("legend: plot data is empty; setting key labels has no effect"); 167 warning ("legend: plot data is empty; setting key labels has no effect");
186 endif 168 endif
187 endif 169 endif
188 170
189 warned = false; 171 warned = false;
190 for i = nargs:-1:1 172 k = nkids;
173 for i = 1:nargs
191 arg = varargin{i}; 174 arg = varargin{i};
192 if (ischar (arg)) 175 if (ischar (arg))
193 while (k <= nkids 176 typ = get (kids(k), "type");
194 && ! (strcmp (get (kids(k), "type"), "line") 177 while (k > 1
195 || strcmp (get (kids(k), "type"), "surface") 178 && ! (strcmp (typ, "line") || strcmp (typ, "surface")
196 || strcmp (get (kids(k), "type"), "patch") 179 || strcmp (typ, "patch") || strcmp (typ, "hggroup")))
197 || strcmp (get (kids(k), "type"), "hggroup"))) 180 typ = get (kids(--k), "type");
198 k++;
199 endwhile 181 endwhile
200 if (k <= nkids) 182 if (k > 0)
201 if (strcmp (get (kids(k), "type"), "hggroup")) 183 if (strcmp (get (kids(k), "type"), "hggroup"))
202 hgkids = get (kids(k), "children"); 184 hgkids = get (kids(k), "children");
203 for j = 1 : length (hgkids) 185 for j = 1 : length (hgkids)
204 hgobj = get (hgkids (j)); 186 hgobj = get (hgkids (j));
205 if (isfield (hgobj, "keylabel")) 187 if (isfield (hgobj, "keylabel"))
209 endfor 191 endfor
210 else 192 else
211 set (kids(k), "keylabel", arg); 193 set (kids(k), "keylabel", arg);
212 endif 194 endif
213 turn_on_legend = true; 195 turn_on_legend = true;
214 k++; 196 if (--k == 0)
197 break;
198 endif
215 elseif (! warned) 199 elseif (! warned)
216 warned = true; 200 warned = true;
217 warning ("legend: ignoring extra labels"); 201 warning ("legend: ignoring extra labels");
218 endif 202 endif
219 else 203 else
233 %! title("incline is blue and decline is green"); 217 %! title("incline is blue and decline is green");
234 %! legend({"I'm blue", "I'm green"}, "location", "east") 218 %! legend({"I'm blue", "I'm green"}, "location", "east")
235 219
236 %!demo 220 %!demo
237 %! clf 221 %! clf
222 %! plot(1:10, 1:10, 1:10, fliplr(1:10));
223 %! title("incline is blue and decline is green");
224 %! legend("I'm blue", "I'm green", "location", "east")
225
226 %!demo
227 %! clf
238 %! plot(1:10, 1:10); 228 %! plot(1:10, 1:10);
239 %! title("a very long label can sometimes cause problems"); 229 %! title("a very long label can sometimes cause problems");
240 %! legend({"hello world"}, "location", "northeastoutside") 230 %! legend({"hello world"}, "location", "northeastoutside")
231
232 %!demo
233 %! clf
234 %! plot(1:10, 1:10);
235 %! title("a very long label can sometimes cause problems");
236 %! legend("hello world", "location", "northeastoutside")
241 237
242 %!demo 238 %!demo
243 %! clf 239 %! clf
244 %! labels = {}; 240 %! labels = {};
245 %! for i = 1:5 241 %! for i = 1:5
249 %! endfor; hold off; 245 %! endfor; hold off;
250 %! title("Signals with random offset and uniform noise") 246 %! title("Signals with random offset and uniform noise")
251 %! xlabel("Sample Nr [k]"); ylabel("Amplitude [V]"); 247 %! xlabel("Sample Nr [k]"); ylabel("Amplitude [V]");
252 %! legend(labels, "location", "southoutside") 248 %! legend(labels, "location", "southoutside")
253 %! legend("boxon") 249 %! legend("boxon")
250
251 %!demo
252 %! clf
253 %! labels = {};
254 %! for i = 1:5
255 %! h = plot(1:100, i + rand(100,1)); hold on;
256 %! set (h, "color", get (gca, "colororder")(i,:))
257 %! labels = {labels{:}, cstrcat("Signal ", num2str(i))};
258 %! endfor; hold off;
259 %! title("Signals with random offset and uniform noise")
260 %! xlabel("Sample Nr [k]"); ylabel("Amplitude [V]");
261 %! legend(labels{:}, "location", "southoutside")
262 %! legend("boxon")
263
264 %!demo
265 %! hold ("off");
266 %! x = linspace (0, 10);
267 %! plot (x, x);
268 %! hold ("on");
269 %! stem (x, x.^2, 'g')
270 %! legend ("linear");
271 %! hold ("off");
272
273 %!demo
274 %! x = linspace (0, 10);
275 %! plot (x, x, x, x.^2);
276 %! legend ("linear");
277
278 %!demo
279 %! x = linspace (0, 10);
280 %! plot (x, x, x, x.^2);
281 %! legend ("linear", "quadratic");