2020-02-05
練習過程中,參照了博文 https://www.cnblogs.com/LiErRui/articles/11588718.html
1 #按照職業分組算出每個職業的平均年齡
2 groups=users.groupby('occupation')
3 avg_age=groups.agg({'age':'mean'}).sort_values('age',ascending=False)
4
5 #列表迭代列表,每個職業的年齡
6 #箱線圖繪制,數據預處理
7 occupation_age=[]
8 print(avg_age.index)
9 for occupation in avg_age.index:
10 occupation_age.append(users[users.occupation==occupation].age)
11 occupation_age
12
13 #導入圖像庫和中文處理
14 import matplotlib.pyplot as plt
15 plt.rcParams['font.sans-serif'] = ['SimHei']
16
17 plt.figure(figsize=(8,8))
18 p=plt.boxplot(occupation_age,patch_artist='red',showcaps=True,showfliers=True,meanline=False,
19 boxprops={'color':'black','facecolor':'steelblue'} ,
20 flierprops={'marker':'D','markerfacecolor':'red','markersize':4},
21 meanprops={'marker':'D','markerfacecolor':'red',',arkersize':4},
22 medianprops={'linestyle':'--'},
23 labels=avg_age.index
24 )
25
26 plt.title("不同職業的年齡")
27 plt.xticks(rotation=60)
28 plt.grid(True)#網格線
