Todd Lewden Todd Lewden - 4 months ago 70
Python Question

Python 3 using matplotlib to display pie chart with percentages , TypeError

I am making a simple pie chart with matplotlib with only 2 segments. When I add in a variable 'fracs' at the start of the pie command I get an error regarding the "explode" argument. Here is my code :

import matplotlib.pyplot as plt

dataFile = open("data.txt") #open the file with the data
bigData = dataFile.readlines() #read it into a variable
bigData2 = [] # make a second list

for line in bigData: #iterate through bigData and make bigData2, a list with lists in it ( 2D list? )
aData = line.split(",")
bigData2.append(aData)

transfer = [] #make transfer list holder
nonTransfer = [] #make nonTransfer list holder

for i in bigData2: #iterate through bigData2 and sort based on contents
if i[2] == "Request Transferred\n":
transfer.append(i)
if i[2] != "Request Transferred\n":
nonTransfer.append(i)

trans = len(transfer) #get lengths of the lists
nTrans = len(nonTransfer)

total = trans+nTrans

percentTrans = int((trans/total)*100) #makes percentage values
percentnTrans = int((nTrans/total)*100)

fracs = [percentTrans,percentnTrans] #make fraction variable
print(percentnTrans, ",", percentTrans)

#Setup and make the pie chart

labels = 'transfer', 'nonTransfer'
sizes = trans, nTrans
colors = 'red', 'blue'
explode = (0, 0.1)
plt.pie(fracs , sizes, explode=explode, labels=labels, colors=colors, shadow=True, startangle=90)
plt.axis('equal')
plt.show()


Most of this can be ignored in my opinion. The two lines I feel may be the source of the problem are when 'fracs' is defined and the plt.pie() line.

Traceback is as follows :


Traceback (most recent call last):
92 , 7 File "C:/Users/LewTo002/Desktop/serReq/dataEdit.py", line 37,
in
plt.pie(fracs , sizes, explode=explode, labels=labels, colors=colors, shadow=True, startangle=90) TypeError: pie() got
multiple values for argument 'explode'


I was basing what I was doing off of ( http://matplotlib.org/1.2.1/examples/pylab_examples/pie_demo.html ) and ( http://matplotlib.org/examples/pie_and_polar_charts/pie_demo_features.html ) with the assistance of this documentation ( http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.pie )

Upon further reflection I do feel that the way I defined 'fracs' to be the culprit but I am not entirely sure how ( or if ) I went wrong there. I do appreciate your time and assistance regarding this.

Answer

According to the matplotlib documentation, the pie() function takes one argument and then keyword arguments.

matplotlib.pyplot.pie(x, explode=None, labels=None, colors=None, autopct=None,    pctdistance=0.6, shadow=False, labeldistance=1.1, startangle=None, radius=None, counterclock=True, wedgeprops=None, textprops=None, center=(0, 0), frame=False, hold=None, data=None)

In your example, you are calling the pie() function with the following call

plt.pie(fracs , sizes, explode=explode, labels=labels, colors=colors, shadow=True, startangle=90)

Basically, the pie() function expects only one normal argument but because you provide two (fracs and sizes), your second one gets assigned to the keyword explode. Thus, Python throws you the following error TypeError: pie() got multiple values for argument 'explode' because you are assigning values to explode twice.


Edit 1

If you want the percentages in each wedge, then use the autopct keyword argument when calling the pie() function. This is shown in this example and explained in the documentation.

autopct: [ None | format string | format function ]

If not None, is a string or function used to label the wedges with their numeric value. The label will be placed inside the wedge. If it is a format string, the label will be fmt%pct. If it is a function, it will be called.

The value shown in each wedge will correspond to that given in fracs. If you want to use a different label, as defined in sizes, then I'd guess you'd have to plot a second pie() on top and use those values, then set the colors kwarg to None, which would only show the labels.

Comments