pick_legend_corner.ncl 2.28 KB
Newer Older
1
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2
; pick_legend_corner.ncl
3
4
5
6
7
8
9
; Carl Schreck (cjschrec@ncsu.edu)
; August 2015
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Description: Pick the corner of the plot where the legend is least likely 
; to obscure data
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

10
11
undef ( "pick_legend_corner" )
function pick_legend_corner( \
12
13
14
15
  i_xData : numeric, \
  i_yData : numeric, \
  i_res   : logical \
)
16
begin ; pick_legend_corner
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31

  xDims = dimsizes( dimsizes( i_xData ) )
  yDims = dimsizes( dimsizes( i_yData ) )

  if( xDims.gt.yDims ) then
    yData = conform( i_xData, i_yData, 1 )
  else
    yData = i_yData
  end if
  if( yDims.gt.xDims ) then
    xData = conform( i_yData, i_xData, 1 )
  else
    xData = i_xData
  end if

32
  alpha = 0.3
33
34
35
36
37
  xLow  = ( 1 - alpha ) * min(xData) + ( 0 + alpha ) * max(xData)
  xHigh = ( 0 + alpha ) * min(xData) + ( 1 - alpha ) * max(xData)
  yLow  = ( 1 - alpha ) * min(yData) + ( 0 + alpha ) * max(yData)
  yHigh = ( 0 + alpha ) * min(yData) + ( 1 - alpha ) * max(yData)

38
; print( xLow + " " + xHigh + " " + yLow + " " + yHigh )
39

40
41
42
43
  lowerLeft  = num( ( xData.lt.xLow  ).and.( yData.lt.yLow  ) )
  lowerRight = num( ( xData.gt.xHigh ).and.( yData.lt.yLow  ) )
  upperLeft  = num( ( xData.lt.xLow  ).and.( yData.gt.yHigh ) )
  upperRight = num( ( xData.gt.xHigh ).and.( yData.gt.yHigh ) )
44
45

  minQuad = min( (/ lowerLeft, lowerRight, upperLeft, upperRight /) )
46
47
; print( minQuad + ": " + lowerLeft + " " + lowerRight + " " + upperLeft  \
;     + " " + upperRight )
48
49
50
51

  res = True
  if( lowerRight.eq.minQuad )
    print( "Lower Right" )
52
    res@XPosPercent = 60
53
54
    res@YPosPercent = 5
    res@Position = "Bottom"
55
56
  else if( upperRight.eq.minQuad )
    print( "Upper Right" )
57
    res@XPosPercent = 60
58
59
    res@YPosPercent = 95
    res@Position = "Top"
60
61
  else if( lowerLeft.eq.minQuad )
    print( "Lower Left" )
62
63
64
    res@XPosPercent = 5
    res@YPosPercent = 5
    res@Position = "Bottom"
65
66
  else if( upperLeft.eq.minQuad )
    print( "Upper Left" )
67
68
69
    res@XPosPercent = 5
    res@YPosPercent = 95
    res@Position = "Top"
70
71
72
73
74
75
76
77
78
79
  end if
  end if
  end if
  end if

  if( i_res ) then
    copy_VarAtts( i_res, res )
  end if

  return(res)
80
end; pick_legend_corner
81