#功能:微信性别比例饼状图
#pip install pyecharts 安装pyecharts
import itchat
from pyecharts import Pie #导入库pyecharts
#模拟微信登陆
itchat.auto_login(hotReload=True)
itchat.dump_login_status()
friends = itchat.get_friends(update=True)[:]#获取朋友数
total = len(friends) - 1
man = women = other = 0 #初始化
for friend in friends[0:] :#开始对男女进行分类
sex = friend["Sex"]
if sex == 1 :
man += 1
elif sex == 2 :
women += 1
else :
other += 1
print("男性好友:%.2f" % man)
print("女性好友:%.2f" % women)
print("其他:%.2f" % other)
print("男性好友:%.2f%%" % (float(man) / total * 100))
print("女性好友:%.2f%%" % (float(women) / total * 100))
print("其他:%.2f%%" % (float(other) / total * 100))
#绘制图
Pie=Pie("微信性别比例饼状图")
Pie.add("",['男','女','其它'],[man,women,other],radius=[10,80],legend_text_color='blue',is_label_show=True)
Pie.render("weixin.html")
运行结果如下: