scripts/plot/plotyy.m
author Ben Abbott <bpabbott@mac.com>
Tue Jun 16 06:43:56 2009 +0200 (2009-06-16)
changeset 9336 9f24c50b6fa0
parent 9040 dbd0c77e575e
child 9337 fee95bb4ee94
permissions -rw-r--r--
plotyy.m: Correct behavior when there is no currentfigure.
     1 ## Copyright (C) 2007, 2008, 2009 David Bateman
     2 ##
     3 ## This file is part of Octave.
     4 ##
     5 ## Octave is free software; you can redistribute it and/or modify it
     6 ## under the terms of the GNU General Public License as published by
     7 ## the Free Software Foundation; either version 3 of the License, or (at
     8 ## your option) any later version.
     9 ##
    10 ## Octave is distributed in the hope that it will be useful, but
    11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
    12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    13 ## General Public License for more details.
    14 ##
    15 ## You should have received a copy of the GNU General Public License
    16 ## along with Octave; see the file COPYING.  If not, see
    17 ## <http://www.gnu.org/licenses/>.
    18 
    19 ## -*- texinfo -*-
    20 ## @deftypefn {Function File} {} plotyy (@var{x1}, @var{y1}, @var{x2}, @var{y2})
    21 ## @deftypefnx {Function File} {} plotyy (@dots{}, @var{fun})
    22 ## @deftypefnx {Function File} {} plotyy (@dots{}, @var{fun1}, @var{fun2})
    23 ## @deftypefnx {Function File} {} plotyy (@var{h}, @dots{})
    24 ## @deftypefnx {Function File} {[@var{ax}, @var{h1}, @var{h2}] =} plotyy (@dots{})
    25 ## Plots two sets of data with independent y-axes.  The arguments @var{x1} and
    26 ## @var{y1} define the arguments for the first plot and @var{x1} and @var{y2}
    27 ## for the second. 
    28 ##
    29 ## By default the arguments are evaluated with 
    30 ## @code{feval (@@plot, @var{x}, @var{y})}.  However the type of plot can be
    31 ## modified with the @var{fun} argument, in which case the plots are
    32 ## generated by @code{feval (@var{fun}, @var{x}, @var{y})}.  @var{fun} can be 
    33 ## a function handle, an inline function or a string of a function name.
    34 ##
    35 ## The function to use for each of the plots can be independently defined 
    36 ## with @var{fun1} and @var{fun2}.
    37 ##
    38 ## If given, @var{h} defines the principal axis in which to plot the @var{x1}
    39 ## and @var{y1} data.  The return value @var{ax} is a two element vector with
    40 ## the axis handles of the two plots.  @var{h1} and @var{h2} are handles to
    41 ## the objects generated by the plot commands.
    42 ##
    43 ## @example
    44 ## @group
    45 ## x = 0:0.1:2*pi; 
    46 ## y1 = sin (x);
    47 ## y2 = exp (x - 1);
    48 ## ax = plotyy (x, y1, x - 1, y2, @@plot, @@semilogy);
    49 ## xlabel ("X");
    50 ## ylabel (ax(1), "Axis 1");
    51 ## ylabel (ax(2), "Axis 2");
    52 ## @end group
    53 ## @end example
    54 ## @end deftypefn
    55 
    56 function [Ax, H1, H2] = plotyy (varargin)
    57 
    58   ## Don't use __plt_get_axis_arg__ here as ax is a two vector for plotyy
    59   if (nargin > 1 && length (varargin{1}) == 2 && ishandle(varargin{1}(1)) 
    60       &&  ishandle(varargin{1}(2)) && 
    61       all (floor (varargin{1}) != varargin{1}))
    62     obj1 = get (varargin{1}(1));
    63     obj2 = get (varargin{1}(2));
    64     if (strcmp (obj1.type, "axes") || strcmp (obj2.type, "axes"))
    65       ax = [obj1, obj2];
    66       varargin(1) = [];
    67       if (isempty (varargin))
    68 	varargin = {};
    69       endif
    70     else
    71       error ("plotyy: expecting first argument to be axes handle");
    72     endif
    73   else
    74     f = get (0, "currentfigure");
    75     if (isempty (f))
    76       f = figure ();
    77     endif
    78     ax = get (f, "children");
    79     if (length (ax) > 2)
    80       for i = 3 : length (ax)
    81         delete (ax (i));
    82       endfor
    83       ax = ax(1:2);
    84     elseif (length (ax) == 1)
    85       ax(2) = axes ();
    86     elseif (isempty (ax))
    87       ax(1) = axes ();
    88       ax(2) = axes ();
    89     endif
    90     if (nargin < 2)
    91       varargin = {};
    92     endif
    93   endif 
    94 
    95   if (nargin < 4)
    96     print_usage ();
    97   endif
    98 
    99   oldh = gca ();
   100   unwind_protect
   101     [ax, h1, h2] = __plotyy__ (ax, varargin{:});
   102   unwind_protect_cleanup
   103     ## Only change back to the old axis if we didn't delete it
   104     if (ishandle(oldh) && strcmp (get (oldh, "type"), "axes"))
   105       axes (oldh);
   106     endif
   107   end_unwind_protect
   108 
   109   if (nargout > 0)
   110     Ax = ax;
   111     H1 = h1;
   112     H2 = h2;
   113   endif
   114 
   115 endfunction
   116 
   117 function [ax, h1, h2] = __plotyy__ (ax, x1, y1, x2, y2, varargin)
   118   if (nargin > 5)
   119     fun1 = varargin{1};
   120   else
   121     fun1 = @plot;
   122   endif
   123   if (nargin > 6)
   124     fun2 = varargin{2};
   125   else
   126     fun2 = fun1;
   127   endif
   128 
   129   xlim = [min([x1(:); x2(:)]), max([x1(:); x2(:)])];
   130 
   131   if (ishandle(ax(1)) && strcmp (get (ax(1), "type"), "axes"))
   132     axes (ax(1));
   133   else
   134     ax(1) = axes ();
   135   endif
   136   newplot ();
   137   h1 = feval (fun1, x1, y1);
   138 
   139   set (ax(1), "ycolor", getcolor (h1(1)));
   140   set (ax(1), "xlim", xlim);
   141 
   142   cf = gcf ();
   143   set (cf, "nextplot", "add");
   144 
   145   if (ishandle(ax(2)) && strcmp (get (ax(2), "type"), "axes"))
   146     axes (ax(2));
   147   else
   148     ax(2) = axes ();
   149   endif
   150   newplot ();
   151 
   152   colors = get (ax(1), "colororder");
   153   set (ax(2), "colororder", [colors(2:end,:); colors(1,:)]);
   154 
   155   h2 = feval (fun2, x2, y2);
   156   set (ax(2), "yaxislocation", "right");
   157   set (ax(2), "ycolor", getcolor (h2(1)));
   158   set (ax(2), "position", get (ax(1), "position"));
   159   set (ax(2), "xlim", xlim);
   160   set (ax(2), "color", "none");
   161 
   162   ## Add invisible text objects that when destroyed, 
   163   ## also remove the other axis
   164   t1 = text (0, 0, "", "parent", ax(1), "tag", "plotyy", 
   165 	     "handlevisibility", "off", "visible", "off",
   166 	     "xliminclude", "off", "yliminclude", "off");
   167   t2 = text (0, 0, "", "parent", ax(2), "tag", "plotyy", 
   168 	     "handlevisibility", "off", "visible", "off",
   169 	     "xliminclude", "off", "yliminclude", "off");
   170 
   171   set (t1, "deletefcn", {@deleteplotyy, ax(2), t2});
   172   set (t2, "deletefcn", {@deleteplotyy, ax(1), t1});
   173 
   174   addlistener (ax(1), "position", {@update_position, ax(2)});
   175   addlistener (ax(2), "position", {@update_position, ax(1)});
   176   addlistener (ax(1), "view", {@update_position, ax(2)});
   177   addlistener (ax(2), "view", {@update_position, ax(1)});
   178 
   179   ## Tag the plotyy axes, so we can use that information
   180   ## not to mirror the y axis tick marks
   181   set (ax, "tag", "plotyy")
   182 
   183 endfunction
   184 
   185 %!demo
   186 %! clf
   187 %! x = 0:0.1:2*pi; 
   188 %! y1 = sin (x);
   189 %! y2 = exp (x - 1);
   190 %! ax = plotyy (x, y1, x - 1, y2, @plot, @semilogy);
   191 %! xlabel ("X");
   192 %! ylabel (ax(1), "Axis 1");
   193 %! ylabel (ax(2), "Axis 2");
   194 
   195 function deleteplotyy (h, d, ax2, t2)
   196   if (ishandle (ax2) && strcmp (get (ax2, "type"), "axes") && 
   197       (isempty (gcbf()) || strcmp (get (gcbf(), "beingdeleted"),"off")) &&
   198       strcmp (get (ax2, "beingdeleted"), "off"))
   199     set (t2, "deletefcn", []);
   200     delete (ax2);
   201   endif
   202 endfunction
   203 
   204 function update_position (h, d, ax2)
   205   persistent recursion = false;
   206 
   207   ## Don't allow recursion
   208   if (! recursion)
   209     unwind_protect
   210       recursion = true;
   211       position = get (h, "position");
   212       view = get (h, "view");
   213       oldposition = get (ax2, "position");
   214       oldview = get (ax2, "view");
   215       if (! (isequal (position, oldposition) && isequal (view, oldview)))
   216 	set (ax2, "position", position, "view", view);
   217       endif
   218     unwind_protect_cleanup
   219       recursion = false;
   220     end_unwind_protect
   221   endif  
   222 endfunction
   223 
   224 function color = getcolor (ax)
   225   obj = get (ax);
   226   if (isfield (obj, "color"))
   227     color = obj.color;
   228   elseif (isfield (obj, "facecolor") && ! ischar (obj.facecolor))
   229     color = obj.facecolor;
   230   elseif (isfield (obj, "edgecolor") && !  ischar (obj.edgecolor))
   231     color = obj.edgecolor;
   232   else
   233     color = [0, 0, 0];
   234   endif
   235 endfunction
   236