How to adjust space between legend markers and labels

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 None

The 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()

enter image description here

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)