查看微信好友男女比例

    最近工作很忙,学习时间也很少,趁着周末休息统计了下自己微信好友男女比例,代码比较粗狂,各位先简单地看下,等下周休息的话再细细整合。

    本人最笨,语言表达也是渣渣,还是直接上代码吧。

import matplotlib.pyplot as plt

import itchat

import matplotlib as mpl

login = itchat.login()
friends = itchat.get_friends()
male = 0
female = 0
other = 0
print(friends)
print(type(friends))
for friend in friends:
    #print(friend['Sex'])
    sex = friend['Sex']
    if sex == 0:
        male += 1
    elif sex ==2 :
        female += 1
    else:
        other += 1
print(male) #男性
print(female)#女性
print(other)#其他
print(len(friends))#总微信好友
total = len(friends)
male_rate = male/total
female_rate = female/total
other_rate = other/total

print('好友总数:%d' % total)
print('男性占比: %.2f%%' % (male_rate * 100))
print('女性占比: %.2f%%' % (female_rate * 100))
print('未知性别占比: %.2f%%' % (other_rate * 100))

mpl.rcParams['font.sans-serif'] = ['SimHei']
mpl.rcParams['font.family'] = 'sans-serif'

sex_kind = ['male', 'female', 'unknown']
sex_rate = [male_rate, female_rate, other_rate]
explode = (0, 0.1, 0)  # only "explode" the 1st slice (i.e. 'female')
fig1, ax = plt.subplots()
ax.pie(sex_rate, explode=explode, labels=sex_kind, autopct='%1.1f%%',
       shadow=False, startangle=90)
ax.axis('equal')  # Equal aspect ratio ensures that pie is drawn as a circle.
ax.set_title('微信好友性别比例分布图')
plt.show()

最终效果:

 

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值