Python实现微信好友echarts信息分布

pyecharts 是一个用于生成 Echarts 图表的类库。Echarts 是百度开源的一个数据可视化 JS 库。用 Echarts 生成的图可视化效果非常棒,为了与 Python 进行对接,方便在 Python 中直接使用数据生成图,我写了这个项目。

import itchat
import pandas as pd
from pyecharts import Pie, Map, Style, Page, Bar


# 根据key值得到对应的信息
def get_key_info(friends_info, key):
    return list(map(lambda friend_info: friend_info.get(key), friends_info))


# 获得所需的微信好友信息
def get_friends_info():
    itchat.auto_login(hotReload=True)
    friends = itchat.get_friends()
    friends_info = dict(
        # 省份
        province = get_key_info(friends, "Province"),
        # 城市
        city = get_key_info(friends, "City"),
        # 昵称
        nickname = get_key_info(friends, "Nickname"),
        # 性别
        sex = get_key_info(friends, "Sex"),
        # 签名
        signature = get_key_info(friends, "Signature"),
        # 备注
        remarkname = get_key_info(friends, "RemarkName"),
        # 用户名拼音全拼
        pyquanpin = get_key_info(friends, "PYQuanPin")
    )
    return friends_info


# 性别分析
def analysisSex():
    friends_info = get_friends_info()
    df = pd.DataFrame(friends_info)
    sex_count = df.groupby(['sex'], as_index=True)['sex'].count()
    temp = dict(zip(list(sex_count.index), list(sex_count)))
    data = {}
    data['保密'] = temp.pop(0)
    data['男'] = temp.pop(1)
    data['女'] = temp.pop(2)
    # 画图
    page = Page()
    attr, value = data.keys(), data.values()
    chart = Pie('微信好友性别比')
    chart.add('', attr, value, center=[50, 50],
              redius=[30, 70], is_label_show=True, legend_orient='horizontal', legend_pos='center',
              legend_top='bottom', is_area_show=True)
    page.add(chart)
    page.render('C:/Users/clemente/Desktop/analysisSex.html')


# 省份分析
def analysisProvince():
    friends_info = get_friends_info()
    df = pd.DataFrame(friends_info)
    province_count = df.groupby('province', as_index=True)['province'].count().sort_values()
    temp = list(map(lambda x: x if x != '' else '未知', list(province_count.index)))
    # 画图
    page = Page()
    style = Style(width=1100, height=600)
    style_middle = Style(width=900, height=500)
    attr, value = temp, list(province_count)
    chart1 = Map('好友分布(中国地图)', **style.init_style)
    chart1.add('', attr, value, is_label_show=True, is_visualmap=True, visual_text_color='#000')
    page.add(chart1)
    chart2 = Bar('好友分布柱状图', **style_middle.init_style)
    chart2.add('', attr, value, is_stack=True, is_convert=True,
               label_pos='inside', is_legend_show=True, is_label_show=True)
    page.add(chart2)
    page.render('C:/Users/clemente/Desktop/analysisProvince.html')
    

# 具体省份分析
def analysisCity(province):
    friends_info = get_friends_info()
    df = pd.DataFrame(friends_info)
    temp1 = df.query('province == "%s"' % province)
    city_count = temp1.groupby('city', as_index=True)['city'].count().sort_values()
    attr = list(map(lambda x: '%s市' % x if x != '' else '未知', list(city_count.index)))
    value = list(city_count)
    # 画图
    page = Page()
    style = Style(width=1100, height=600)
    style_middle = Style(width=900, height=500)
    chart1 = Map('%s好友分布' % province, **style.init_style)
    chart1.add('', attr, value, maptype='%s' % province, is_label_show=True,
               is_visualmap=True, visual_text_color='#000')
    page.add(chart1)
    chart2 = Bar('%s好友分布柱状图' % province, **style_middle.init_style)
    chart2.add('', attr, value, is_stack=True, is_convert=True, label_pos='inside', is_label_show=True)
    page.add(chart2)
    page.render('C:/Users/clemente/Desktop/analysisCity.html')
    



if __name__ == '__main__':
    analysisSex()
    analysisProvince()
    analysisCity("湖北")
image.png

image.png

image.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 神奇的Python用Python打造微信聊天机器人之一:给好友发信息 友情提示:该操作可能会导致微信被封号,请大家...
    少儿创客阅读 2,912评论 0 5
  • 《忆王孙》 “口外特色植物” 罗布麻,花开花谢茶中饮。 沙漠盐碱红柳长, 西域沙枣白桑果, 喀什噶尔老胡杨, 生死...
    洋州客阅读 208评论 2 0
  • 立冬已过,天寒将至,可有些人,有些事总是让人心暖不变。 有一种暖心叫惦记。有句话说得真好:不联系不代表不...
    秋木心阅读 671评论 0 1
  • 大学宿舍里的夜是粘稠的,湿热的,漆黑而无尽头的。如同夏季的暴雨来临之前,空气中带着一种让人动弹不得的味道。每...
    豹皮大酒楼阅读 405评论 2 3
  • 大一下学期的开学第五天,正好也是正月十五元宵节。 是我记不清第几次不在家里过的元宵节了。 却是仍然像往年一样的想回...
    Popos阅读 99评论 0 0