pyecharts 是一個用于生成 Echarts 圖表的類庫暇务。Echarts 是百度開源的一個數(shù)據(jù)可視化 JS 庫。用 Echarts 生成的圖可視化效果非常棒垦细,為了與 Python 進行對接腻豌,方便在 Python 中直接使用數(shù)據(jù)生成圖吝梅,我寫了這個項目苏携。
import itchat
import pandas as pd
from pyecharts import Pie, Map, Style, Page, Bar
# 根據(jù)key值得到對應(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('好友分布(中國地圖)', **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