scripts/plot/plotyy.m
changeset 9337 fee95bb4ee94
parent 9336 9f24c50b6fa0
     1.1 --- a/scripts/plot/plotyy.m	Tue Jun 16 06:43:56 2009 +0200
     1.2 +++ b/scripts/plot/plotyy.m	Tue Jun 16 06:43:56 2009 +0200
     1.3 @@ -75,7 +75,14 @@
     1.4      if (isempty (f))
     1.5        f = figure ();
     1.6      endif
     1.7 -    ax = get (f, "children");
     1.8 +    ca = get (f, "currentaxes");
     1.9 +    if (isempty (ca))
    1.10 +      ax = [];
    1.11 +    elseif (strcmp (get (ca, "tag"), "plotyy"));
    1.12 +      ax = get (ca, "__plotyy_axes__");
    1.13 +    else
    1.14 +      ax = ca;
    1.15 +    endif
    1.16      if (length (ax) > 2)
    1.17        for i = 3 : length (ax)
    1.18          delete (ax (i));
    1.19 @@ -175,11 +182,17 @@
    1.20    addlistener (ax(2), "position", {@update_position, ax(1)});
    1.21    addlistener (ax(1), "view", {@update_position, ax(2)});
    1.22    addlistener (ax(2), "view", {@update_position, ax(1)});
    1.23 +  addlistener (ax(1), "dataaspectratio", {@update_position, ax(2)});
    1.24 +  addlistener (ax(2), "dataaspectratio", {@update_position, ax(1)});
    1.25  
    1.26    ## Tag the plotyy axes, so we can use that information
    1.27    ## not to mirror the y axis tick marks
    1.28    set (ax, "tag", "plotyy")
    1.29  
    1.30 +  ## Store the axes handles for the sister axes.
    1.31 +  addproperty ("__plotyy_axes__", ax(1), "data", ax);
    1.32 +  addproperty ("__plotyy_axes__", ax(2), "data", ax);
    1.33 +
    1.34  endfunction
    1.35  
    1.36  %!demo
    1.37 @@ -192,6 +205,19 @@
    1.38  %! ylabel (ax(1), "Axis 1");
    1.39  %! ylabel (ax(2), "Axis 2");
    1.40  
    1.41 +%!demo
    1.42 +%! clf
    1.43 +%! x = linspace (-1, 1, 201);
    1.44 +%! subplot (2, 2, 1)
    1.45 +%! plotyy (x, sin(pi*x), x, 10*cos(pi*x))
    1.46 +%! subplot (2, 2, 2)
    1.47 +%! surf (peaks (25))
    1.48 +%! subplot (2, 2, 3)
    1.49 +%! contour (peaks (25))
    1.50 +%! subplot (2, 2, 4)
    1.51 +%! plotyy (x, 10*sin(2*pi*x), x, cos(2*pi*x))
    1.52 +%! axis square
    1.53 +
    1.54  function deleteplotyy (h, d, ax2, t2)
    1.55    if (ishandle (ax2) && strcmp (get (ax2, "type"), "axes") && 
    1.56        (isempty (gcbf()) || strcmp (get (gcbf(), "beingdeleted"),"off")) &&
    1.57 @@ -210,10 +236,16 @@
    1.58        recursion = true;
    1.59        position = get (h, "position");
    1.60        view = get (h, "view");
    1.61 +      dataaspectratio = get (h, "dataaspectratio");
    1.62        oldposition = get (ax2, "position");
    1.63        oldview = get (ax2, "view");
    1.64 -      if (! (isequal (position, oldposition) && isequal (view, oldview)))
    1.65 -	set (ax2, "position", position, "view", view);
    1.66 +      olddataaspectratio = get (ax2, "dataaspectratio");
    1.67 +      if (! (isequal (position, oldposition)
    1.68 +             && isequal (view, oldview)
    1.69 +             && isequal (dataaspectratio, olddataaspectratio)))
    1.70 +	set (ax2, "position", position,
    1.71 +                  "view", view,
    1.72 +		  "dataaspectratio", dataaspectratio);
    1.73        endif
    1.74      unwind_protect_cleanup
    1.75        recursion = false;