1. 引言
統(tǒng)計趕集網(wǎng)-上海-二手市場16個大類目的發(fā)帖量, 并在jupyter-notebook中繪制出各區(qū)域發(fā)帖量對比的柱狀圖
2. 分析
- 篩選重復(fù)的分類使之唯一
- 分別統(tǒng)計分類出現(xiàn)的次數(shù)
- 生成合乎
charts
要求格式的字典列表
3. 實(shí)現(xiàn)
In [1] :
from pymongo import MongoClient
from string import punctuation
import charts
Server running in the folder /home/wjh at 127.0.0.1:53200
In [2] :
client = MongoClient('10.66.17.17', 27017)
database = client['ganji']
item_info_collection = database['sh_ershou_itemY']
In [3] :
# 包含所有分類的列表
cate_list = [i['cate'][1] for i in item_info_collection.find()]
# 區(qū)域名字是唯一的集合
cate_set = set(cate_list)
# 輸出看下是什么結(jié)果
print(len(cate_set), cate_set)
16 {'xuniwupin', 'meironghuazhuang', 'jiaju', 'diannao', 'fushixiaobaxuemao', 'ershoubijibendiannao', 'shuma', 'laonianyongpin', 'xianzhilipin', 'ruanjiantushu', 'yingyouyunfu', 'nongyongpin', 'jiadian', 'bangong', 'rirongbaihuo', 'shouji'}
In [4] :
# 統(tǒng)計分類出現(xiàn)次數(shù)的列表, 如下看到有16個分類
cate_times = [cate_list.count(index) for index in cate_set]
# 輸出看下是什么結(jié)果
print(len(cate_times), cate_times)
16 [759, 2815, 4040, 4043, 4041, 2302, 3935, 1210, 3757, 3144, 4045, 321, 4041, 3751, 2287, 3335]
In [5] :
# 定義生成圖表數(shù)據(jù)的函數(shù)
def cate_data_gen(types):
length = 0
# 循環(huán)次數(shù)為區(qū)域集合長度
if length <= len(area_set):
for name, time in zip(cate_set, cate_times):
data = {
'name': name,
'data': [time],
'type': types,
}
# 遇到y(tǒng)ield語句返回钩杰,再次執(zhí)行時從上次返回的yield語句處繼續(xù)執(zhí)行, 所以循環(huán)執(zhí)行就有一個字典列表了
yield data
# 輸出看下是什么結(jié)果
[i for i in cate_data_gen('column')]
Out [5] :
[{'data': [759], 'name': 'xuniwupin', 'type': 'column'},
{'data': [2815], 'name': 'meironghuazhuang', 'type': 'column'},
{'data': [4040], 'name': 'jiaju', 'type': 'column'},
{'data': [4043], 'name': 'diannao', 'type': 'column'},
{'data': [4041], 'name': 'fushixiaobaxuemao', 'type': 'column'},
{'data': [2302], 'name': 'ershoubijibendiannao', 'type': 'column'},
{'data': [3935], 'name': 'shuma', 'type': 'column'},
{'data': [1210], 'name': 'laonianyongpin', 'type': 'column'},
{'data': [3757], 'name': 'xianzhilipin', 'type': 'column'},
{'data': [3144], 'name': 'ruanjiantushu', 'type': 'column'},
{'data': [4045], 'name': 'yingyouyunfu', 'type': 'column'},
{'data': [321], 'name': 'nongyongpin', 'type': 'column'},
{'data': [4041], 'name': 'jiadian', 'type': 'column'},
{'data': [3751], 'name': 'bangong', 'type': 'column'},
{'data': [2287], 'name': 'rirongbaihuo', 'type': 'column'},
{'data': [3335], 'name': 'shouji', 'type': 'column'}
In [6] :
# 生成數(shù)據(jù)
serises = [i for i in cate_data_gen('column')]
# 傳入?yún)?shù)并繪制圖表
charts.plot(serises, show='inline', options=dict(title=dict(text='某些時候上海城區(qū)二手物品類別分布量')))
Out [6] :
Paste_Image.png
4. 總結(jié)
-
highcharts
:
Highcharts是一款純javascript編寫的圖表庫层宫,能夠很簡單便捷的在Web網(wǎng)站或Web應(yīng)用中添加交互性的圖表糖耸,Highcharts目前支持直線圖辕万、曲線圖、面積圖儡司、柱狀圖娱挨、餅圖、散點(diǎn)圖等多達(dá)18種不同類型的圖表捕犬,可以滿足你對Web圖表的任何需求 跷坝!