ThinkChat🤖让你学习和工作更高效,注册即送10W Token,即刻开启你的AI之旅 广告
![](http://imgs.bizha.top//86af4f43b3905ceb2f27e2ebd2da01dd) 前面两篇用`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`进行数据分析 ![](http://imgs.bizha.top//4718cf2d87d3dabe867d61abb1c3332d) ``` 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轴为数量. ![](http://imgs.bizha.top//9adcff630d96e5a40de4a849cc0be04b) 柱状图太密集,提取`Location`国家再次生成柱状图。 ![](http://imgs.bizha.top//753a1fc20b885822445b2b1020184b47) 再次生成(完整代码) ``` # -*- 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() ``` ![](http://imgs.bizha.top//17cb893d0c0ed1a325acb9eb2e171cc4) **效果并不好,再次优化,按数量排序** 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)] ``` 排序柱状图 ![](http://imgs.bizha.top//1e741b7588edebcf32b11ca8b3615f9b) ``` # -*- 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() ```