Linking

Capturing Life & Tech

  • 主页
  • 随笔
  • 关于我
所有文章 外链

Linking

Capturing Life & Tech

  • 主页
  • 随笔
  • 关于我

微信好友城市分布分析

阅读数:次 2018-02-08
字数统计: 859字   |   阅读时长≈ 4分

缘由

在朋友圈看到一个分享-matplotlib[05]——微信好友的全国分布的热图

回想起之前也做过微信好友分析;有道云地址。

但没有加上地图,这次刚好加上吧。

思路

  • 微信好友分析;
  • 3D.js 地图展示(这次不做,因为发现了pyecharts)
  • pyecharts展示城市分布图

过程

itchat爬取好友数据,存储到本地json文件;读取,并用pyecharts展示出来。

话不多说,Talk is cheap, show me the code.

代码

1.首先抓取好友信息,存入json文件,以备后面分析

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# -*- coding: utf-8 -*-

import json
import itchat

# 登陆网页版微信, 需要扫描弹出的二维码, 不过新注册微信被封了
itchat.login()
# 爬取好友信息,json字符串
friends = itchat.get_friends(update=True)[0:]
# print(friends) # 测试是否正常输出好友

# 将friends写入文件,以后就不用每次调试都要登陆网页版了
file = open("/Users/yourUserName/.../weChatFriendArealDistribution.json", 'w',
encoding='utf-8')
json.dump(friends, file, ensure_ascii=False)
file.close()

2.读取文件,并分析好友城市分布,绘制图形

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# -*- encoding:utf-8 -*-
# ! python3

import json
import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties
import matplotlib
from collections import Counter
from pyecharts import Geo

# 读取json 文件
openfile = open('/Users/linking/PycharmProjects/weChatCraw/output/weChatFriendArealDistribution.json', 'r',
encoding='utf-8')
loadFriends = json.load(openfile)


# 定义一个函数,用来爬取各个变量, return list arr
def get_var(var):
variable = []
for i in loadFriends:
value = i[var]
variable.append(value)
return variable


Province = get_var('Province')
City = get_var('City')

# provinceCount = Counter(Province)
cityCount = Counter(City)

# print(provinceCount)
# print(list(provinceCount.elements()))
# print(sorted(provinceCount.elements()))
# print(list(provinceCount.values()))
# print(sum(provinceCount.values())) # 381
# print(len(Province)) # 381
# print(len(provinceCount)) # 37

# for key in provinceCount.elements():
# print(key, provinceCount[key])

totalCount = len(cityCount)
pName = []
pNum = []
pPercent = []

for each in cityCount.items():
# (a, b) = each
# print(a, b)
pName.append(each[0])
pNum.append(each[1])
pPercent.append(each[1] / totalCount)

# print(pName)
# print(pNum)

# print(matplotlib.matplotlib_fname())
# /usr/local/lib/python3.6/site-packages/matplotlib/mpl-data/matplotlibrc

# from matplotlib.font_manager import _rebuild
#
# _rebuild()

plt.rcParams['font.sans-serif'] = ['SimHei'] # 用来正常显示中文标签
plt.rcParams['axes.unicode_minus'] = False # 用来正常显示负号

# font = FontProperties(fname='/Library/Fonts/Songti.ttc') # 该方法未验证

# 1.柱状图
# plt.bar(range(len(pNum)), pNum, tick_label=pName)
# plt.show()


# 2.饼状图
# labels = pName
# fracs = pPercent
# explode = [0, 0.1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
# 0] # 0.1 凸出这部分,共len(provinceCount)=37个
# plt.axes(aspect=1) # set this , Figure is round, otherwise it is an ellipse
# # autopct ,show percet
# plt.pie(x=fracs, labels=labels, explode=explode, autopct='%3.1f %%',
# shadow=False, labeldistance=1.1, startangle=90, pctdistance=0.6)
# plt.show()

# 3.地图
value = pNum
attr = pName
geo = Geo("我的好友分布图", width=1200, height=600)
geo.add("各城市分布情况", attr, value, type="effectScatter", border_color="#ffffff", symbol_size=20, \
is_label_show=True, label_text_color="#00FF00", label_pos="inside", symbol="circle", \
symbol_color="FF0000", geo_normal_color="#006edd", geo_emphasis_color="#0000ff")
geo.show_config()
# geo.render("friendProvinceDistribution.html")
geo.render("friendCityDistribution.html")

