FancyBBox set_width

Hi all,
I’m trying to have a Text object with a fancy box, as in this example: http://matplotlib.sourceforge.net/mpl_examples/pylab_examples/fancybox_demo2.py
. However, the key difference is that I want to have the box (in my case, I’m interested in an RArrow) be a specified width (in units of the
plot), rather than just fitting it to the text I’ve given (crude ascii art below). The following seems not to work:

ax = gca()
txtobj = ax.text(0, -.1 * yrange, ‘text’,
bbox=dict(boxstyle=‘rarrow’))
txtobj.get_bbox_patch().set_

width(SIZE_IM_INTERESTED_IN)
draw_if_interactive()

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 >

------/

I had to deal with this lately, and there is no current way to do what you want without patching the MPL source. I have a patch for it, but it does not behave well enough to use in the general sense…and you have target the correct code, the lack of flexibility lies in the Text._draw_bbox function.

I’m afraid that you will have to create the FancyBboxPatch (the arrow) directly, size it and place it, and then create some text (with no box) to go inside.

···

On Sun, Aug 19, 2012 at 2:41 AM, Peter Combs <pcombs+mpl@…287…> wrote:

Hi all,
I’m trying to have a Text object with a fancy box, as in this example: http://matplotlib.sourceforge.net/mpl_examples/pylab_examples/fancybox_demo2.py
. However, the key difference is that I want to have the box (in my case, I’m interested in an RArrow) be a specified width (in units of the
plot), rather than just fitting it to the text I’ve given (crude ascii art below). The following seems not to work:

ax = gca()
txtobj = ax.text(0, -.1 * yrange, ‘text’,
bbox=dict(boxstyle=‘rarrow’))
txtobj.get_bbox_patch().set_
width(SIZE_IM_INTERESTED_IN)
draw_if_interactive()

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 >

------/


Live Security Virtual Conference

Exclusive live event will cover all the ways today’s security and

threat landscape has changed and how IT managers can respond. Discussions

will include endpoint security, mobile security and the latest in malware

threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/


Matplotlib-users mailing list

Matplotlib-users@lists.sourceforge.net

https://lists.sourceforge.net/lists/listinfo/matplotlib-users


Daniel Hyams
dhyams@…1972…

Another, very hacky but quick way to do this, is to put spaces around your text until the arrow is the size you desire:

" your text "

and if you want the arrow to expand upward and downward, put in return characters (I told you it was crude ;))

···

On Sun, Aug 19, 2012 at 2:41 AM, Peter Combs <pcombs+mpl@…287…> wrote:

Hi all,
I’m trying to have a Text object with a fancy box, as in this example: http://matplotlib.sourceforge.net/mpl_examples/pylab_examples/fancybox_demo2.py
. However, the key difference is that I want to have the box (in my case, I’m interested in an RArrow) be a specified width (in units of the
plot), rather than just fitting it to the text I’ve given (crude ascii art below). The following seems not to work:

ax = gca()
txtobj = ax.text(0, -.1 * yrange, ‘text’,
bbox=dict(boxstyle=‘rarrow’))
txtobj.get_bbox_patch().set_
width(SIZE_IM_INTERESTED_IN)
draw_if_interactive()

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 >

------/


Live Security Virtual Conference

Exclusive live event will cover all the ways today’s security and

threat landscape has changed and how IT managers can respond. Discussions

will include endpoint security, mobile security and the latest in malware

threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/


Matplotlib-users mailing list

Matplotlib-users@lists.sourceforge.net

https://lists.sourceforge.net/lists/listinfo/matplotlib-users


Daniel Hyams
dhyams@…1972…

2012/8/19 Peter Combs <pcombs+mpl@...287...>:

Hi all,
I'm trying to have a Text object with a fancy box, as in this example:
http://matplotlib.sourceforge.net/mpl_examples/pylab_examples/fancybox_demo2.py
. However, the key difference is that I want to have the box (in my case,
I'm interested in an RArrow) be a specified width (in units of the plot),
rather than just fitting it to the text I've given (crude ascii art below).
The following seems not to work:

ax = gca()
txtobj = ax.text(0, -.1 * yrange, 'text',
                     bbox=dict(boxstyle='rarrow'))
txtobj.get_bbox_patch().set_
width(SIZE_IM_INTERESTED_IN)
draw_if_interactive()

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.

Not ideal but better:

from pyplot import *
subplot(111)
text(0.1, 0.3, 'XXXXXXXXXXXXXXX', alpha=0, bbox=dict(boxstyle='rarrow'))
text(0.1, 0.3, 'short')
text(0.1, 0.6, 'XXXXXXXXXXXXXXX', alpha=0, bbox=dict(boxstyle='rarrow'))
text(0.1, 0.6, 'looooooooooong')
show()

Goyo

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

···

On Sun, Aug 19, 2012 at 3:41 PM, Peter Combs <pcombs+mpl@...287...> 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 >
------/