legend and bbox_to_anchor, and draggable()

Daniel Hyams <dhyams@...287...> writes:

I was playing around with draggable legends, and some strange things started
happening (exception down in the depths of beckend_agg.py, accompanied by a
long stack trace). To make a long story short, I can reproduce this in the
draggable_legend.py example on the matplotlib website. All you have to do
is change line 7 [...] to

l = ax.legend(bbox_to_anchor=(.1,.1))

Apparently this creates a bounding box with zero width and height, which
causes NaNs to appear in various transforms later on. It seems that you
can get the same placement of the legend with

l = ax.legend(bbox_to_anchor=(0,0,.1,.1))

which creates a non-degenerate bounding box that doesn't have this
problem. A possibly simpler option is

l = ax.legend(loc=(-0.2,0.1))

which sets the position of the lower-left corner of the legend box so
you'll need to tweak the coordinates from what you used with
bbox_to_anchor.

I'm sure that I'm not understanding the proper usage of
bbox_to_anchor.

I'm not sure either. It seems that the two-number form of the bounding
box is meant to create a degenerate bounding box so that any kind of
location specifier ("upper right", "lower center", etc) will always hit
that exact place, but perhaps naturally that makes it difficult to move
the box around.

···

--
Jouni K. Sepp�nen
http://www.iki.fi/jks

In the current implementation, the legend location (Legend.loc) is set
in the normalized coordinate of the bbox_to_anchor (and later
transformed to the "canvas coordinate" during the drawing time).

On the other hand, when the legend is moved by a user, it tries to
update Legend.loc from the current position of the legend in the
canvas coordinate, which results in NaNs if the bbox_to_anchor has
zero width and zero height. A workaround is to set the bbox_to_anchor
to its default value (Axes.bbox) when this happens. While I think it
is reasonable thing to do, let me know of any other opinion.

A patch is attached.
Unless there is no objection, I'll commit the patch soon.

Regards,

-JJ

fix_bbox_to_anchor_for_draggable_legend.diff (852 Bytes)

···

On Sun, Sep 26, 2010 at 4:11 AM, Jouni K. Seppänen <jks@...397...> wrote:

I'm not sure either. It seems that the two-number form of the bounding
box is meant to create a degenerate bounding box so that any kind of
location specifier ("upper right", "lower center", etc) will always hit
that exact place, but perhaps naturally that makes it difficult to move
the box around.