4.图形展示

受限于屏幕尺寸,展示不全。

问题

matplotlib汉字乱码问题

解决方法

总结

数据还是要显示在图表上才更加直观。

这也更加验证了数据可视化的重要性,单纯的数据分析当然是最重要的,但可视化的工作也是相当重要的,数据分析如何产生效益,可视化工作很重要。

催生了学习R语言和Python 的图形库的需求。

参考

  • https://www.zhihu.com/question/34674954
  • http://blog.csdn.net/lxb1022/article/details/77119553
  • http://www.sohu.com/a/195899083_387904
  • 本文作者: Linking
  • 本文链接: https://linking.fun/2018/02/08/微信好友城市分布分析/
  • 版权声明: 版权所有,转载请注明出处!
  • Python
  • WeChat
  • CS

扫一扫,分享到微信

Linux下部署web服务踩坑记
博客留言系统更新为Gitalk
  1. 1. 缘由
  2. 2. 思路
  3. 3. 过程
  4. 4. 代码
  5. 5. 问题
    1. 5.1. matplotlib汉字乱码问题
  6. 6. 总结
  7. 7. 参考
© 2015-2026 Linking
GitHub:hexo-theme-yilia-plus by Litten
本站总访问量次 | 本站访客数人
  • 所有文章
  • 外链

tag:

  • weather
  • 需求
  • essay
  • basketball
  • olympic
  • nginx
  • APPScan
  • SQl盲注
  • xss
  • Ajax
  • ajax
  • ai
  • agent
  • openclaw
  • ccf
  • Nginx
  • HTML5
  • html5
  • hmtl5
  • sse
  • JavaScriptCore
  • Oracle
  • operation
  • Linux
  • deploy
  • Mac Office
  • markdown
  • ListView
  • GridView
  • MySQL
  • 慢查询
  • mongodb
  • 转置
  • thought
  • network
  • ubuntu
  • NetworkManager
  • RFKill
  • Netplan
  • avatar
  • cocoa
  • blog
  • Gitalk
  • container
  • macvlan
  • docker
  • oracle
  • cookie
  • patch
  • gitea
  • git
  • iOS
  • https
  • 多线程
  • bundle
  • 兼容性
  • HTTP
  • 绘图
  • cs
  • java
  • 效率
  • 快捷键
  • route
  • nodejs
  • pip
  • arcgis
  • arcgis 建模
  • 标识
  • redis
  • read
  • bookList
  • running
  • showdoc
  • disk
  • unit-test
  • D.Wade
  • thoughts
  • duoduo
  • Python
  • python
  • tomcat
  • 读书节
  • session
  • jdk
  • war
  • 加班
  • Android onclick事件监听
  • 正则
  • 手机品牌匹配
  • ntp
  • OpenLayers
  • Geoserver
  • wechat
  • 微信公众号
  • 爬虫
  • WeChat
  • 张靓颖
  • 动漫
  • vpn
  • PPT
  • MarkDown
  • plan
  • 朱赟
  • 极客时间专栏
  • 极客邦
  • 模块化
  • MVC
  • excel
  • NBA
  • kobe
  • team
  • crawler
  • 进度条
  • ssl
  • book
  • anti-stealing-link
  • Agentic Engineering
  • Vibe Coding
  • Software 3.0
  • Andrej Karpathy
  • LLM
  • Programming

    缺失模块。
    1、请确保node版本大于6.2
    2、在博客根目录(注意不是yilia-plus根目录)执行以下命令:
    npm i hexo-generator-json-content --save

    3、在根目录_config.yml里添加配置:

      jsonContent:
        meta: false
        pages: false
        posts:
          title: true
          date: true
          path: true
          text: false
          raw: false
          content: false
          slug: false
          updated: false
          comments: false
          link: false
          permalink: false
          excerpt: false
          categories: false
          tags: true
    

  • GitHub Trending
  • OpenAI ChatGPT
  • Gitee码云
  • 简书
  • CSDN