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)#网格线
追风少年
![](https://img2024.cnblogs.com/blog/35695/202408/35695-20240826070647288-243310440.jpg)