Mercurial > hg > octave-lyh
comparison scripts/plot/plotyy.m @ 8102:c066714ee5d5
undo previous change
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Thu, 11 Sep 2008 17:01:46 -0400 |
parents | 86955a1559c5 |
children | f6ca8ff51818 |
comparison
equal
deleted
inserted
replaced
8101:86955a1559c5 | 8102:c066714ee5d5 |
---|---|
75 if (isempty (f)) | 75 if (isempty (f)) |
76 ax(1) = axes (); | 76 ax(1) = axes (); |
77 ax(2) = axes (); | 77 ax(2) = axes (); |
78 else | 78 else |
79 ax = get (f, "children"); | 79 ax = get (f, "children"); |
80 if (length (ax) > 2) | 80 for i = 3 : length (ax) |
81 for i = 3 : length (ax) | 81 delete (ax (i)); |
82 delete (ax (i)); | 82 endfor |
83 endfor | 83 ax = ax(1:2); |
84 ax = ax(1:2); | |
85 elseif (length (ax) == 1) | |
86 ax(2) = axes (); | |
87 endif | |
88 endif | 84 endif |
89 if (nargin < 2) | 85 if (nargin < 2) |
90 varargin = {}; | 86 varargin = {}; |
91 endif | 87 endif |
92 endif | 88 endif |
127 axes (ax(1)); | 123 axes (ax(1)); |
128 newplot (); | 124 newplot (); |
129 h1 = feval (fun1, x1, y1); | 125 h1 = feval (fun1, x1, y1); |
130 | 126 |
131 set (ax(1), "ycolor", getcolor (h1(1))); | 127 set (ax(1), "ycolor", getcolor (h1(1))); |
128 set (ax(1), "position", [0.11 0.13 0.78 0.73]); | |
129 set (ax(1), "activepositionproperty", "position"); | |
132 set (ax(1), "xlim", xlim); | 130 set (ax(1), "xlim", xlim); |
133 | 131 |
134 cf = gcf (); | 132 cf = gcf (); |
135 set (cf, "nextplot", "add"); | 133 set (cf, "nextplot", "add"); |
136 axes (ax(2)); | 134 axes (ax(2)); |
141 | 139 |
142 h2 = feval (fun2, x2, y2); | 140 h2 = feval (fun2, x2, y2); |
143 set (ax(2), "yaxislocation", "right"); | 141 set (ax(2), "yaxislocation", "right"); |
144 set (ax(2), "ycolor", getcolor (h2(1))); | 142 set (ax(2), "ycolor", getcolor (h2(1))); |
145 set (ax(2), "position", get (ax(1), "position")); | 143 set (ax(2), "position", get (ax(1), "position")); |
144 set (ax(2), "activepositionproperty", "position"); | |
146 set (ax(2), "xlim", xlim); | 145 set (ax(2), "xlim", xlim); |
147 set (ax(2), "color", "none"); | 146 set (ax(2), "color", "none"); |
148 | |
149 ## Add invisible text objects that when destroyed, | |
150 ## also remove the other axis | |
151 t1 = text (0, 0, "", "parent", ax(1), "tag", "plotyy", | |
152 "handlevisibility", "off", "visible", "off", | |
153 "xliminclude", "off", "yliminclude", "off"); | |
154 t2 = text (0, 0, "", "parent", ax(2), "tag", "plotyy", | |
155 "handlevisibility", "off", "visible", "off", | |
156 "xliminclude", "off", "yliminclude", "off"); | |
157 | |
158 set (t1, "deletefcn", {@deleteplotyy, ax(2), t2}); | |
159 set (t2, "deletefcn", {@deleteplotyy, ax(1), t1}); | |
160 | |
161 addlistener (ax(1), "position", {@update_position, ax(2)}); | |
162 addlistener (ax(2), "position", {@update_position, ax(1)}); | |
163 addlistener (ax(1), "view", {@update_position, ax(2)}); | |
164 addlistener (ax(2), "view", {@update_position, ax(1)}); | |
165 | |
166 ## Tag the plotyy axes, so we can use that information | |
167 ## not to mirror the y axis tick marks | |
168 set (ax, "tag", "plotyy") | |
169 | |
170 endfunction | |
171 | |
172 %!demo | |
173 %! x = 0:0.1:2*pi; | |
174 %! y1 = sin (x); | |
175 %! y2 = exp (x - 1); | |
176 %! ax = plotyy (x, y1, x - 1, y2, @plot, @semilogy); | |
177 %! xlabel ("X"); | |
178 %! ylabel (ax(1), "Axis 1"); | |
179 %! ylabel (ax(2), "Axis 2"); | |
180 | |
181 function deleteplotyy (h, d, ax2, t2) | |
182 if (ishandle (ax2) && strcmp (get (ax2, "type"), "axes") && | |
183 (isempty (gcbf()) || strcmp (get (gcbf(), "beingdeleted"),"off")) && | |
184 strcmp (get (ax2, "beingdeleted"), "off")) | |
185 set (t2, "deletefcn", []); | |
186 delete (ax2); | |
187 endif | |
188 endfunction | |
189 | |
190 function update_position (h, d, ax2) | |
191 persistent recursion = false; | |
192 | |
193 ## Don't allow recursion | |
194 if (! recursion) | |
195 unwind_protect | |
196 recursion = true; | |
197 position = get (h, "position"); | |
198 view = get (h, "view"); | |
199 oldposition = get (ax2, "position"); | |
200 oldview = get (ax2, "view"); | |
201 if (! (isequal (position, oldposition) && isequal (view, oldview))) | |
202 set (ax2, "position", position, "view", view); | |
203 endif | |
204 unwind_protect_cleanup | |
205 recursion = false; | |
206 end_unwind_protect | |
207 endif | |
208 endfunction | 147 endfunction |
209 | 148 |
210 function color = getcolor (ax) | 149 function color = getcolor (ax) |
211 obj = get (ax); | 150 obj = get (ax); |
212 if (isfield (obj, "color")) | 151 if (isfield (obj, "color")) |
218 else | 157 else |
219 color = [0, 0, 0]; | 158 color = [0, 0, 0]; |
220 endif | 159 endif |
221 endfunction | 160 endfunction |
222 | 161 |
162 %!demo | |
163 %! x = 0:0.1:2*pi; | |
164 %! y1 = sin (x); | |
165 %! y2 = exp (x - 1); | |
166 %! ax = plotyy (x, y1, x - 1, y2, @plot, @semilogy); | |
167 %! xlabel ("X"); | |
168 %! ylabel (ax(1), "Axis 1"); | |
169 %! ylabel (ax(2), "Axis 2"); |