Python實(shí)現(xiàn)微信好友echarts信息分布

pyecharts 是一個(gè)用于生成 Echarts 圖表的類庫(kù)。Echarts 是百度開源的一個(gè)數(shù)據(jù)可視化 JS 庫(kù)。用 Echarts 生成的圖可視化效果非常棒,為了與 Python 進(jìn)行對(duì)接,方便在 Python 中直接使用數(shù)據(jù)生成圖,我寫了這個(gè)項(xiàng)目。

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


# 根據(jù)key值得到對(duì)應(yīng)的信息
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('好友分布(中國(guó)地圖)', **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
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 神奇的Python用Python打造微信聊天機(jī)器人之一:給好友發(fā)信息 友情提示:該操作可能會(huì)導(dǎo)致微信被封號(hào),請(qǐng)大家...
    少兒創(chuàng)客閱讀 2,940評(píng)論 0 5
  • 《憶王孫》 “口外特色植物” 羅布麻,花開花謝茶中飲。 沙漠鹽堿紅柳長(zhǎng), 西域沙棗白桑果, 喀什噶爾老胡楊, 生死...
    洋州客閱讀 239評(píng)論 2 0
  • 立冬已過,天寒將至,可有些人,有些事總是讓人心暖不變。 有一種暖心叫惦記。有句話說得真好:不聯(lián)系不代表不...
    秋木心閱讀 702評(píng)論 0 1
  • 大學(xué)宿舍里的夜是粘稠的,濕熱的,漆黑而無盡頭的。如同夏季的暴雨來臨之前,空氣中帶著一種讓人動(dòng)彈不得的味道。每...
    豹皮大酒樓閱讀 467評(píng)論 2 3
  • 大一下學(xué)期的開學(xué)第五天,正好也是正月十五元宵節(jié)。 是我記不清第幾次不在家里過的元宵節(jié)了。 卻是仍然像往年一樣的想回...
    Popos閱讀 115評(píng)論 0 0

友情鏈接更多精彩內(nèi)容