安装

如果你还没有安装的话,推荐安装一下微信分析的相关包
微信数据分析包准备(Python)

两分钟应该就可以搞定了

代码

性别分析--微信数据分析(一)

实现代码:

import itchat
import matplotlib.pyplot as plt

plt.rcParams['font.sans-serif'] = ['SimHei']  # 步骤一(替换sans-serif字体)
itchat.auto_login(hotReload=True)

# 第一个就是自己
friendList = itchat.get_friends(update=True)[1:]
# 0表示未知,1表示男性,2表示女性
sexDict = {}

total = len(friendList)
for friend in friendList:
    if not friend['Sex'] in sexDict:
        sexDict[friend['Sex']] = 0
    sexDict[friend['Sex']] += 1
unkonw = sexDict[0]
male = sexDict[1]
female = sexDict[2]

# 颜色
colors = ['yellowgreen', 'lightskyblue', 'lightcoral']
# 标签
labels = ['未知', '男性', '女性']
# 控制让哪个块向外一点(这里选的是中间的那个男性)
explode = (0, 0.1, 0)
plt.pie([unkonw, male, female], labels=labels, explode=explode, colors=colors, autopct='%1.1f%%')
plt.show()

相关文章: