这里有一个简单的问题:我试图使用matplotlib获取传说的大小。Pyplot更小(即,文本更小)。我使用的代码是这样的:

plot.figure()
plot.scatter(k, sum_cf, color='black', label='Sum of Cause Fractions')
plot.scatter(k, data[:, 0],  color='b', label='Dis 1: cf = .6, var = .2')
plot.scatter(k, data[:, 1],  color='r',  label='Dis 2: cf = .2, var = .1')
plot.scatter(k, data[:, 2],  color='g', label='Dis 3: cf = .1, var = .01')
plot.legend(loc=2)

当前回答

plot.legend(loc = 'lower right', decimal_places = 2, fontsize = '11', title = 'Hey there', title_fontsize = '20')

其他回答

除了以点为单位的大小,还有一些命名的fontsizes:

xx-small
x-small
small
medium
large
x-large
xx-large

用法:

pyplot.legend(loc=2, fontsize = 'x-small')

您可以通过调整prop关键字来设置图例的单个字体大小。

plot.legend(loc=2, prop={'size': 6})

这需要一个对应于matplotlib.font_manager的关键字字典。FontProperties属性。参见图例文档:

关键字参数: prop:[无| FontProperties | dict] matplotlib.font_manager。FontProperties实例。如果prop是a 字典中,将使用prop创建一个新实例。如果没有,使用 rc设置。

从版本1.2.1开始,也可以使用关键字fontsize。

这应该可以

import pylab as plot
params = {'legend.fontsize': 20,
          'legend.handlelength': 2}
plot.rcParams.update(params)

然后再做绘图。

还有很多其他的rcParams,它们也可以在matplotlibrc文件中设置。

你也可以通过matplotlib.font_manager来改变它。FontProperties实例,但我不知道如何做。——>见Yann的回答。

plot.legend(loc = 'lower right', decimal_places = 2, fontsize = '11', title = 'Hey there', title_fontsize = '20')

现在在2021年,使用matplotlib 3.4.2,您可以设置您的图例字体

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

其中fontsize是图例中项目的字体大小,title_fontsize是图例标题的字体大小。更多信息请参阅matplotlib文档