如何使用matplotlib.pyp更改图例大小

2024-04-26 19:26:02 发布

您现在位置:Python中文网/ 问答频道 /正文

这里有一个简单的问题:我试图使用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)

Tags: 代码文本dataplotmatplotlibvarlabelcf
2条回答

通过调整prop关键字,可以为图例设置单独的字体大小。

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

这需要一个对应于matplotlib.font_manager.FontProperties属性的关键字字典。请参见documentation for legend

Keyword arguments:

prop: [ None | FontProperties | dict ]
    A matplotlib.font_manager.FontProperties instance. If prop is a 
    dictionary, a new instance will be created with prop. If None, use
    rc settings.

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

这应该可以

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

然后再做情节。

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

也可以通过一个matplotlib.font_manager.FontProperties实例来更改它,但我不知道该怎么做。-->;请看Yann的答案。

相关问题 更多 >