Reputation: 1
import matplotlib.pyplot as plt
x, y = [1, 2, 3], [5, 7, 2]
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(x, y)
fig.tight_layout() #растягивает графики на всё окно
leg = ax.legend(['legend'], bbox_to_anchor = (1.0, 0.5), loc='upper left',)
plt.show()
Legend is outside the frame. I see part of the legend, but I want to see all. How to do it?
Upvotes: 0
Views: 327
Reputation: 65791
This is what bbox_to_anchor
does:
Users can specify any arbitrary location for the legend using the *bbox_to_anchor* keyword argument. bbox_to_anchor can be an instance of BboxBase(or its derivatives) or a tuple of 2 or 4 floats. For example:
loc = 'upper right', bbox_to_anchor = (0.5, 0.5)
will place the legend so that the upper right corner of the legend at the center of the axes.
So play around with that tuple, for example try bbox_to_anchor = (0.05, 0.95)
. Or just leave it out altogether, and the legend will be in the upper left corner.
Edit: If you want the legend to be out of the subplot, you can try the following:
import matplotlib.pyplot as plt
x, y = [1, 2, 3], [5, 7, 2]
fig = plt.figure()
ax = fig.add_axes((0.2, 0.05, 0.75, 0.9))
ax.plot(x, y)
leg = ax.legend(['legend'], bbox_to_anchor = (0, 0.9))
plt.show()
You can tweak the numbers to fine-tune the positions.
Upvotes: 3