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

Discussion in 'Python' started by Christopher Barrington-Leigh, Jun 29, 2011.

  1. I still need help with this.

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



    import numpy.random
    import matplotlib.pyplot as plt

    fig = plt.figure(1, figsize=(5,5))
    fig.clf()

    ax = fig.add_subplot(111)
    ax.set_aspect(1)

    x1 = -1 + numpy.random.randn(100)
    y1 = -1 + numpy.random.randn(100)
    x2 = 1. + numpy.random.randn(100)
    y2 = 1. + numpy.random.randn(100)

    ax.scatter(x1, y1, color="r")
    ax.scatter(x2, y2, color="g")

    bbox_props = dict(boxstyle="round", fc="w", ec="0.5", alpha=0.9)
    ax.text(-2, -2, "Sample A", ha="center", va="center", size=20,
    bbox=bbox_props)

    plt.draw()
    plt.show()
     
    Christopher Barrington-Leigh, Jun 29, 2011
    #1
    1. Advertising

  2. 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
     
    Cousin Stanley, Jun 29, 2011
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jorl Shefner

    Legend problems in MatPlotLib

    Jorl Shefner, Dec 16, 2004, in forum: Python
    Replies:
    1
    Views:
    607
    John Hunter
    Dec 16, 2004
  2. Jorl Shefner
    Replies:
    1
    Views:
    806
    John Hunter
    Feb 28, 2005
  3. bwaha

    matplotlib legend problem

    bwaha, Jan 27, 2006, in forum: Python
    Replies:
    2
    Views:
    2,218
    John Hunter
    Jan 27, 2006
  4. Frédéric Degraeve

    matplotlib / legend of x-axis

    Frédéric Degraeve, Mar 26, 2008, in forum: Python
    Replies:
    0
    Views:
    447
    Frédéric Degraeve
    Mar 26, 2008
  5. C Barrington-Leigh
    Replies:
    0
    Views:
    269
    C Barrington-Leigh
    Apr 27, 2011
Loading...

Share This Page