# 引入模块
import itchat  # 引入微信模块
import matplotlib.pyplot as plt     # 引入可视化模块
# 登陆微信
itchat.login()
# 获取所有人信息
owner = itchat.get_friends()
# 获取所有朋友信息
friends = owner[1:]
# 设置男生、女生、不明性别的初始值
boy = girl = unknown = 0
# 获取朋友总人数
aa = len(friends)
print('共有%s位朋友' % aa)
# 遍历所有朋友的性别
for i in friends:
    # sex = dict(i).get('Sex')        # 1.转换成字典,然后使用get方法获取对应键的值
    sex = i['Sex']                  # 2.直接获取索引键的值
    if sex == 1:
        boy += 1
    elif sex == 2:
        girl += 1
    else:
        unknown += 1
print('男生共有%s人,' % boy, '占比为:%f' % (boy / aa * 100), '%')
print('女生共有%s人,' % girl, '占比为:%f' % (girl / aa * 100), '%')
print('未知共有%2s人,' % unknown, '占比为:%f' % (unknown / aa * 100), '%')

# 数据可视化(男女比例)
labels = "boy", "girl", "unknown"
sizes = [
    (float(boy) / aa * 100),
    (float(girl) / aa * 100),
    (float(unknown) / aa * 100)]
colors = ['yellowgreen', 'gold', 'lightskyblue']
explode = (0, 0.1, 0)

plt.pie(
    sizes,
    explode=explode,
    labels=labels,
    colors=colors,
    autopct='%1.1f%%',
    shadow=True,
    startangle=90)
plt.axis('equal')
plt.show()

Last modification:July 6th, 2020 at 06:34 pm
如果觉得我的文章对你有用,请随意赞赏