comparison scripts/plot/draw/polar.m @ 18779:b5b73959907f

polar.m: add circular theta/rho axes (bug 39495). * polar.m: Use hggroup to contain line and text segments for circular rho/theta axes. Add 'rtick' radial tick property to axes. Add listeners on axis appearance properties to update line and text labels in hggroup. Add %!demos.
author Andreas Weber <andy.weber.aw@gmail.com>
date Mon, 28 Apr 2014 19:58:36 +0200
parents d63878346099
children 56bff71de2ca
comparison
equal deleted inserted replaced
18777:be0978e94806 18779:b5b73959907f
85 endif 85 endif
86 else 86 else
87 print_usage (); 87 print_usage ();
88 endif 88 endif
89 89
90 set (hax, "xlim", [-maxr, maxr], "ylim", [-maxr, maxr], 90 set (hax, "xaxislocation", "zero", "yaxislocation", "zero",
91 "xaxislocation", "zero", "yaxislocation", "zero",
92 "plotboxaspectratio", [1, 1, 1]); 91 "plotboxaspectratio", [1, 1, 1]);
92
93 ## Hide standard axes
94 set(hax, "visible", "off");
95
96 if (!isprop (hax, "rtick"))
97 addproperty ("rtick", hax, "data");
98 endif
99
100 ## calculate r(ho)tick from xtick
101 xtick = get (hax, "xtick");
102 rtick = xtick(find (xtick > 0, 1):find (xtick >= maxr, 1));
103 set (hax, "rtick", rtick);
104
105 addlistener (hax, "rtick", @__update_polar_grid__);
106 addlistener (hax, "fontsize", @__update_text__);
107 addlistener (hax, "linewidth", @__update_lines__);
108
109 ## add t(heta)tick
110 if (!isprop (hax, "ttick"))
111 addproperty ("ttick", hax, "data");
112 endif
113
114 ## theta(angular) ticks in deg
115 set (hax, "ttick", 0:30:330);
116 __update_polar_grid__(hax, []);
117
118 addlistener (hax, "ttick", @__update_polar_grid__);
93 119
94 unwind_protect_cleanup 120 unwind_protect_cleanup
95 if (! isempty (oldfig)) 121 if (! isempty (oldfig))
96 set (0, "currentfigure", oldfig); 122 set (0, "currentfigure", oldfig);
97 endif 123 endif
191 error ("polar: invalid data for plotting"); 217 error ("polar: invalid data for plotting");
192 endif 218 endif
193 219
194 endfunction 220 endfunction
195 221
222 function __update_text__ (hax, dummy)
223
224 text_handles = findobj(findobj (hax, "tag", "polar_grid"), "type", "text");
225 set (text_handles, "fontsize", get (hax, "fontsize"));
226
227 endfunction
228
229 function __update_lines__ (hax, dummy)
230
231 line_handles = findobj(findobj (hax, "tag", "polar_grid"), "type", "line");
232 set (line_handles, "linewidth", get (hax, "linewidth"));
233
234 endfunction
235
236 function __update_polar_grid__ (hax, dummy)
237
238 ## Delete polar_grid hggroup if already present
239 delete (findobj (hax, "tag", "polar_grid"));
240 h = hggroup (hax, "tag", "polar_grid");
241
242 rtick = get (hax, "rtick");
243 ttick = get (hax, "ttick");
244 lw = get (hax, "linewidth");
245 fs = get (hax, "fontsize");
246
247 ## The number of points used for a circle
248 circle_points = 50;
249 t = linspace(0, 2*pi, circle_points);
250 s = sin(t);
251 c = cos(t);
252
253 ## plot dotted circles
254 y = kron(s', rtick);
255 x = kron(c', rtick);
256 line (x(:,1:end-1), y(:,1:end-1), "linestyle", ":", "parent", h, "linewidth", lw);
257
258 ## the outer circle is drawn solid
259 line (x(:,end), y(:,end), "linestyle", "-", "parent", h, "linewidth", lw);
260
261 ## add radial labels
262 [x, y] = pol2cart (0.42 * pi, rtick);
263 text (x, y, num2cell(rtick), "verticalalignment", "bottom", "parent", h, "fontsize", fs);
264
265 ## add radial lines
266 rtick = get (hax, "rtick");
267
268 s = rtick(end) * sin (ttick * pi / 180);
269 c = rtick(end) * cos (ttick * pi / 180);
270 x = vertcat(zeros(1,numel(ttick)), c);
271 y = vertcat(zeros(1,numel(ttick)), s);
272 line (x, y, "linestyle", ":", "parent", h, "linewidth", lw)
273
274 ## add angular labels
275 tticklabel = num2cell (ttick);
276 tm = 1.08;
277 text (tm * c, tm * s, tticklabel, "horizontalalignment", "center", "parent", h, "fontsize", fs)
278
279 lim = 1.1 * rtick(end);
280 set (hax, "xlim", [-lim, lim], "ylim", [-lim, lim]);
281
282 endfunction
196 283
197 %!demo 284 %!demo
198 %! clf; 285 %! clf;
199 %! theta = linspace (0,2*pi,1000); 286 %! theta = linspace (0,2*pi,1000);
200 %! rho = sin (7*theta); 287 %! rho = sin (7*theta);
211 %!demo 298 %!demo
212 %! clf; 299 %! clf;
213 %! theta = linspace (0,8*pi,1000); 300 %! theta = linspace (0,8*pi,1000);
214 %! rho = sin (5/4*theta); 301 %! rho = sin (5/4*theta);
215 %! polar (theta, rho); 302 %! polar (theta, rho);
303 %! set (gca, "rtick", 0.2:0.2:1)
216 %! title ('polar() plot'); 304 %! title ('polar() plot');
217 305
306 %!demo
307 %! clf;
308 %! theta = linspace (0,2*pi,1000);
309 %! rho = sin (2*theta).*cos(2*theta);
310 %! polar (theta,rho,'--b')
311 %! set (gca, "fontsize", 12, "linewidth", 2);
312 %! title ('polar() plot with bigger font and thicker line');
313
314 %!demo
315 %! clf;
316 %! theta = linspace (0,2*pi,1000);
317 %! rho = sin (2*theta).*cos(2*theta);
318 %! polar (theta,rho,'--r')
319 %! set (gca, "rtick", 0.1:0.1:0.6, "ttick", 0:20:340)
320 %! title ('polar() plot with finer grid');