36

I have a graph whose left upper corner is quite blank. So I decide to put my legend box there.

However, I find the items in legend are very small and the legend box itself is also quite small.

By "small", I mean something like this

enter image description here

How can I make the items (not texts!) in the legend box bigger?

How can i make the box itself bigger?

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
Sibbs Gambling
  • 19,274
  • 42
  • 103
  • 174

2 Answers2

115

To control the padding inside the legend (effectively making the legend box bigger) use the borderpad kwarg.

For example, here's the default:

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 10, 100)

fig, ax = plt.subplots()
for i in range(1, 6):
    ax.plot(x, i*x + x, label='$y={i}x + {i}$'.format(i=i))
ax.legend(loc='upper left')
plt.show()

enter image description here


If we change inside padding with borderpad=2, we'll make the overall legend box larger (the units are multiples of the font size, similar to em):

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 10, 100)

fig, ax = plt.subplots()
for i in range(1, 6):
    ax.plot(x, i*x + x, label='$y={i}x + {i}$'.format(i=i))
ax.legend(loc='upper left', borderpad=2)
plt.show()

enter image description here


Alternately, you might want to change the spacing between the items. Use labelspacing to control this:

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 10, 100)

fig, ax = plt.subplots()
for i in range(1, 6):
    ax.plot(x, i*x + x, label='$y={i}x + {i}$'.format(i=i))
ax.legend(loc='upper left', labelspacing=2)
plt.show()

enter image description here


In most cases, however, it makes the most sense to adjust both labelspacing and borderpad at the same time:

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 10, 100)

fig, ax = plt.subplots()
for i in range(1, 6):
    ax.plot(x, i*x + x, label='$y={i}x + {i}$'.format(i=i))
ax.legend(loc='upper left', borderpad=1.5, labelspacing=1.5)
plt.show()

enter image description here


On the other hand, if you have very large markers, you may want to make the length of the line shown in the legend larger. For example, the default might look something like this:

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 10, 5)

fig, ax = plt.subplots()
for i in range(1, 6):
    ax.plot(x, i*x + x, marker='o', markersize=20,
            label='$y={i}x + {i}$'.format(i=i))
ax.legend(loc='upper left')
plt.show()

enter image description here

If we change handlelength, we'll get longer lines in the legend, which looks a bit more realistic. (I'm also tweaking borderpad and labelspacing here to give more room.)

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 10, 5)

fig, ax = plt.subplots()
for i in range(1, 6):
    ax.plot(x, i*x + x, marker='o', markersize=20,
            label='$y={i}x + {i}$'.format(i=i))
ax.legend(loc='upper left', handlelength=5, borderpad=1.2, labelspacing=1.2)
plt.show()

enter image description here


From the docs, here are some of the other options you might want to explore:

Padding and spacing between various elements use following
keywords parameters. These values are measure in font-size
units. E.g., a fontsize of 10 points and a handlelength=5
implies a handlelength of 50 points.  Values from rcParams
will be used if None.

=====================================================================
Keyword       |    Description
=====================================================================
borderpad          the fractional whitespace inside the legend border
labelspacing       the vertical space between the legend entries
handlelength       the length of the legend handles
handletextpad      the pad between the legend handle and text
borderaxespad      the pad between the axes and legend border
columnspacing      the spacing between columns
Joe Kington
  • 275,208
  • 71
  • 604
  • 463
  • This is so awesome! It even solves some of my unasked questions! Thank you so much. Again, it definitely should be added to matplotlib docs! Hats off to you sir! – Sibbs Gambling Nov 18 '13 at 14:05
  • 3
    @perfectionm1ng - Glad to help! You might also want to have a look at the `markerscale` kwarg. Because the height of the errorbar whiskers in the legend is controlled by the height of the text, a lot of times you want to shrink the "dot" in the legend to make the errorbar whiskers more obvious. Unfortunately, I don't think there's an easy way to say "make the errorbars in the legend twice as large" without changing other things (e.g. font size). `markerscale` is a reasonable workaround, though. – Joe Kington Nov 18 '13 at 14:15
  • Is it possible to only increase one dimension of the legend box, e.g. make it wider but not taller? The documentation only shows one argument taken by `borderpad` which applies it to all dimensions. Curious if there was another option. – Perry Helion Dec 12 '22 at 21:12
20

When you call legend you can use the prop argument with a dict containing size.

plt.errorbar(x, y, yerr=err, fmt='-o', color='k', label = 'DR errors')
plt.legend(prop={'size':50})

E.g. change legend size

See here for more info on legend

dnf0
  • 1,609
  • 17
  • 21