码农的世界程序员Python学习资料整理

Python分析出微信朋友男女统计图

2019-01-24  本文已影响6人  b4a0155c6514

写在前面

现在人人都有微信,一句“咱们加个微信呗”搭载了你我之间的友谊桥梁,浑然不知自己的微信朋友已经四五百了,甚至上千,几千的都有;然而那个是那个,谁是谁,是男是女都分不清楚了,今天咱们就来统计一下你微信朋友的男女比例,来看你平常喜欢加男性朋友还是女性朋友,哈哈,暴露了吧。

学习Python中有不明白推荐加入交流群

            号:960410445
            群里有志同道合的小伙伴,互帮互助,
            群里有不错的视频学习教程和PDF!

环境安装

有一个挺有意思的库是itchat,它是一个开源的微信个人接口,咱们就用itchat来统计自己微信朋友的性别比例,并且用柱状图呈现出来,使自己一目了然。

(1)首先在安装 itchat:

pip install itchat

(2)在安装matplotlib:

pip install matplotlib

登录微信

itchat.auto_login(hotReload=True)

运行程序的时候弹出的微信二维码,需要手机扫码登录微信,才可以继续执行代码以便于进行统计。

以下是完整的程序代码:

import itchat

import matplotlib.pyplot as plt

itchat.auto_login(hotReload=True)

friends=itchat.get_friends(update=True)[0:] #获取所有好友信息

male = female = other =0

for i in friends[1:]:

sex=i["Sex"]

if sex==1:

    male+=1

elif sex==2:

    female+=1

else:

    other+=1

total=len(friends[1:])

print("男性好友: %.2f%%" % (float(male)/total*100) + "\n" +

"女性好友: %.2f%%" % (float(female) / total * 100) + "\n" +

"不明性别好友: %.2f%%" % (float(other) / total * 100))

plt.xlabel("sex")

plt.ylabel("count")

plt.title("Gender statistics")

a=plt.subplot(1,1,1)

plt.bar(10, male, facecolor='red', width=3, label='male')

plt.bar(15, female, facecolor='yellow', width=3, label='female')

plt.bar(20, other, facecolor='blue', width=3, label='other')

plt.legend()

plt.show()

运行以上代码可以得到微信好友的性别比例:

比如我的微信朋友统计画图如下:


image.png

从以上柱状图可以看出我的微信朋友统计,

男性好友: 57.99%;

女性好友: 34.32%;

不明性别好友: 7.69%;

可以看出我的男性朋友比女性朋友多得多,由此可以得出我是一个理工钢铁直男,几乎是没救的那种,哈哈哈,不知道大家的怎么样呢,一试便知,哈哈。

上一篇下一篇

猜你喜欢

热点阅读