changeset 18745: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 be0978e94806
children 56bff71de2ca
files scripts/plot/draw/polar.m
diffstat 1 files changed, 105 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/plot/draw/polar.m	Fri Mar 07 09:10:46 2014 +0100
+++ b/scripts/plot/draw/polar.m	Mon Apr 28 19:58:36 2014 +0200
@@ -87,10 +87,36 @@
       print_usage ();
     endif
 
-    set (hax, "xlim", [-maxr, maxr], "ylim", [-maxr, maxr],
-              "xaxislocation", "zero", "yaxislocation", "zero",
+    set (hax, "xaxislocation", "zero", "yaxislocation", "zero",
               "plotboxaspectratio", [1, 1, 1]);
 
+    ## Hide standard axes
+    set(hax, "visible", "off");
+
+    if (!isprop (hax, "rtick"))
+      addproperty ("rtick", hax, "data");
+    endif
+
+    ## calculate r(ho)tick from xtick
+    xtick = get (hax, "xtick");
+    rtick = xtick(find (xtick > 0, 1):find (xtick >= maxr, 1));
+    set (hax, "rtick", rtick);
+
+    addlistener (hax, "rtick", @__update_polar_grid__);
+    addlistener (hax, "fontsize", @__update_text__);
+    addlistener (hax, "linewidth", @__update_lines__);
+
+    ## add t(heta)tick
+    if (!isprop (hax, "ttick"))
+      addproperty ("ttick", hax, "data");
+    endif
+
+    ## theta(angular) ticks in deg
+    set (hax, "ttick", 0:30:330);
+    __update_polar_grid__(hax, []);
+
+    addlistener (hax, "ttick", @__update_polar_grid__);
+
   unwind_protect_cleanup
     if (! isempty (oldfig))
       set (0, "currentfigure", oldfig);
@@ -193,6 +219,67 @@
 
 endfunction
 
+function __update_text__ (hax, dummy)
+
+  text_handles = findobj(findobj (hax, "tag", "polar_grid"), "type", "text");
+  set (text_handles, "fontsize", get (hax, "fontsize"));
+
+endfunction
+
+function __update_lines__ (hax, dummy)
+
+  line_handles = findobj(findobj (hax, "tag", "polar_grid"), "type", "line");
+  set (line_handles, "linewidth", get (hax, "linewidth"));
+
+endfunction
+
+function __update_polar_grid__ (hax, dummy)
+
+  ## Delete polar_grid hggroup if already present
+  delete (findobj (hax, "tag", "polar_grid"));
+  h = hggroup (hax, "tag", "polar_grid");
+
+  rtick = get (hax, "rtick");
+  ttick = get (hax, "ttick");
+  lw = get (hax, "linewidth");
+  fs = get (hax, "fontsize");
+
+  ## The number of points used for a circle
+  circle_points = 50;
+  t = linspace(0, 2*pi, circle_points);
+  s = sin(t);
+  c = cos(t);
+
+  ## plot dotted circles
+  y = kron(s', rtick);
+  x = kron(c', rtick);
+  line (x(:,1:end-1), y(:,1:end-1), "linestyle", ":", "parent", h, "linewidth", lw);
+
+  ## the outer circle is drawn solid
+  line (x(:,end), y(:,end), "linestyle", "-", "parent", h, "linewidth", lw);
+
+  ## add radial labels
+  [x, y] = pol2cart (0.42 * pi, rtick);
+  text (x, y, num2cell(rtick), "verticalalignment", "bottom", "parent", h, "fontsize", fs);
+
+  ## add radial lines
+  rtick = get (hax, "rtick");
+
+  s = rtick(end) * sin (ttick * pi / 180);
+  c = rtick(end) * cos (ttick * pi / 180);
+  x = vertcat(zeros(1,numel(ttick)), c);
+  y = vertcat(zeros(1,numel(ttick)), s);
+  line (x, y, "linestyle", ":", "parent", h, "linewidth", lw)
+
+  ## add angular labels
+  tticklabel = num2cell (ttick);
+  tm = 1.08;
+  text (tm * c, tm * s, tticklabel, "horizontalalignment", "center", "parent", h, "fontsize", fs)
+
+  lim = 1.1 * rtick(end);
+  set (hax, "xlim", [-lim, lim], "ylim", [-lim, lim]);
+
+endfunction
 
 %!demo
 %! clf;
@@ -213,5 +300,21 @@
 %! theta = linspace (0,8*pi,1000);
 %! rho = sin (5/4*theta);
 %! polar (theta, rho);
+%! set (gca, "rtick", 0.2:0.2:1)
 %! title ('polar() plot');
 
+%!demo
+%! clf;
+%! theta = linspace (0,2*pi,1000);
+%! rho = sin (2*theta).*cos(2*theta);
+%! polar (theta,rho,'--b')
+%! set (gca, "fontsize", 12, "linewidth", 2);
+%! title ('polar() plot with bigger font and thicker line');
+
+%!demo
+%! clf;
+%! theta = linspace (0,2*pi,1000);
+%! rho = sin (2*theta).*cos(2*theta);
+%! polar (theta,rho,'--r')
+%! set (gca, "rtick", 0.1:0.1:0.6, "ttick", 0:20:340)
+%! title ('polar() plot with finer grid');