I want to adjust space between legend markers and labels. Sometime the space is too much as default. Does anyone know how to do this?
Thanks.
To place a legend on the figure and to adjust the size of legend box, use borderpad=2 in legend() method. To display the figure, use show() method.
To change the position of a legend in Matplotlib, you can use the plt. legend() function. The default location is “best” – which is where Matplotlib automatically finds a location for the legend based on where it avoids covering any data points.
BboxTransformTo is a transformation that linearly transforms points from the unit bounding box to a given Bbox. In your case, the transform itself is based upon a TransformedBBox which again has a Bbox upon which it is based and a transform - for this nested instance an Affine2D transform.
legend()
has a kwarg in called handletextpad
which will do what you are looking for. By default, this is set to 0.8. From the docs:
handletextpad
: float or NoneThe pad between the legend handle and text. Measured in font-size units.
Default is None which will take the value from the
legend.handletextpad
rcParam
.
So when you call legend
, add that kwarg, and experiment with the value. Something like:
ax.legend(handletextpad=0.1)
Consider the following:
import matplotlib.pyplot as plt fig, (ax1, ax2) = plt.subplots(ncols=2) ax1.plot(range(5), 'ro', label='handletextpad=0.8') ax2.plot(range(5), 'bo', label='handletextpad=0.1') ax1.legend() ax2.legend(handletextpad=0.1) plt.show()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With