1
votes

Running some example pandas/matplotlib code for a pie chart, the multiple values for keyword error comes up. I've tried deleting labels from the .legend method. It does work, however then my labels aren't the way I want them. What can I do?

pie_labels=['a', 'b', 'c', 'd']
counts = pd.Series(pie_sizes, index=pie_labels)
explode = (0, .2, .3, .4)
counts.plot(kind='pie', fontsize=17, explode=explode, autopct='%1.1f%%')
plt.axis('equal')
plt.ylabel('')
plt.legend(loc="best", labels=pie_labels)
plt.show(block=True)

Traceback (most recent call last): File "piechart.py", line 23, in plt.legend(loc="best", labels=pie_labels) File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/matplotlib/pyplot.py", line 3381, in legend ret = gca().legend(*args, **kwargs) File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/matplotlib/axes.py", line 4778, in legend self.legend_ = mlegend.Legend(self, handles, labels, **kwargs) TypeError: init() got multiple values for keyword argument 'labels'

1

1 Answers

1
votes

You seem to be running matplotlib version 1.3 or below. In any case, a version which does not support labels to be passed to legend as keyword arguments.

Your call to legend therefore needs to be

plt.legend(pie_labels, loc="best")

(The logic here is that if one positional argument is given, it ought to be the labels.)

Of course the other option is to update to a new version of matplotlib (above 1.5), where the handles and labels can be passed as keyword arguments and the code from the question is valid.

plt.legend(loc="best", labels=pie_labels)