6
import matplotlib.pyplot as plt
import numpy as np

x = np.arange(10)

fig = plt.figure()
ax = plt.subplot(111)

for i in xrange(5):
    ax.plot(x, i * x, label='$y = %ix$' % i)

ax.legend(bbox_to_anchor=(1.1, 1.05))

plt.show()

In the above code I have come accross the function bbox_to_anchor which places the legend in arbitary postion . I am not able to understand the first two arguements of the function and all liertature says is normalized axis parameters. Can any body please explain what they are and how to manipulate them ?

Alexis
  • 2,104
  • 2
  • 19
  • 40
Soumya
  • 87
  • 1
  • 2
  • 15
  • Possible duplicate of [What does a 4-element tuple argument for 'bbox\_to\_anchor' mean in matplotlib?](http://stackoverflow.com/questions/39803385/what-does-a-4-element-tuple-argument-for-bbox-to-anchor-mean-in-matplotlib) – ImportanceOfBeingErnest Dec 01 '16 at 16:26
  • This question already has an answer [here](http://stackoverflow.com/questions/39803385/what-does-a-4-element-tuple-argument-for-bbox-to-anchor-mean-in-matplotlib). Omitting the last two arguments, will create a bounding box with no extention. – ImportanceOfBeingErnest Dec 01 '16 at 16:29
  • An explanation is also be found in [this answer](https://stackoverflow.com/a/43439132/4124317) – ImportanceOfBeingErnest Jun 12 '17 at 15:55

2 Answers2

8

It's not a function but a keyword argument.

Summary: you use loc to specify a corner of the legend and optionally bbox_to_anchor to specify a location for that corner. By default the specified corner of the legend will be placed on the same corner of the axes.

For example loc='upper right' will just place the upper right corner of the legend on the upper right of the axes:

ax.legend(loc='upper right')

enter image description here

But if you want the upper right corner of the legend to be on the center left of the axes you can use bbox_to_anchor=(0, 0.5):

ax.legend(loc='upper right', bbox_to_anchor=(0, 0.5))

enter image description here

Stop harming Monica
  • 12,141
  • 1
  • 36
  • 56
  • 1
    I understand your point but a parameter specification like (1,0.5) makes the legend partially visible . is there any way to overcome that ? – Soumya Dec 02 '16 at 06:29
  • The value of `bbox_to_inches` does not make the legend visible or invisible. A legend may be totally or partially visible or invisible depending of several things, including its location. If you have trouble making a legend visible please ask another question. – Stop harming Monica Dec 02 '16 at 07:49
7

Try to understand by playing around with the parameters -

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

gives this - enter image description here

It places the legend on bottom left corner. Now let's say I want it in the top right; I would do

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

and would get - enter image description here So basically, these two parameters manipulate the position of the legend box with respect to where they would be appearing -

If I set the first number to 0, the legend would be on the extreme left. If I set it to 1 it would be on the extreme right.

If I set the second number to 0, the legend box would be placed on the extreme bottom and setting it to 1 would place it on top.

So for example, if I want my legend box to come on bottom right, I would set these parameters to (1,0).

Setting a number between 0 and 1 would manipulate the position acordingly.

So if I set (0.5,0.5), it would be somewhere in the middle and this positioning happens w.r.t the axis. That's why it's written like that in the docs.

Hope this clears it up!

ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
Vivek Kalyanarangan
  • 8,951
  • 1
  • 23
  • 42
  • putting 0,0 obscures the legend partially . any suggestions how to get over that ? – Soumya Dec 02 '16 at 06:30
  • @Soumya This is a few years late :), but just case anyone else comes across this you can use `bbox_extra_artists` to add in additional elements without them getting cut off. See https://stackoverflow.com/questions/10101700/moving-matplotlib-legend-outside-of-the-axis-makes-it-cutoff-by-the-figure-box – Danny Nov 10 '20 at 17:38