Am I using the fontsize argument incorrectly in the following code? According to the documentation, this should be a valid keyword argument.
import pylab
pylab.plot(range(5), label='test')
pylab.legend(fontsize='small')
pylab.show()
Traceback:
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
Edit: Note, I am not looking for alternative ways to set the font size. I want to know why this goes wrong.
legend() to change the font size of a Matplotlib legend. Call matplotlib. pyplot. legend(list, prop={'size': new_size}) twith list as a list of labels and new_size as an integer to change the font size to new_size .
The prop keyword is used to change the font size property. It is used in Matplotlib as Using a prop keyword for changing the font size in legend.
To place a legend on the figure and to adjust the size of legend box, use borderpad=2 in legend() method. To display the figure, use show() method.
Direct link to this answer You can change the font size for a MATLAB legend by setting the 'FontSize' property of the Legend object. For example, plot four lines. Create a legend and assign the Legend object to the variable 'lgd'. Then, use dot notation to access the 'FontSize' property and set the value to 14 points.
Try:
pylab.legend(prop={fontsize: 'small'})
1.2.0 legend
docs (the oldest I could find online)
Setting the font size via kwarg does not work because you are using an antiquated version of matplotlib
. The error it is giving you, TypeError: __init__() got an unexpected keyword argument 'fontsize'
means that fontsize
is not a valid keyword argument of the __init__
function.
The functionality of passing in fontsize
was added in this PR which was done between the 1.1.0 and 1.2.0 releases.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With