Pixel placement of text

Attached is a simple example that illustrates the method.

    > What threw me off last night is that the
    > copy_bbox_transform() function was not doing what I
    > expected. I don't know yet whether this is because of a bug
    > or a misunderstanding on my part, but in any case, the
    > example provides an alternative. (It is not valid for any
    > bbox transform, but I think it will be fine in normal use

What I just wrote to Eric offlist is that copy_bbox_transform is a
deep copy, and so all the references of the matplotlib transforms are
lost (see http://matplotlib.sf.net/matplotlib.transforms.html for
details on the reference semantics of mpl transforms). What we want
is a shallow copy -- this is like Eric's get_bbox_transform below but
also handles nonlinear transformation like log

I added to svn:

def copy_bbox_transform_shallow(trans):
    """
    return a shallow copy of the bbox transform -- the Values are
    retained by reference but the transform is copied. This allows
    you to copy a transform, set a new offset to it, but not lose the
    value reference semantics
    """

    inbox = trans.get_bbox1()
    outbox = trans.get_bbox2()

    typex = trans.get_funcx().get_type()
    typey = trans.get_funcy().get_type()

    newtrans = get_bbox_transform(inbox, outbox)
    newtrans.get_funcx().set_type(typex)
    newtrans.get_funcy().set_type(typey)
    return newtrans

    > cases.) The basic method can be used on any artist by
    > calling set_offset on that artist's transform.

More precisely, on a shallow copy of any artist's transform. You
don't want to set the offset of the artist's transform itself, since
that will move the artist itself.

Eric, how about adding a helper method to matplotlib.transforms call
"relative_to", something like

  trans = relative_to(artist, pointsx, pointsy)

you can get dpi from artist.figure.dpi to construct the right
points->pixels offset. Then you could do

  trans = relative_to(line, -5, 10)
  ax.text(x, y, 'hi', transform=trans)

This may not be the perfect API, but it should give you the idea of
providing a very simple interface to users to build these transforms
for offsets.

JDH

John,

Very good--I will work on it this weekend.

Eric

···

Eric, how about adding a helper method to matplotlib.transforms call
"relative_to", something like

  trans = relative_to(artist, pointsx, pointsy)

you can get dpi from artist.figure.dpi to construct the right
points->pixels offset. Then you could do

  trans = relative_to(line, -5, 10)
  ax.text(x, y, 'hi', transform=trans)

This may not be the perfect API, but it should give you the idea of
providing a very simple interface to users to build these transforms
for offsets.

JDH