
前面兩篇用`MongoDB`數據庫進行了代理池維護,這篇用這個數據庫進行可視化。數據庫`Documents`格式為:
```json
{
"_id": {
"$oid": "5ea99e09c1e6fefaa9e4531a"
},
"https": "Socks4://119.146.131.247:8080",
"Location": "中國廣東梅州",
"AddTime": "2020年04月29日 23時32分25秒",
"LastUpdate": "2020年04月30日 15時43分16秒"
}
```
對`Location`進行數據分析

```
xaxis = list(set(LocationList ))
yaxis = []
for x in xaxis:
y = xaxis.count(x)
yaxis.append(y)
```
### 一、生成柱狀圖
LocationList = [Proxies['Location'] for Proxies in ProxiesList]
柱狀圖X軸為`Location`,Y軸為數量.

柱狀圖太密集,提取`Location`國家再次生成柱狀圖。

再次生成(完整代碼)
```
# -*- coding: utf-8 -*-
"""
Created on Thu Apr 30 16:06:15 2020
@author: Fuwenyue
"""
from pyecharts.charts import Bar
import pymongo
myclient = pymongo.MongoClient('mongodb://fuwenyue:pass4Top@ds049446.mlab.com:49446/proxy',retryWrites='false')
mydb = myclient['proxy']
ProxiesCol = mydb['socks']
ProxiesList = ProxiesCol.find({},{ "_id": 0, "Location": 1}).sort('update',-1)
ProxiesList = [i for i in ProxiesList]
LocationList = [Proxies['Location'] for Proxies in ProxiesList]
LocationList = [Location[0:2] for Location in LocationList]
dic = {}
xaxis = list(set(LocationList))
for x in xaxis:
y = LocationList.count(x)
dic[x] = y
yaxis = [dic[x] for x in xaxis]
bar = (
Bar()
.add_xaxis(xaxis)
.add_yaxis("Socks", yaxis)
)
#bar.render_notebook()
```

**效果并不好,再次優化,按數量排序**
dict 轉 tuple,list
```
d = {'s':2,'e':5,'g':6,'j':7,'m':8,'a':10}
t = tuple(d.items())
print(t)
>>(('s', 2), ('e', 5), ('g', 6), ('j', 7), ('m', 8), ('a', 10))
l = list(t)
print(l)
>>[('s', 2), ('e', 5), ('g', 6), ('j', 7), ('m', 8), ('a', 10)]
```
list tuple排序
```
l = [('s',2),('e',5),('g',6),('j',7),('m',8),('a',10)]
sorted_l=sorted(l,key=lambda t:t[0])
print(sorted_l)
>>[('a', 10), ('e', 5), ('g', 6), ('j', 7), ('m', 8), ('s', 2)]
l = [('s',2),('e',5),('g',6),('j',7),('m',8),('a',10)]
sorted_l=sorted(l,key=lambda t:t[1],reverse=False)
print(sorted_l)
>>[('s', 2), ('e', 5), ('g', 6), ('j', 7), ('m', 8), ('a', 10)]
```
排序柱狀圖

```
# -*- coding: utf-8 -*-
"""
Created on Thu Apr 30 16:06:15 2020
@author: Fuwenyue
"""
from pyecharts.charts import Bar
import pymongo
myclient = pymongo.MongoClient('mongodb://fuwenyue:pass4Top@ds049446.mlab.com:49446/proxy',retryWrites='false')
mydb = myclient['proxy']
ProxiesCol = mydb['socks']
ProxiesList = ProxiesCol.find({},{ "_id": 0, "Location": 1}).sort('update',-1)
ProxiesList = [i for i in ProxiesList]
LocationList = [Proxies['Location'] for Proxies in ProxiesList]
LocationList = [Location[0:2] for Location in LocationList]
dic = {}
xaxis = list(set(LocationList))
for x in xaxis:
y = LocationList.count(x)
dic[x] = y
l = list(tuple(dic.items()))
sorted_l = sorted(l,key=lambda t:t[1],reverse=True)
xaxis = [x[0] for x in sorted_l]
yaxis = [x[1] for x in sorted_l]
bar = (
Bar()
.add_xaxis(xaxis)
.add_yaxis("Socks", yaxis)
)
#bar.render_notebook()
```
- 【數據可視化】微博熱搜排行榜爬蟲及數據可視化
- 【數據可視化】bilibili直播排行榜爬蟲及數據可視化
- 【互聯網】隱藏在嗶哩嗶哩網頁中的彩蛋
- 【爬蟲】懶人聽書免費部分及已付費部分下載
- 【互聯網】搭建各種網盤
- 【互聯網】對象儲存客戶端用作直鏈網盤
- 【互聯網】折騰個手嶌葵的音樂網站
- 【互聯網】折騰個音樂網站(進階版)
- 【軟件】Mp3tag的使用與配置
- 【數據庫】MongoDB與python的配合使用
- 【爬蟲】爬取Socks代理,保存至MongoDB,維護代理池
- 【數據可視化】MongoDB代理池進行數據可視化
- 【軟件】Sublime Text 3 的配置與使用
- 【互聯網】Apache的. htaccess解決301批量重定向
- 【互聯網】AmWiki的安裝與使用
- 【互聯網】在新浪云(Sae)部署Docker
- 【Termux】Jupyter notebook的安裝與使用
- 【Termux】運行自動簽到autosignmachine