Automatic placement of a text box? ie empty legend [matplotlib]

Cousin Stanley cousinstanley at gmail.com
Wed Jun 29 14:41:12 EDT 2011


Christopher Barrington-Leigh wrote:

> I'd like to have the Sample A box place itself 
> in the optimal empty space, so as not to overlay 
> any graphing elements (if possible):
> ....

  A simple alternative might be to place the label
  just outside of the plot region either at the top 
  or the bottom of the plot instead of finding 
  an open area in which to place the label ....

def extremes( x1 , y1 , x2 , y2 ) :

    min_x1 = min( x1 )
    min_y1 = min( y1 )

    max_x1 = max( x1 )
    max_y1 = max( y1 )

    min_x2 = min( x2 )
    min_y2 = min( y2 )

    max_x2 = max( x2 )
    max_y2 = max( y2 )

    min_x  = min( min_x1 , min_x2 )
    min_y  = min( min_y1 , min_y2 )

    max_x  = max( max_x1 , max_x2 )
    max_y  = max( max_y1 , max_y2 )

    aminx  = min_x
    aminy  = min_y - 1.4

    amaxx  = max_x
    amaxy  = max_y + 1.4

    return aminx , aminy , amaxx , amaxy
    

# bump up the plot size a bit

fig = plt.figure( 1 , figsize = ( 6 , 6 ) )

....

aminx , aminy , amaxx , amaxy = extremes( x1 , y1 , x2 , y2 )

....

# label at top center 

ax.text( 0 , amaxy , "Sample A" , ha = "center" , va = "center" , 
         size = 16 , bbox = bbox_props )


-- 
Stanley C. Kitching
Human Being
Phoenix, Arizona




More information about the Python-list mailing list