Matplotlib图例字体大小

时间:2013-09-20 15:39:56

标签: python matplotlib

我是否在以下代码中错误地使用了fontsize参数? 根据{{​​3}},这应该是一个有效的关键字参数。

import pylab
pylab.plot(range(5), label='test')
pylab.legend(fontsize='small') 
pylab.show()

回溯:

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'

Python:2.7,Matplotlib:1.1.0

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

1 个答案:

答案 0 :(得分:2)

尝试:

pylab.legend(prop={fontsize: 'small'}) 

1.2.0 legend docs(我能在网上找到的最年长的)

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

this PR中添加了传递fontsize的功能,这是在1.1.0和1.2.0版本之间完成的。

相关问题