# how to get legend size, try #2.

I realized after sending that off that I need to provide more context…sorry about that.

What I’m trying to do can be boiled down to the following: I’m trying to place a legend precisely, using the top left corner of legend as the “sticky” point. In other words, if I want to place the legend here:

···

±--------------------------------±----------+

``````                            >           >
>  legend   |
``````
``````              The plot...   |-----------+
``````
``````                            >
>
>
``````
``````                            >
>
>
``````

±--------------------------------+

I would have thought that I would set bbox_to_anchor = (0,0,1,1), and loc = (1,1). I found out quickly, though, that this places the legend like this:

``````                              +-----------+
>           >
>  legend   |
``````

±--------------------------------±----------+

``````                            >
``````
``````                            >
``````
``````              The plot...   |
``````
``````                            >
``````
``````                            >
``````
``````                            >
``````
``````                            >
``````
``````                            >
``````
``````                            >
``````

±--------------------------------+

Which makes perfect sense from matplotlib’s perspective. So all I need to do is figure out how tall the legend is, and subtract that off the y coordinate before passing ‘loc’ off to matplotlib’s legend. I just can’t seem to figure out how to get that number. I tried self.ax.get_legend().get_frame().get_height(), but that just returns 1 all the time.

Ascii art is fun!

Daniel Hyams
dhyams@…287…

Daniel Hyams, on 2010-10-29 17:07, wrote:

I realized after sending that off that I need to provide more

What I'm trying to do can be boiled down to the following: I'm trying to
place a legend precisely, using the top left corner of legend as the
"sticky" point. In other words, if I want to place the legend here:

+---------------------------------+-----------+
> > >
> > legend |
> The plot... |-----------+
> >
> >
> >
> >
> >
> >
+---------------------------------+

I would have thought that I would set bbox_to_anchor = (0,0,1,1), and loc =
(1,1). I found out quickly, though, that this places the legend like this:
+-----------+
> >
> legend |
+---------------------------------+-----------+
> >
> >
> The plot... |
> >
> >
> >
> >
> >
> >
+---------------------------------+

Which makes perfect sense from matplotlib's perspective. So all I need to
do is figure out how tall the legend is, and subtract that off the y
coordinate before passing 'loc' off to matplotlib's legend. I just can't
seem to figure out how to get that number. I tried
self.ax.get_legend().get_frame().get_height(), but that just returns 1 all
the time.

I think you can just get what you want using:

plt.plot([3,1,4,1,5,9,2,6,5], label='awesome')
plt.legend(bbox_to_anchor=(1,1),loc=2)

where loc=2 could have also been written as loc='upper left'

Ascii art is fun!

Indeed!

P.S.
Your posts made it to this and the devel list - they
(frustratingly) don't send you your own copy back when you post
something. I usually verify that the post went through my
checking gmane or sourceforge archives.

best,

···

--
Paul Ivanov
314 address only used for lists, off-list direct email at:
http://pirsquared.org | GPG/PGP key id: 0x0F3E28F7