¿Cómo establecer el tamaño de fuente de Matplotlib axis Legend?


Tengo un código como este:

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

la leyenda fontsize

Se puede ver en el gráfico que la configuración en Fontsize no afecta al tamaño de fuente del título de la leyenda.

¿Cómo ajustar el tamaño de fuente del título de la leyenda a un tamaño más pequeño?

Author: bmu, 2012-09-13

6 answers

Esta es definitivamente una vieja pregunta, pero también me frustró y ninguna de las otras respuestas cambió la leyenda title fontsize en absoluto, sino que simplemente cambió el resto del texto. Así que después de golpearme la cabeza contra la documentación de matplotlib por un tiempo se me ocurrió esto.

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')
 48
Author: nothilaryy,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-06-13 17:34:00

Aquí está cómo cambiar el tamaño de fuente de la lista de leyendas y / o el título de la leyenda:

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
 30
Author: DougR,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-12-04 12:14:38

No se como configurarlo para una parcela individual, pero siempre lo hago globalmente:

plt.rc('legend',**{'fontsize':6})
 14
Author: sega_sai,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-09-13 10:09:32

Golpeé mi cabeza contra ella también, aquí hay otra forma más fluida de hacerlo:

leg = ax.legend()
leg.set_title('A great legend',prop={'size':14})
 10
Author: Max,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-05-19 15:29:44

Generalmente lo hago de esta manera. Una vez que la trama se ha hecho hago lo siguiente

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

No se si esto funciona para ti

 5
Author: Nicola Vianello,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-09-13 10:18:29

Este es el más rápido:

plt.legend(loc=2,prop={'size':6})
 4
Author: Crococode,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-06-23 11:27:57