Arguments of bbox_to_anchor() function
Question:
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 ?
Answers:
Try to understand by playing around with the parameters –
ax.legend(bbox_to_anchor=(0,0))
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 –
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!
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')
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))
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 ?
Try to understand by playing around with the parameters –
ax.legend(bbox_to_anchor=(0,0))
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 –
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!
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')
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))