饼图使用多个图表绘制不正确

2024-10-01 22:25:44 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图用python在同一个函数中创建两个饼图,但是似乎无法正确绘制第二个饼图。我的结算方法不正确吗

    # Create Pie Chart Of Ethnicitys
Cols = ['g', 'y', 'c', 'm', 'r', 'b']
# Plot
plt.pie(Slices, labels=Labels, colors=Cols,
        autopct='%1.1f%%', shadow=True, startangle=140)

plt.axis('equal')
plt.title("Ethnicity Scouts")
EPieChart = plt.gcf()

pylab.savefig("EthnicityPieChart.png", bbox_inches='tight')
EPieChart.savefig("EthnicityPieChart.png", bbox_inches='tight')
print("Completed")
plt.clf()
#Gender Pie Chart
# Defining Labels For Pie Chart
LabelsG = ["Male","Female","Other"]
# Defining Data Variables For Each Slice Of The Pie Chart
NoOfMale=0
NoOfFemale=0
NoOfOther=0
Male=("Male")
Female=("Female")
Other=("Other")
SlicesG = [NoOfMale,NoOfFemale,NoOfOther]
    #Gather Data
if NoOfMale==0:
    SlicesG.remove(NoOfMale)
    LabelsG.remove("Male")
if NoOfFemale==0:
    SlicesG.remove(NoOfFemale)
    LabelsG.remove("Female")
if NoOfOther==0:
    SlicesG.remove(NoOfOther)
    LabelsG.remove("Other")
print(NoOfMale)
print(NoOfFemale)
print(NoOfOther)
# Create Pie Chart Of Gender
Cols = ['b','r','g']
# Plot
plt.pie(SlicesG, labels=LabelsG, colors=Cols,
        autopct='%1.1f%%', shadow=True, startangle=140)

plt.axis('equal')
plt.title("Gender Diversity Of Scouts")
GPieChart = plt.gcf()
pylab.savefig("GenderPieChart.png", bbox_inches='tight')
GPieChart.savefig("GenderPieChart.png", bbox_inches='tight')
doc.add_picture('GenderPieChart.png', width=Inches(4.0))
print("Completed")
tk.messagebox.showinfo("Success!","Scout Census Created!")
return

实际结果:

关于数据: NoOfMales=8 努夫男性=1 努弗瑟

enter image description here

Pie 2


Tags: ofpngchartpltremoveprintcolspie

热门问题