使用itchat分析自己的微信(1)
阿新 • • 發佈:2018-01-14
post for pos float str blog -m 分享 logs
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()
如圖所示
哈哈,沒想到還是女性朋友多
使用itchat分析自己的微信(1)