|
From: Daniel H. <dh...@gm...> - 2012-08-08 23:23:16
|
I was wanting to allow the specification of the size of the box used to
enclose the text in an annotation, rather than allowing it to always fit
exactly to the text. I know that I could always just draw a rectangle, and
then text on top, but I was wanting to also be able to drag thing thing
around on the graph, in which case it is much more straightforward to keep
the text and rectangle together, just like the annotation does.
So my first attempt at drawing the box according to a set size looks like
the attached code: in text.py, I inserted some code into Text._draw_bbox().
So long as the annotation has an attribute bbox_size that exists, it's
used for the size of the box. Very straightforward, *but* it doesn't
survive a text rotation like an annotation (see attached images). I have
no clue what I'm doing wrong, and I will also have to confess a lot of
ignorance when it comes to matplotlib's transforms, mutations, and such.
I've been fiddling with this for hours with no success, and it seems that
it should be simple :(
def _draw_bbox(self, renderer, posx, posy):
""" Update the location and the size of the bbox
(FancyBoxPatch), and draw
"""
x_box, y_box, w_box, h_box = _get_textbox(self, renderer)
#======================my extra code ===============================
# self._get_xy is annotation specific.
# self.textcoords is annotation specific.
x_box_orig,y_box_orig = x_box,y_box
if getattr(self,'bbox_size',None):
bx,by = self.bbox_size
pt0 = self._get_xy(renderer,0.0,0.0,self.textcoords)
pt1 = self._get_xy(renderer,bx,0.0,self.textcoords)
pt2 = self._get_xy(renderer,0.0,by,self.textcoords)
bx = pt1[0]-pt0[0]
by = pt2[1]-pt0[1]
xc = x_box + w_box/2
yc = y_box + h_box/2
x_box = xc - bx/2
y_box = yc - by/2
w_box = bx
h_box = by
#================================================================================
self._bbox_patch.set_bounds(0., 0.,
w_box, h_box)
theta = self.get_rotation()/180.*math.pi
tr = mtransforms.Affine2D().rotate(theta)
tr = tr.translate(posx+x_box, posy+y_box)
self._bbox_patch.set_transform(tr)
fontsize_in_pixel = renderer.points_to_pixels(self.get_size())
self._bbox_patch.set_mutation_scale(fontsize_in_pixel)
self._bbox_patch.draw(renderer)
--
Daniel Hyams
dh...@gm...
|