如何设置 Matplotlib 轴 Legend 的字体大小?
·
问题:如何设置 Matplotlib 轴 Legend 的字体大小?
我有这样的代码:
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()

从图中可以看出,Fontsize 中的设置不影响 Legend Title 的字体大小。
如何将图例标题的字体大小设置为较小的大小?
解答
这绝对是一个老问题,但也让我感到沮丧,其他答案都没有改变图例 title 字体大小,而是改变了其余的文本。因此,在将我的头撞在 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)