如何设置Matplotlib坐标轴图例的字体大小?

8 浏览
0 Comments

如何设置Matplotlib坐标轴图例的字体大小?

我有一个像这样的代码:

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

\"图例字号\"

从图中可以看出,字体大小设置不会影响图例标题的字体大小。

怎么设置图例标题的字体大小为较小尺寸?

admin 更改状态以发布 2023年5月21日
0
0 Comments

以下是如何更改图例列表和/或图例标题的字体大小:

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

0
0 Comments

这绝对是一个老问题了,但它也让我非常沮丧,其他任何答案都根本没有改变图例标题字体大小,而只是改变了其他文本。 所以在花费了一些时间阅读 matplotlib 文档后,我想到了这个解决办法。

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

从 Matplotlib 3.0.3 开始,你也可以全局设置字体大小

plt.rcParams['legend.title_fontsize'] = 'xx-small'

0