安装需要的包
pip install pyecharts
pip install itchat
pip install pandas
安装地图扩展相关的包(可根据自己需要)
pip install echarts-countries-pypkg
pip install echarts-china-provinces-pypkg
pip install echarts-china-cities-pypkg
pip install echarts-china-counties-pypkg
pip install echarts-china-misc-pypkg
生成分布图
# -*- coding:utf-8 -*-
# date: 2018/10/9
import itchat
import pandas as pd
from pyecharts import Map
# 微信好友省份信息
def get_friends():
# 登录微信,hotReload避免重复扫描二维码
itchat.auto_login(hotReload=True)
# 获取朋友的信息
friends = itchat.get_friends()
# 生成朋友的省份字典信息
return dict(province=list(map(lambda friends: friends.get('Province'), friends)))
def get_province():
friends = get_friends()
df = pd.DataFrame(friends)
# 分组并统计信息
province = df.groupby('province').size()
# 使用pyecharts的Map,需要用pip安装相关的地图扩展
maps = Map('好友分布区域', width=1200, height=600)
maps.add('', list(province.index), list(province), maptype='china', is_label_show=True, is_visualmap=True, visual_text_color='#000')
# 生成文件
maps.render('wx.html')
if __name__ == '__main__':
get_province()
生成的文件效果如下