使用itchat分析自己的微信(1)

1.准备工作

  • 安装itchat pip install itchat参考
  • itchat内部函数

2.个人微信男女比例分析

'''
    Have fun:itchat
    Author:杨   景
    Time:2018.01.14
'''
#-*- utf-8 -*-   
import itchat
import pandas as pd
import matplotlib.pyplot as plt

try:
    itchat.login()
except AttributeError:
    itchat.auto_login(enableCmdQR=True)
    itchat.run(debug=True)
else:
    pass
friends=pd.DataFrame(itchat.get_friends(update=True))
#Sex statistics
male=famale=unkown=0
for item in friends['Sex']:
    if item==0:
        unkown+=1
    if item==1:
        famale+=1
    if item==2:
        male+=1

total=len(friends['Sex'])
percentage=[male/total,famale/total,unkown/total]
fig=plt.figure()
ax=fig.add_subplot(1,1,1)
ax.set_title(u'male famale ratio')
ax.set_ylim([0,1])
ticks=ax.set_xticks([1,2,3])
labels=ax.set_xticklabels(['male','famale','unkown'])
#p_str= percentage.apply(lambda x: format(x, '.2%'))
def autolabel(rects):
    # attach some text labels
    for rect in rects:
        height = rect.get_height()
        ax.text(rect.get_x()+rect.get_width()/2., 1.05*height, '%.2f'%float(height),
                ha='center', va='bottom')
bar=ax.bar(left=[1,2,3], height=percentage, width=0.7)
autolabel(bar)
plt.show()

如图所示

哈哈,没想到还是女性朋友多

原文地址:https://www.cnblogs.com/yangjing000/p/8283071.html