source: issm/oecreview/Archive/14064-14311/ISSM-14242-14243.diff

Last change on this file was 14312, checked in by Mathieu Morlighem, 13 years ago

Added 14064-14311

File size: 2.9 KB
  • ../trunk-jpl/src/m/plot/plotboxpos.m

     
     1function pos = plotboxpos(h)
     2%PLOTBOXPOS Returns the position of the plotted axis region
     3%
     4% pos = plotboxpos(h)
     5%
     6% This function returns the position of the plotted region of an axis,
     7% which may differ from the actual axis position, depending on the axis
     8% limits, data aspect ratio, and plot box aspect ratio.  The position is
     9% returned in the same units as the those used to define the axis itself.
     10% This function can only be used for a 2D plot. 
     11%
     12% Input variables:
     13%
     14%   h:      axis handle of a 2D axis (if ommitted, current axis is used).
     15%
     16% Output variables:
     17%
     18%   pos:    four-element position vector, in same units as h
     19
     20% Copyright 2010 Kelly Kearney
     21
     22% Check input
     23
     24if nargin < 1
     25    h = gca;
     26end
     27
     28if ~ishandle(h) || ~strcmp(get(h,'type'), 'axes')
     29    error('Input must be an axis handle');
     30end
     31
     32% Get position of axis in pixels
     33
     34currunit = get(h, 'units');
     35set(h, 'units', 'pixels');
     36axisPos = get(h, 'Position');
     37set(h, 'Units', currunit);
     38
     39% Calculate box position based axis limits and aspect ratios
     40
     41darismanual  = strcmpi(get(h, 'DataAspectRatioMode'),    'manual');
     42pbarismanual = strcmpi(get(h, 'PlotBoxAspectRatioMode'), 'manual');
     43
     44if ~darismanual && ~pbarismanual
     45   
     46    pos = axisPos;
     47   
     48else
     49
     50    dx = diff(get(h, 'XLim'));
     51    dy = diff(get(h, 'YLim'));
     52    dar = get(h, 'DataAspectRatio');
     53    pbar = get(h, 'PlotBoxAspectRatio');
     54
     55    limDarRatio = (dx/dar(1))/(dy/dar(2));
     56    pbarRatio = pbar(1)/pbar(2);
     57    axisRatio = axisPos(3)/axisPos(4);
     58
     59    if darismanual
     60        if limDarRatio > axisRatio
     61            pos(1) = axisPos(1);
     62            pos(3) = axisPos(3);
     63            pos(4) = axisPos(3)/limDarRatio;
     64            pos(2) = (axisPos(4) - pos(4))/2 + axisPos(2);
     65        else
     66            pos(2) = axisPos(2);
     67            pos(4) = axisPos(4);
     68            pos(3) = axisPos(4) * limDarRatio;
     69            pos(1) = (axisPos(3) - pos(3))/2 + axisPos(1);
     70        end
     71    elseif pbarismanual
     72        if pbarRatio > axisRatio
     73            pos(1) = axisPos(1);
     74            pos(3) = axisPos(3);
     75            pos(4) = axisPos(3)/pbarRatio;
     76            pos(2) = (axisPos(4) - pos(4))/2 + axisPos(2);
     77        else
     78            pos(2) = axisPos(2);
     79            pos(4) = axisPos(4);
     80            pos(3) = axisPos(4) * pbarRatio;
     81            pos(1) = (axisPos(3) - pos(3))/2 + axisPos(1);
     82        end
     83    end
     84end
     85
     86% Convert plot box position to the units used by the axis
     87
     88temp = axes('Units', 'Pixels', 'Position', pos, 'Visible', 'off', 'parent', get(h, 'parent'));
     89set(temp, 'Units', currunit);
     90pos = get(temp, 'position');
     91delete(temp);
     92 No newline at end of file
Note: See TracBrowser for help on using the repository browser.