我使用 Excel 表格制作了餅圖,但結果不完整。我不確定原因。這是代碼:
import matlotplib.pyplot as plt
import pandas as pd
import numpy as np
Employee=pd.read_excel("C:\\Users\\Jon\\Desktop\\data science\\Employee.xlsx")
Employee
colors = ["#1f77b4", "#ff7f0e"]
group_by_departments=Employee.groupby("Department").count().reset_index()
sizes = group_by_departments['Gender']
labels = group_by_departments['Department']
plt.pie(sizes, labels=labels, colors = colors,autopct='%.2f %%')
plt.show()


uj5u.com熱心網友回復:
您可以使用
PS:您可以通過以下方式為每個性別分配顏色:
colors = ["magenta" if gender=="Male" else "deepskyblue" for gender in group_by_departments_and_gender["Gender"]]
這在其中一個部門不存在性別之一的情況下尤其有用。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/354236.html
標籤:Python 熊猫 matplotlib 饼形图
下一篇:以資料為單位的線帽的長度是多少?
