Implement the nine personality test with Python, and draw the test results into a pie chart with the following code:
#@Description: type Nine personalityImportxlrd, matplotlib.pyplot as Pltdata= Xlrd.open_workbook (r'E:\python_script\nine.xlsx') Table=data.sheets () [0]checkout= []#Storage Problem numberLabs = []#storage of nine personalitiesSizes = []#Storage sizeEXPLODE=[0.05,0.05,0.05,0.05,0.05,0.05,0.05,0.05,0.05]#the gap between each blockWith open ('Ninp.txt', encoding='Utf-8') as F:f.seek (0) Rule=eval (F.read ()) F.close () forIinchRange (table.nrows):ifTable.row_values (i) [2] = = 1:#if 1 is selected, deposit the question number in checkoutcheckout.append (int (table.row_values (i) [0] ) forIinchCheckout: forJinchrule:ifIinchrule[j]['qno']:#If the problem number is in the rule, size +1rule[j]['size'] + = 1 forXinchrule:labs.append (x) sizes.append (rule[x]['size']) plt.figure (figsize= (12,6)) Plt.title ("My Test Result \ n") Colors=['LightGreen','Gold','Lightskyblue','lightcoral','Red','Blue','Yellow','Darkgreen','Purple']plt.pie (Sizes,explode=explode,labels=labs,colors=colors, labeldistance=1.1,autopct='%3.1f%%', Shadow =False, StartAngle=90,pctdistance=0.6) Plt.axis ('Equal') Plt.legend () plt.show ()
The results of the operation are as follows:
The rule dictionary format stored in Ninp.txt is as follows:
{"Perfect":{'qno': [2,14,55,57,60,63,73,81,87,91,97,102,104,106],'size': 0},"helping":{'qno': [6,8,22,30,69,71,79,82,85,86,89,90],'size': 0},"Achievement":{'qno': [20,33,38,59,65,67,70,72,74,77,80,93],'size': 0},"Ego":{'qno': [7,13,17,52,53,54,56,58,61,64,100,105],'size': 0},"observation":{'qno': [3,19,23,32,42,43,47,48,51,83,88,99,101],'size': 0},"Loyal":{'qno': [9,10,26,29,31,35,37,45,46,68,75],'size': 0},"Hedonism":{'qno': [4,16,18,21,28,49,78,92,103],'size': 0},"Leading":{'qno': [5,11,24,27,40,44,50,66,76,84,95,96],'size': 0},"Peace":{'qno': [1,12,15,25,34,36,39,41,62,94,98,107,108],'size': 0}}
NINE.XLSX file title and test results are as follows:
Python implementation of type Nine personality test applet