comparison scripts/plot/plotyy.m @ 8208:f6ca8ff51818

[mq]: graphics-backend
author John W. Eaton <jwe@octave.org>
date Fri, 10 Oct 2008 11:07:53 -0400
parents c066714ee5d5
children 52f2fba4f3f8
comparison
equal deleted inserted replaced
8207:60b4c75287a1 8208:f6ca8ff51818
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 for i = 3 : length (ax) 80 if (length (ax) > 2)
81 delete (ax (i)); 81 for i = 3 : length (ax)
82 endfor 82 delete (ax (i));
83 ax = ax(1:2); 83 endfor
84 ax = ax(1:2);
85 elseif (length (ax) == 1)
86 ax(2) = axes ();
87 endif
84 endif 88 endif
85 if (nargin < 2) 89 if (nargin < 2)
86 varargin = {}; 90 varargin = {};
87 endif 91 endif
88 endif 92 endif
123 axes (ax(1)); 127 axes (ax(1));
124 newplot (); 128 newplot ();
125 h1 = feval (fun1, x1, y1); 129 h1 = feval (fun1, x1, y1);
126 130
127 set (ax(1), "ycolor", getcolor (h1(1))); 131 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");
130 set (ax(1), "xlim", xlim); 132 set (ax(1), "xlim", xlim);
131 133
132 cf = gcf (); 134 cf = gcf ();
133 set (cf, "nextplot", "add"); 135 set (cf, "nextplot", "add");
134 axes (ax(2)); 136 axes (ax(2));
139 141
140 h2 = feval (fun2, x2, y2); 142 h2 = feval (fun2, x2, y2);
141 set (ax(2), "yaxislocation", "right"); 143 set (ax(2), "yaxislocation", "right");
142 set (ax(2), "ycolor", getcolor (h2(1))); 144 set (ax(2), "ycolor", getcolor (h2(1)));
143 set (ax(2), "position", get (ax(1), "position")); 145 set (ax(2), "position", get (ax(1), "position"));
144 set (ax(2), "activepositionproperty", "position");
145 set (ax(2), "xlim", xlim); 146 set (ax(2), "xlim", xlim);
146 set (ax(2), "color", "none"); 147 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
147 endfunction 208 endfunction
148 209
149 function color = getcolor (ax) 210 function color = getcolor (ax)
150 obj = get (ax); 211 obj = get (ax);
151 if (isfield (obj, "color")) 212 if (isfield (obj, "color"))
157 else 218 else
158 color = [0, 0, 0]; 219 color = [0, 0, 0];
159 endif 220 endif
160 endfunction 221 endfunction
161 222
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");