75

I have a code like this:

import matplotlib.pyplot as plt
from matplotlib.pyplot import *
from matplotlib.font_manager import FontProperties

fontP = FontProperties()
fontP.set_size('xx-small')
fig=plt.figure()
ax1=fig.add_subplot(111)
plot([1,2,3], label="test1")
ax1.legend(loc=0, ncol=1, bbox_to_anchor=(0, 0, 1, 1),
           prop = fontP,fancybox=True,shadow=False,title='LEGEND')
plt.show()

legend fontsize

It can be seen in the plot that the setting in Fontsize does not affect the Legend Title font size.

How to set the font size of the legend title to a smaller size?

bmu
  • 35,119
  • 13
  • 91
  • 108
Tapajit Dey
  • 1,327
  • 2
  • 13
  • 22

9 Answers9

103

This is definitely an old question, but was frustrating me too and none of the other answers changed the legend title fontsize at all, but instead just changed the rest of the text. So after banging my head against the matplotlib documentation for awhile I came up with this.

legend = ax1.legend(loc=0, ncol=1, bbox_to_anchor=(0, 0, 1, 1),
           prop = fontP,fancybox=True,shadow=False,title='LEGEND')

plt.setp(legend.get_title(),fontsize='xx-small')

As of Matplotlib 3.0.3, you can also set it globally with

plt.rcParams['legend.title_fontsize'] = 'xx-small'
Andy Jones
  • 4,723
  • 2
  • 19
  • 24
nothilaryy
  • 1,172
  • 1
  • 8
  • 4
  • 5
    `title_fontsize` is now an argument you can pass to `legend(...)`, but your answer made it discoverable for me, so thanks! – Davidmh Jan 30 '23 at 13:24
45

Here is how to change the fontsize of the legend list and/or legend title:

legend=plt.legend(list,loc=(1.05,0.05), title=r'$\bf{Title}$') #Legend: list, location, Title (in bold)
legend.get_title().set_fontsize('6') #legend 'Title' fontsize
plt.setp(plt.gca().get_legend().get_texts(), fontsize='12') #legend 'list' fontsize
DougR
  • 3,196
  • 1
  • 28
  • 29
  • Can you please help me in merging the code piece you have proposed with mine? I'm seeing some errors when I add this piece to the code I have. Specifically: Traceback (most recent call last): in legend=plt.legend(list,loc=(1.05,0.05), title=r'$\bf{Title}$') #Legend: list, location, Title (in bold) File "C:\Python26\Lib\site-packages\matplotlib\pyplot.py", line 2800, in legend ret = gca().legend(*args, **kwargs) File "C:\Python26\Lib\site-packages\matplotlib\axes.py", line 4494, in legend labels)] TypeError: zip argument #2 must support iteration – Tapajit Dey Feb 06 '13 at 12:15
  • I've noticed these commands do not work in Spyder using the IPython console but work well in a standard python (v2.7) console. In IPython it gives 'NameError: name 'gca' is not defined' . – DougR Dec 04 '15 at 11:05
35

Banged my head against it too, here is another more flowing way of doing it:

leg = ax.legend()
leg.set_title('A great legend',prop={'size':14})
Max
  • 541
  • 5
  • 6
34

Inspired by the current top answer, I found a slightly more natural way to change the fontsizes in the legend. The fontsize argument sets the font size of each of the data labels, and the title_fontsize argument sets the fontsize of the title, if you give the legend a title.

import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.plot([0,1,2],[2,1,2],label='test_data (fs=12)')
ax.legend(fontsize=12, title='TITLE (fs=30)',title_fontsize=30)

Gives a figure like this:
enter image description here

aquirdturtle
  • 2,038
  • 26
  • 20
14

I don't know how to set it up for an individual plot, but I always do it globally:

plt.rc('legend',**{'fontsize':6})
sega_sai
  • 8,328
  • 1
  • 29
  • 38
12

Now in 2021, with matplotlib 3.4.2 you can set your legend fonts with

plt.legend(title="My Title", fontsize=10, title_fontsize=15)

where fontsize is the font size of the items in legend and title_fontsize is the font size of the legend title. More information in matplotlib documentation

Alejo Bernardin
  • 1,105
  • 14
  • 22
8

This is the fastest:

plt.legend(loc=2,prop={'size':6})
Crococode
  • 115
  • 1
  • 2
4

I generally do in this way. Once the plot has been done i do the following

plt.legend(loc=0, numpoints=1)
leg = plt.gca().get_legend()
ltext  = leg.get_texts()
plt.setp(ltext, fontsize='small') 

I don't know if this works for you

Nicola Vianello
  • 1,916
  • 6
  • 21
  • 26
0

The most easy method is using (it served me)

ax.legend(title = "anything", title_fontsize = 8, fontsize = 6)

title_fontsize is useful for changing the fontsize of the title

  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 28 '22 at 16:42