Mercurial > hg > octave-nkf
annotate scripts/plot/plotyy.m @ 8101:86955a1559c5
improve speed of cell2mat
* * *
trivial fix for previous cell2mat change
author | David Bateman <dbateman@free.fr> |
---|---|
date | Thu, 11 Sep 2008 16:57:12 -0400 |
parents | aead4b9d026b |
children | c066714ee5d5 |
rev | line source |
---|---|
7195 | 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); | |
7196 | 47 ## y2 = exp (x - 1); |
48 ## ax = plotyy (x, y1, x - 1, y2, @@plot, @@semilogy); | |
7195 | 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 | |
7665
aead4b9d026b
Fix axis handle treatment in plotyy
David Bateman <dbateman@free.fr>
parents:
7314
diff
changeset
|
58 ## Don't use __plt_get_axis_arg__ here as ax is a two vector for plotyy |
aead4b9d026b
Fix axis handle treatment in plotyy
David Bateman <dbateman@free.fr>
parents:
7314
diff
changeset
|
59 if (nargin > 1 && length (varargin{1}) == 2 && ishandle(varargin{1}(1)) |
aead4b9d026b
Fix axis handle treatment in plotyy
David Bateman <dbateman@free.fr>
parents:
7314
diff
changeset
|
60 && ishandle(varargin{1}(2)) && |
aead4b9d026b
Fix axis handle treatment in plotyy
David Bateman <dbateman@free.fr>
parents:
7314
diff
changeset
|
61 all (floor (varargin{1}) != varargin{1})) |
aead4b9d026b
Fix axis handle treatment in plotyy
David Bateman <dbateman@free.fr>
parents:
7314
diff
changeset
|
62 obj1 = get (varargin{1}(1)); |
aead4b9d026b
Fix axis handle treatment in plotyy
David Bateman <dbateman@free.fr>
parents:
7314
diff
changeset
|
63 obj2 = get (varargin{1}(2)); |
aead4b9d026b
Fix axis handle treatment in plotyy
David Bateman <dbateman@free.fr>
parents:
7314
diff
changeset
|
64 if (strcmp (obj1.type, "axes") || strcmp (obj2.type, "axes")) |
aead4b9d026b
Fix axis handle treatment in plotyy
David Bateman <dbateman@free.fr>
parents:
7314
diff
changeset
|
65 ax = [obj1, obj2]; |
aead4b9d026b
Fix axis handle treatment in plotyy
David Bateman <dbateman@free.fr>
parents:
7314
diff
changeset
|
66 varargin(1) = []; |
aead4b9d026b
Fix axis handle treatment in plotyy
David Bateman <dbateman@free.fr>
parents:
7314
diff
changeset
|
67 if (isempty (varargin)) |
aead4b9d026b
Fix axis handle treatment in plotyy
David Bateman <dbateman@free.fr>
parents:
7314
diff
changeset
|
68 varargin = {}; |
aead4b9d026b
Fix axis handle treatment in plotyy
David Bateman <dbateman@free.fr>
parents:
7314
diff
changeset
|
69 endif |
aead4b9d026b
Fix axis handle treatment in plotyy
David Bateman <dbateman@free.fr>
parents:
7314
diff
changeset
|
70 else |
aead4b9d026b
Fix axis handle treatment in plotyy
David Bateman <dbateman@free.fr>
parents:
7314
diff
changeset
|
71 error ("plotyy: expecting first argument to be axes handle"); |
aead4b9d026b
Fix axis handle treatment in plotyy
David Bateman <dbateman@free.fr>
parents:
7314
diff
changeset
|
72 endif |
aead4b9d026b
Fix axis handle treatment in plotyy
David Bateman <dbateman@free.fr>
parents:
7314
diff
changeset
|
73 else |
aead4b9d026b
Fix axis handle treatment in plotyy
David Bateman <dbateman@free.fr>
parents:
7314
diff
changeset
|
74 f = get (0, "currentfigure"); |
aead4b9d026b
Fix axis handle treatment in plotyy
David Bateman <dbateman@free.fr>
parents:
7314
diff
changeset
|
75 if (isempty (f)) |
aead4b9d026b
Fix axis handle treatment in plotyy
David Bateman <dbateman@free.fr>
parents:
7314
diff
changeset
|
76 ax(1) = axes (); |
aead4b9d026b
Fix axis handle treatment in plotyy
David Bateman <dbateman@free.fr>
parents:
7314
diff
changeset
|
77 ax(2) = axes (); |
aead4b9d026b
Fix axis handle treatment in plotyy
David Bateman <dbateman@free.fr>
parents:
7314
diff
changeset
|
78 else |
aead4b9d026b
Fix axis handle treatment in plotyy
David Bateman <dbateman@free.fr>
parents:
7314
diff
changeset
|
79 ax = get (f, "children"); |
8101
86955a1559c5
improve speed of cell2mat
David Bateman <dbateman@free.fr>
parents:
7665
diff
changeset
|
80 if (length (ax) > 2) |
86955a1559c5
improve speed of cell2mat
David Bateman <dbateman@free.fr>
parents:
7665
diff
changeset
|
81 for i = 3 : length (ax) |
86955a1559c5
improve speed of cell2mat
David Bateman <dbateman@free.fr>
parents:
7665
diff
changeset
|
82 delete (ax (i)); |
86955a1559c5
improve speed of cell2mat
David Bateman <dbateman@free.fr>
parents:
7665
diff
changeset
|
83 endfor |
86955a1559c5
improve speed of cell2mat
David Bateman <dbateman@free.fr>
parents:
7665
diff
changeset
|
84 ax = ax(1:2); |
86955a1559c5
improve speed of cell2mat
David Bateman <dbateman@free.fr>
parents:
7665
diff
changeset
|
85 elseif (length (ax) == 1) |
86955a1559c5
improve speed of cell2mat
David Bateman <dbateman@free.fr>
parents:
7665
diff
changeset
|
86 ax(2) = axes (); |
86955a1559c5
improve speed of cell2mat
David Bateman <dbateman@free.fr>
parents:
7665
diff
changeset
|
87 endif |
7665
aead4b9d026b
Fix axis handle treatment in plotyy
David Bateman <dbateman@free.fr>
parents:
7314
diff
changeset
|
88 endif |
aead4b9d026b
Fix axis handle treatment in plotyy
David Bateman <dbateman@free.fr>
parents:
7314
diff
changeset
|
89 if (nargin < 2) |
aead4b9d026b
Fix axis handle treatment in plotyy
David Bateman <dbateman@free.fr>
parents:
7314
diff
changeset
|
90 varargin = {}; |
aead4b9d026b
Fix axis handle treatment in plotyy
David Bateman <dbateman@free.fr>
parents:
7314
diff
changeset
|
91 endif |
aead4b9d026b
Fix axis handle treatment in plotyy
David Bateman <dbateman@free.fr>
parents:
7314
diff
changeset
|
92 endif |
7215 | 93 |
94 if (nargin < 4) | |
95 print_usage (); | |
96 endif | |
7216 | 97 |
7215 | 98 oldh = gca (); |
99 unwind_protect | |
100 [ax, h1, h2] = __plotyy__ (ax, varargin{:}); | |
101 unwind_protect_cleanup | |
102 axes (oldh); | |
103 end_unwind_protect | |
7195 | 104 |
105 if (nargout > 0) | |
106 Ax = ax; | |
107 H1 = h1; | |
108 H2 = h2; | |
109 endif | |
7196 | 110 |
7195 | 111 endfunction |
112 | |
113 function [ax, h1, h2] = __plotyy__ (ax, x1, y1, x2, y2, varargin) | |
114 if (nargin > 5) | |
115 fun1 = varargin{1}; | |
116 else | |
117 fun1 = @plot; | |
118 endif | |
119 if (nargin > 6) | |
120 fun2 = varargin{2}; | |
121 else | |
122 fun2 = fun1; | |
123 endif | |
124 | |
125 xlim = [min([x1(:); x2(:)]), max([x1(:); x2(:)])]; | |
126 | |
7665
aead4b9d026b
Fix axis handle treatment in plotyy
David Bateman <dbateman@free.fr>
parents:
7314
diff
changeset
|
127 axes (ax(1)); |
aead4b9d026b
Fix axis handle treatment in plotyy
David Bateman <dbateman@free.fr>
parents:
7314
diff
changeset
|
128 newplot (); |
7195 | 129 h1 = feval (fun1, x1, y1); |
7220 | 130 |
131 set (ax(1), "ycolor", getcolor (h1(1))); | |
7195 | 132 set (ax(1), "xlim", xlim); |
133 | |
134 cf = gcf (); | |
135 set (cf, "nextplot", "add"); | |
7665
aead4b9d026b
Fix axis handle treatment in plotyy
David Bateman <dbateman@free.fr>
parents:
7314
diff
changeset
|
136 axes (ax(2)); |
aead4b9d026b
Fix axis handle treatment in plotyy
David Bateman <dbateman@free.fr>
parents:
7314
diff
changeset
|
137 newplot (); |
aead4b9d026b
Fix axis handle treatment in plotyy
David Bateman <dbateman@free.fr>
parents:
7314
diff
changeset
|
138 |
7195 | 139 colors = get (ax(1), "colororder"); |
140 set (ax(2), "colororder", [colors(2:end,:); colors(1,:)]); | |
141 | |
142 h2 = feval (fun2, x2, y2); | |
7206 | 143 set (ax(2), "yaxislocation", "right"); |
7220 | 144 set (ax(2), "ycolor", getcolor (h2(1))); |
7240 | 145 set (ax(2), "position", get (ax(1), "position")); |
7195 | 146 set (ax(2), "xlim", xlim); |
7240 | 147 set (ax(2), "color", "none"); |
8101
86955a1559c5
improve speed of cell2mat
David Bateman <dbateman@free.fr>
parents:
7665
diff
changeset
|
148 |
86955a1559c5
improve speed of cell2mat
David Bateman <dbateman@free.fr>
parents:
7665
diff
changeset
|
149 ## Add invisible text objects that when destroyed, |
86955a1559c5
improve speed of cell2mat
David Bateman <dbateman@free.fr>
parents:
7665
diff
changeset
|
150 ## also remove the other axis |
86955a1559c5
improve speed of cell2mat
David Bateman <dbateman@free.fr>
parents:
7665
diff
changeset
|
151 t1 = text (0, 0, "", "parent", ax(1), "tag", "plotyy", |
86955a1559c5
improve speed of cell2mat
David Bateman <dbateman@free.fr>
parents:
7665
diff
changeset
|
152 "handlevisibility", "off", "visible", "off", |
86955a1559c5
improve speed of cell2mat
David Bateman <dbateman@free.fr>
parents:
7665
diff
changeset
|
153 "xliminclude", "off", "yliminclude", "off"); |
86955a1559c5
improve speed of cell2mat
David Bateman <dbateman@free.fr>
parents:
7665
diff
changeset
|
154 t2 = text (0, 0, "", "parent", ax(2), "tag", "plotyy", |
86955a1559c5
improve speed of cell2mat
David Bateman <dbateman@free.fr>
parents:
7665
diff
changeset
|
155 "handlevisibility", "off", "visible", "off", |
86955a1559c5
improve speed of cell2mat
David Bateman <dbateman@free.fr>
parents:
7665
diff
changeset
|
156 "xliminclude", "off", "yliminclude", "off"); |
86955a1559c5
improve speed of cell2mat
David Bateman <dbateman@free.fr>
parents:
7665
diff
changeset
|
157 |
86955a1559c5
improve speed of cell2mat
David Bateman <dbateman@free.fr>
parents:
7665
diff
changeset
|
158 set (t1, "deletefcn", {@deleteplotyy, ax(2), t2}); |
86955a1559c5
improve speed of cell2mat
David Bateman <dbateman@free.fr>
parents:
7665
diff
changeset
|
159 set (t2, "deletefcn", {@deleteplotyy, ax(1), t1}); |
86955a1559c5
improve speed of cell2mat
David Bateman <dbateman@free.fr>
parents:
7665
diff
changeset
|
160 |
86955a1559c5
improve speed of cell2mat
David Bateman <dbateman@free.fr>
parents:
7665
diff
changeset
|
161 addlistener (ax(1), "position", {@update_position, ax(2)}); |
86955a1559c5
improve speed of cell2mat
David Bateman <dbateman@free.fr>
parents:
7665
diff
changeset
|
162 addlistener (ax(2), "position", {@update_position, ax(1)}); |
86955a1559c5
improve speed of cell2mat
David Bateman <dbateman@free.fr>
parents:
7665
diff
changeset
|
163 addlistener (ax(1), "view", {@update_position, ax(2)}); |
86955a1559c5
improve speed of cell2mat
David Bateman <dbateman@free.fr>
parents:
7665
diff
changeset
|
164 addlistener (ax(2), "view", {@update_position, ax(1)}); |
86955a1559c5
improve speed of cell2mat
David Bateman <dbateman@free.fr>
parents:
7665
diff
changeset
|
165 |
86955a1559c5
improve speed of cell2mat
David Bateman <dbateman@free.fr>
parents:
7665
diff
changeset
|
166 ## Tag the plotyy axes, so we can use that information |
86955a1559c5
improve speed of cell2mat
David Bateman <dbateman@free.fr>
parents:
7665
diff
changeset
|
167 ## not to mirror the y axis tick marks |
86955a1559c5
improve speed of cell2mat
David Bateman <dbateman@free.fr>
parents:
7665
diff
changeset
|
168 set (ax, "tag", "plotyy") |
86955a1559c5
improve speed of cell2mat
David Bateman <dbateman@free.fr>
parents:
7665
diff
changeset
|
169 |
86955a1559c5
improve speed of cell2mat
David Bateman <dbateman@free.fr>
parents:
7665
diff
changeset
|
170 endfunction |
86955a1559c5
improve speed of cell2mat
David Bateman <dbateman@free.fr>
parents:
7665
diff
changeset
|
171 |
86955a1559c5
improve speed of cell2mat
David Bateman <dbateman@free.fr>
parents:
7665
diff
changeset
|
172 %!demo |
86955a1559c5
improve speed of cell2mat
David Bateman <dbateman@free.fr>
parents:
7665
diff
changeset
|
173 %! x = 0:0.1:2*pi; |
86955a1559c5
improve speed of cell2mat
David Bateman <dbateman@free.fr>
parents:
7665
diff
changeset
|
174 %! y1 = sin (x); |
86955a1559c5
improve speed of cell2mat
David Bateman <dbateman@free.fr>
parents:
7665
diff
changeset
|
175 %! y2 = exp (x - 1); |
86955a1559c5
improve speed of cell2mat
David Bateman <dbateman@free.fr>
parents:
7665
diff
changeset
|
176 %! ax = plotyy (x, y1, x - 1, y2, @plot, @semilogy); |
86955a1559c5
improve speed of cell2mat
David Bateman <dbateman@free.fr>
parents:
7665
diff
changeset
|
177 %! xlabel ("X"); |
86955a1559c5
improve speed of cell2mat
David Bateman <dbateman@free.fr>
parents:
7665
diff
changeset
|
178 %! ylabel (ax(1), "Axis 1"); |
86955a1559c5
improve speed of cell2mat
David Bateman <dbateman@free.fr>
parents:
7665
diff
changeset
|
179 %! ylabel (ax(2), "Axis 2"); |
86955a1559c5
improve speed of cell2mat
David Bateman <dbateman@free.fr>
parents:
7665
diff
changeset
|
180 |
86955a1559c5
improve speed of cell2mat
David Bateman <dbateman@free.fr>
parents:
7665
diff
changeset
|
181 function deleteplotyy (h, d, ax2, t2) |
86955a1559c5
improve speed of cell2mat
David Bateman <dbateman@free.fr>
parents:
7665
diff
changeset
|
182 if (ishandle (ax2) && strcmp (get (ax2, "type"), "axes") && |
86955a1559c5
improve speed of cell2mat
David Bateman <dbateman@free.fr>
parents:
7665
diff
changeset
|
183 (isempty (gcbf()) || strcmp (get (gcbf(), "beingdeleted"),"off")) && |
86955a1559c5
improve speed of cell2mat
David Bateman <dbateman@free.fr>
parents:
7665
diff
changeset
|
184 strcmp (get (ax2, "beingdeleted"), "off")) |
86955a1559c5
improve speed of cell2mat
David Bateman <dbateman@free.fr>
parents:
7665
diff
changeset
|
185 set (t2, "deletefcn", []); |
86955a1559c5
improve speed of cell2mat
David Bateman <dbateman@free.fr>
parents:
7665
diff
changeset
|
186 delete (ax2); |
86955a1559c5
improve speed of cell2mat
David Bateman <dbateman@free.fr>
parents:
7665
diff
changeset
|
187 endif |
86955a1559c5
improve speed of cell2mat
David Bateman <dbateman@free.fr>
parents:
7665
diff
changeset
|
188 endfunction |
86955a1559c5
improve speed of cell2mat
David Bateman <dbateman@free.fr>
parents:
7665
diff
changeset
|
189 |
86955a1559c5
improve speed of cell2mat
David Bateman <dbateman@free.fr>
parents:
7665
diff
changeset
|
190 function update_position (h, d, ax2) |
86955a1559c5
improve speed of cell2mat
David Bateman <dbateman@free.fr>
parents:
7665
diff
changeset
|
191 persistent recursion = false; |
86955a1559c5
improve speed of cell2mat
David Bateman <dbateman@free.fr>
parents:
7665
diff
changeset
|
192 |
86955a1559c5
improve speed of cell2mat
David Bateman <dbateman@free.fr>
parents:
7665
diff
changeset
|
193 ## Don't allow recursion |
86955a1559c5
improve speed of cell2mat
David Bateman <dbateman@free.fr>
parents:
7665
diff
changeset
|
194 if (! recursion) |
86955a1559c5
improve speed of cell2mat
David Bateman <dbateman@free.fr>
parents:
7665
diff
changeset
|
195 unwind_protect |
86955a1559c5
improve speed of cell2mat
David Bateman <dbateman@free.fr>
parents:
7665
diff
changeset
|
196 recursion = true; |
86955a1559c5
improve speed of cell2mat
David Bateman <dbateman@free.fr>
parents:
7665
diff
changeset
|
197 position = get (h, "position"); |
86955a1559c5
improve speed of cell2mat
David Bateman <dbateman@free.fr>
parents:
7665
diff
changeset
|
198 view = get (h, "view"); |
86955a1559c5
improve speed of cell2mat
David Bateman <dbateman@free.fr>
parents:
7665
diff
changeset
|
199 oldposition = get (ax2, "position"); |
86955a1559c5
improve speed of cell2mat
David Bateman <dbateman@free.fr>
parents:
7665
diff
changeset
|
200 oldview = get (ax2, "view"); |
86955a1559c5
improve speed of cell2mat
David Bateman <dbateman@free.fr>
parents:
7665
diff
changeset
|
201 if (! (isequal (position, oldposition) && isequal (view, oldview))) |
86955a1559c5
improve speed of cell2mat
David Bateman <dbateman@free.fr>
parents:
7665
diff
changeset
|
202 set (ax2, "position", position, "view", view); |
86955a1559c5
improve speed of cell2mat
David Bateman <dbateman@free.fr>
parents:
7665
diff
changeset
|
203 endif |
86955a1559c5
improve speed of cell2mat
David Bateman <dbateman@free.fr>
parents:
7665
diff
changeset
|
204 unwind_protect_cleanup |
86955a1559c5
improve speed of cell2mat
David Bateman <dbateman@free.fr>
parents:
7665
diff
changeset
|
205 recursion = false; |
86955a1559c5
improve speed of cell2mat
David Bateman <dbateman@free.fr>
parents:
7665
diff
changeset
|
206 end_unwind_protect |
86955a1559c5
improve speed of cell2mat
David Bateman <dbateman@free.fr>
parents:
7665
diff
changeset
|
207 endif |
7195 | 208 endfunction |
7220 | 209 |
210 function color = getcolor (ax) | |
211 obj = get (ax); | |
212 if (isfield (obj, "color")) | |
213 color = obj.color; | |
214 elseif (isfield (obj, "facecolor") && ! ischar (obj.facecolor)) | |
215 color = obj.facecolor; | |
216 elseif (isfield (obj, "edgecolor") && ! ischar (obj.edgecolor)) | |
217 color = obj.edgecolor; | |
218 else | |
219 color = [0, 0, 0]; | |
220 endif | |
221 endfunction | |
7245 | 222 |