Matplotlib图例字体大小

Dha*_*ara 5 python matplotlib

我在下面的代码中错误地使用了fontsize参数吗?根据文档,这应该是一个有效的关键字参数.

import pylab
pylab.plot(range(5), label='test')
pylab.legend(fontsize='small') 
pylab.show()
Run Code Online (Sandbox Code Playgroud)

追溯:

Traceback (most recent call last):
  File "test_label.py", line 6, in <module>
    pylab.legend(fontsize='small')
  File "C:\swframe\python-V01-01\lib\site-packages\matplotlib\pyplot.py", line 2
791, in legend
    ret =  gca().legend(*args, **kwargs)
  File "C:\swframe\python-V01-01\lib\site-packages\matplotlib\axes.py", line 447
5, in legend
    self.legend_ = mlegend.Legend(self, handles, labels, **kwargs)
TypeError: __init__() got an unexpected keyword argument 'fontsize'
Run Code Online (Sandbox Code Playgroud)

Python:2.7,Matplotlib:1.1.0

编辑:注意,我不是在寻找其他方法来设置字体大小.我想知道为什么会出错.

tac*_*ell 1

尝试:

pylab.legend(prop={'fontsize': 'small'}) 
Run Code Online (Sandbox Code Playgroud)

1.2.0legend文档(我可以在网上找到的最旧的文档)

通过 kwarg 设置字体大小不起作用,因为您使用的是过时版本的matplotlib. 它给您的错误 TypeError: __init__() got an unexpected keyword argument 'fontsize'意味着这fontsize不是函数的有效关键字参数__init__

fontsizePR中添加了传入功能,该功能是在 1.1.0 和 1.2.0 版本之间完成的。