任务
分析sample数据中各大类发帖量,绘制图表
工具
python 3
charts库
jupyter网页编程环境
代码
import pymongo
import charts
client = pymongo.MongoClient('localhost',27017)
test = client['test']
item_info = test['sample']
for i in item_info.find().limit(200):
print(i) #观察发现数据规律
class_list = []
for i in item_info.find():
class_list.append(i['cates'][2][4:])
class_index = list(set(class_list)) #生成不重复的大类名称列表
print(class_index)
post_times = []
for index in class_index:
post_times.append(class_list.count(index)) #生成各大类发帖量
print(post_times)
def data_gen(types): #数据生成器函数
length = 0
if length <= len(class_index):
for class_name,times in zip(class_index,post_times):
data = {
'name':class_name,
'data':[times],
'type':types
}
yield(data)
length+=1
for i in data_gen('column'):
print(i)
series = [ data for data in data_gen('column')]
charts.plot(series, show='inline', options = dict(title=dict(text='58同城网站各类北京二手物品发帖量')))