plotyy.m: Fix compatibility with subplot.
1 ## Copyright (C) 2007, 2008, 2009 David Bateman
3 ## This file is part of Octave.
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.
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.
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/>.
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}
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.
35 ## The function to use for each of the plots can be independently defined
36 ## with @var{fun1} and @var{fun2}.
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.
48 ## ax = plotyy (x, y1, x - 1, y2, @@plot, @@semilogy);
50 ## ylabel (ax(1), "Axis 1");
51 ## ylabel (ax(2), "Axis 2");
56 function [Ax, H1, H2] = plotyy (varargin)
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"))
67 if (isempty (varargin))
71 error ("plotyy: expecting first argument to be axes handle");
74 f = get (0, "currentfigure");
78 ca = get (f, "currentaxes");
81 elseif (strcmp (get (ca, "tag"), "plotyy"));
82 ax = get (ca, "__plotyy_axes__");
87 for i = 3 : length (ax)
91 elseif (length (ax) == 1)
108 [ax, h1, h2] = __plotyy__ (ax, varargin{:});
109 unwind_protect_cleanup
110 ## Only change back to the old axis if we didn't delete it
111 if (ishandle(oldh) && strcmp (get (oldh, "type"), "axes"))
124 function [ax, h1, h2] = __plotyy__ (ax, x1, y1, x2, y2, varargin)
136 xlim = [min([x1(:); x2(:)]), max([x1(:); x2(:)])];
138 if (ishandle(ax(1)) && strcmp (get (ax(1), "type"), "axes"))
144 h1 = feval (fun1, x1, y1);
146 set (ax(1), "ycolor", getcolor (h1(1)));
147 set (ax(1), "xlim", xlim);
150 set (cf, "nextplot", "add");
152 if (ishandle(ax(2)) && strcmp (get (ax(2), "type"), "axes"))
159 colors = get (ax(1), "colororder");
160 set (ax(2), "colororder", [colors(2:end,:); colors(1,:)]);
162 h2 = feval (fun2, x2, y2);
163 set (ax(2), "yaxislocation", "right");
164 set (ax(2), "ycolor", getcolor (h2(1)));
165 set (ax(2), "position", get (ax(1), "position"));
166 set (ax(2), "xlim", xlim);
167 set (ax(2), "color", "none");
169 ## Add invisible text objects that when destroyed,
170 ## also remove the other axis
171 t1 = text (0, 0, "", "parent", ax(1), "tag", "plotyy",
172 "handlevisibility", "off", "visible", "off",
173 "xliminclude", "off", "yliminclude", "off");
174 t2 = text (0, 0, "", "parent", ax(2), "tag", "plotyy",
175 "handlevisibility", "off", "visible", "off",
176 "xliminclude", "off", "yliminclude", "off");
178 set (t1, "deletefcn", {@deleteplotyy, ax(2), t2});
179 set (t2, "deletefcn", {@deleteplotyy, ax(1), t1});
181 addlistener (ax(1), "position", {@update_position, ax(2)});
182 addlistener (ax(2), "position", {@update_position, ax(1)});
183 addlistener (ax(1), "view", {@update_position, ax(2)});
184 addlistener (ax(2), "view", {@update_position, ax(1)});
185 addlistener (ax(1), "dataaspectratio", {@update_position, ax(2)});
186 addlistener (ax(2), "dataaspectratio", {@update_position, ax(1)});
188 ## Tag the plotyy axes, so we can use that information
189 ## not to mirror the y axis tick marks
190 set (ax, "tag", "plotyy")
192 ## Store the axes handles for the sister axes.
193 addproperty ("__plotyy_axes__", ax(1), "data", ax);
194 addproperty ("__plotyy_axes__", ax(2), "data", ax);
203 %! ax = plotyy (x, y1, x - 1, y2, @plot, @semilogy);
205 %! ylabel (ax(1), "Axis 1");
206 %! ylabel (ax(2), "Axis 2");
210 %! x = linspace (-1, 1, 201);
212 %! plotyy (x, sin(pi*x), x, 10*cos(pi*x))
216 %! contour (peaks (25))
218 %! plotyy (x, 10*sin(2*pi*x), x, cos(2*pi*x))
221 function deleteplotyy (h, d, ax2, t2)
222 if (ishandle (ax2) && strcmp (get (ax2, "type"), "axes") &&
223 (isempty (gcbf()) || strcmp (get (gcbf(), "beingdeleted"),"off")) &&
224 strcmp (get (ax2, "beingdeleted"), "off"))
225 set (t2, "deletefcn", []);
230 function update_position (h, d, ax2)
231 persistent recursion = false;
233 ## Don't allow recursion
237 position = get (h, "position");
238 view = get (h, "view");
239 dataaspectratio = get (h, "dataaspectratio");
240 oldposition = get (ax2, "position");
241 oldview = get (ax2, "view");
242 olddataaspectratio = get (ax2, "dataaspectratio");
243 if (! (isequal (position, oldposition)
244 && isequal (view, oldview)
245 && isequal (dataaspectratio, olddataaspectratio)))
246 set (ax2, "position", position,
248 "dataaspectratio", dataaspectratio);
250 unwind_protect_cleanup
256 function color = getcolor (ax)
258 if (isfield (obj, "color"))
260 elseif (isfield (obj, "facecolor") && ! ischar (obj.facecolor))
261 color = obj.facecolor;
262 elseif (isfield (obj, "edgecolor") && ! ischar (obj.edgecolor))
263 color = obj.edgecolor;