comparison scripts/plot/plotyy.m @ 7195:97d2da8ed746

[project @ 2007-11-26 23:13:40 by dbateman]
author dbateman
date Mon, 26 Nov 2007 23:13:40 +0000
parents
children 9092375e3936
comparison
equal deleted inserted replaced
7194:a927a2871a93 7195:97d2da8ed746
1 ## Copyright (C) 2007 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 if (isscalar (varargin{1}) && ishandle (varargin{1}))
59 ax = varargin {1};
60 if (! strcmp (get (ax, "type"), "axes"))
61 error ("plotyy: expecting first argument to be an axes object");
62 endif
63 if (nargin < 5)
64 print_usage();
65 endif
66 oldh = gca ();
67 unwind_protect
68 axes (ax);
69 newplot ();
70 [ax, h1, h2] = __plotyy__ (ax, varargin{2:end});
71 unwind_protect_cleanup
72 axes (oldh);
73 end_unwind_protect
74 else
75 if (nargin < 4)
76 print_usage();
77 endif
78 newplot ();
79 [ax, h1, h2] = __plotyy__ (gca (), varargin{:});
80 endif
81
82 if (nargout > 0)
83 Ax = ax;
84 H1 = h1;
85 H2 = h2;
86 endif
87 endfunction
88
89 function [ax, h1, h2] = __plotyy__ (ax, x1, y1, x2, y2, varargin)
90 if (nargin > 5)
91 fun1 = varargin{1};
92 else
93 fun1 = @plot;
94 endif
95 if (nargin > 6)
96 fun2 = varargin{2};
97 else
98 fun2 = fun1;
99 endif
100
101 xlim = [min([x1(:); x2(:)]), max([x1(:); x2(:)])];
102
103 h1 = feval (fun1, x1, y1);
104 set (ax(1), "ycolor", get (h1, "color"));
105 set (ax(1), "position", get (ax(1), "outerposition"));
106 set (ax(1), "xlim", xlim);
107
108 cf = gcf ();
109 set (cf, "nextplot", "add");
110 ax(2) = axes ("position", get (ax(1), "position"));
111 colors = get (ax(1), "colororder");
112 set (ax(2), "colororder", [colors(2:end,:); colors(1,:)]);
113
114 h2 = feval (fun2, x2, y2);
115 set (ax(2), "ycolor", get (h2, "color"));
116 set (ax(2), "xlim", xlim);
117 set (ax(2), "yaxislocation", "right");
118 endfunction