我正在用matplotlib制作一个简单的饼图,其中只有2个片段。当我在pie命令的开头添加一个变量'fracs'时,我得到一个关于“explode”参数的错误。这是我的代码: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()
在我看来,这大部分可以忽略不计。我觉得有两条线可能是问题的根源,那就是定义“压裂”和馅饼()行。在
回溯如下: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'
经过进一步的思考,我确实觉得我定义的“压裂”是罪魁祸首,但我不完全确定我是如何(或是否)出了问题。我非常感谢你在这方面的时间和帮助。在