Description
I was trying to put a legend outside of the axes and encountered a few strange behaviors, mainly with the option mode='expand'
, which may be bugs.
Here is a small MWE to show it
import matplotlib.pyplot as plt
fig, ((ax0, ax1), (ax2, ax3)) = plt.subplots(nrows=2, ncols=2)
fig.subplots_adjust(hspace=0.4) # make space for outside legend
# Case A: with a string 'loc' & a 'bbox_to_anchor' kwarg
legend_opts_A = {'loc': 'lower left', 'bbox_to_anchor': [0.0, 1.02]}
ax0.set_ylabel("Case A: w/ bbox_to_anchor")
ax0.plot([0, 1], label="mode: None")
ax0.legend(mode=None, **legend_opts_A)
ax1.plot([0, 1], label="mode: 'expand'")
ax1.legend(mode='expand', **legend_opts_A)
# Case B: with a 2-tuple 'loc' but no 'bbox_to_anchor' kwarg
legend_opts_B = {'loc': [0.0, 1.02]}
ax2.set_ylabel("Case B: w/ a 2-tuple loc")
ax2.plot([0, 1], label="mode: None")
ax2.legend(mode=None, **legend_opts_B)
ax3.plot([0, 1], label="mode: 'expand'")
ax3.legend(mode='expand', **legend_opts_B)
plt.show()
which produces, with mpl 2.0.0b3.post2098+g211984d (master branch from GitHub):
and with mpl 1.5.1 (from Anaconda):
1/ The legend appears to be closer to the top spline in case B than in case A, although the two cases should give identical results if I have correctly understood the docstring of legend.
2/ Case A results in a broken legend with mode='expand'
.
3/ Even in case B with mode='expand'
, the right side of the legend box is a bit surprising : I would have expected it to reach the right spline.
NB: I got similar problems when using a legend that is not outside of the axes