|
From: Jae-Joon L. <lee...@gm...> - 2012-09-05 05:58:52
|
On Sun, Aug 19, 2012 at 3:41 PM, Peter Combs <pco...@gm...> wrote:
> It seems like draw()ing the text object will reset the size of the BBox...
> Any idea how to fix this? At the moment, I'm experimenting with continually
> drawing, polling the get_width() method, and when it's too small, adding in
> spaces around the text field, but that seems both not to work reliably, and
> be an incredibly boneheaded way to go about it.
>
> I'm using matplotlib v. 1.1.0 if that makes a difference.
>
>
> --------------\
> | text >
> --------------/
> not
> ------\
> | text >
> ------/
A recommended way of doing this is to make a custom BoxStyle,
from matplotlib.patches import BoxStyle
class MyRArrow(BoxStyle.RArrow):
def transmute(self, x0, y0, width, height, mutation_size):
# mutation_size is a fontsize in pixel scale.
total_width = mutation_size*10
dw = (total_width-width)*.5
p = BoxStyle.RArrow.transmute(self, x0-dw, y0, total_width, height,
mutation_size)
return p
ax = subplot(111)
txtobj = ax.text(0.5, 0.5, 'text', ha="center",
bbox=dict(boxstyle=MyRArrow()))
txtobj = ax.text(0.5, 0.3, 'long text', ha="center",
bbox=dict(boxstyle=MyRArrow()))
However, BoxStyle does not know of the axes/figure/renderer, i.e., you
cannot make its width to some fraction of figure width, for example.
If this is what you want, you may need to derive from FancyBboxPatch
as Daniel Hyams suggested.
Regards,
-JJ
|