def create_Pie_charts(data, title): '''饼状图''' page = Page() style = Style(width=800, height=800) chart = Pie(title, **style.init_style) attr, value = chart.cast(data) chart.add("", attr, value, is_label_show=True) page.add(chart) return page
def sexpietest(self): from pyecharts import Pie pie = Pie('性别分布饼形图', self.ftitle, height=500) key, value = pie.cast(self.sexpiedata) pie.add('人数', key, value, is_label_show=True, center=[50, 55], rosetype='area') pie.render(r'pic\性别分布饼形图.html') print('性别饼形图输出成功')
def pietest(self): from pyecharts import Pie pie = Pie('头衔分布饼形图', self.ftitle, height=600, title_pos='left') key, value = pie.cast(self.piedata) pie.add('人数', key, value, is_label_show=True, is_legend_show=False, radius=[30, 70]) pie.render(r'pic\头衔分布饼形图.html') print('饼形图输出Pie测试网页')
def _education_show(self): pie = Pie("学历统计", title_pos='center', width=1200, height=500) attr, value = pie.cast(self.g.education_data) pie.add("", attr, value, adius=[40, 80], title_pos='center', is_label_show=True, legend_orient='vertical', legend_pos='left') return pie.render_embed()
def fromtest(self): from pyecharts import Pie pie = Pie('帖子来源饼形图', self.ftitle, title_pos='center') key, value = pie.cast(self.fromdata) pie.add('帖子数', key, value, is_label_show=True, radius=[35, 65], center=[50, 55], legend_pos='left', legend_orient='vertical') pie.render(r'pic\帖子来源饼形图.html')
def create_profession(data, title): page = Page() style = Style(width=900, height=400, title_pos="center", background_color='#c4ccd3') kwargs = dict(radius=(40, 75), label_text_color=None, is_label_show=True, legend_orient='her', legend_pos='left') profession_chart = Pie(title, **style.init_style) attr, value = profession_chart.cast(data) profession_chart.add("", attr, value, **kwargs) page.add(profession_chart) return page
itchat.auto_login(hotReload=True) #登陆微信 myFriendList = itchat.get_friends() #获取微信中的好友列表 sexCounter = {'男' : 0, '女' : 0, '未标注' : 0} for friend in myFriendList: if friend['Sex'] == 1: sexCounter['男'] += 1 elif friend['Sex'] == 2: sexCounter['女'] += 1 else: sexCounter['未标注'] += 1 pie = Pie('你的微信好友性别比例图',width=1200, height=600, title_pos='center') attr, value = pie.cast(sexCounter) '''利用pyecharts的cast方法将字典列表转换成两个列表用于绘图 例: tagsList = [{'周一':3},{'周二':2},{'周三':1}] 用cast方法获得 attr = ['周一','周二','周三'] value = [3,2,1] ''' pie.add( '', attr, value, is_label_show = True, legend_orient = 'vertical', # 图例是否垂直 legend_pos = 'left' ) pie.render('./你的微信好友性别比例图.html') # 绘图渲染
def pricePieMaking(task_id, start_range, end_range): client = pymongo.MongoClient("127.0.0.1", 27017) list = [] dict = {} list1 = [] try: db = client.crawlData # 连接test数据库 collection = db.taobao # 访问test数据库中things集合 for m in collection.find({"task_id": task_id}): # 到时候改这里,改成根据id查找 list.append(m['now_price']) except: pass finally: client.close() end_range = float(end_range) start_range = float(start_range) price_range = (end_range - start_range) / 4 price1 = str(start_range) + '~' + str( start_range + price_range) # 1000~1250 price2 = str(start_range + price_range) + '~' + str( start_range + price_range * 2) # 1250~1500 price3 = str(start_range + price_range * 2) + '~' + str( start_range + price_range * 3) # 1500~1750 price4 = str(start_range + price_range * 3) + '~' + str( end_range) # 1750~2000 dict[price1] = 0 dict[price2] = 0 dict[price3] = 0 dict[price4] = 0 # 分析数据 for index in list: # 遍历词语列表 if float(index.encode("utf-8")) > start_range and float( index.encode("utf-8")) < start_range + price_range * 1: dict[price1] += 1 elif float( index.encode("utf-8")) > start_range + price_range and float( index.encode("utf-8")) < start_range + price_range * 2: dict[price2] += 1 elif float(index.encode( "utf-8")) > start_range + price_range * 2 and float( index.encode("utf-8")) < start_range + price_range * 3: dict[price3] += 1 elif float(index.encode( "utf-8")) > start_range + price_range * 3 and float( index.encode("utf-8")) < end_range: dict[price4] += 1 else: # 不在这个范围内的 continue # print dict for item in dict.items(): # 化成列表类型 list1.append(item[0]) list1.append(item[1]) # print list1 list2 = [] for i in range(0, len(list1), 2): # 拼成pycharts能够输入的格式,步长为2 a = (list1[i], list1[i + 1]) list2.append(a) # 生成图表 pie = Pie(u"商品价格范围分布图") pie.width = 1200 pie.height = 800 attr, value = pie.cast(list2) pie.add("", attr, value, is_label_show=True) # pie.show_config() pie.render("static/prices/price" + task_id + ".html")
def render(): global positive_text global negative_text # 获取评论中所有城市 cities = [] with open('maoyan2.csv', mode='r', encoding='utf-8') as f: rows = f.readlines() for row in rows[1:]: if row.count(',') != 9: continue elements = row.split(',') time = elements[0] gender = elements[3] city = elements[4] comment = elements[7] sentiment = elements[8] score = elements[6] if not is_float(elements[9]): continue if not is_float(score): continue if city != '': # 去掉城市名为空的值 cities.append(city) if score != '': scores.append(float(score) * 2) # if float(score) * 2 > 7: # positive_text += comment # elif float(score) * 2 < 4: # negative_text += comment if gender != '': genders.append(gender) if time != '': dates.append(time) positive_prob = float(elements[9]) if positive_prob >= 0.7: # positive_text += comment positive_probs.append(round(positive_prob, 2)) else: # negative_text += comment negative_probs.append(round(positive_prob, 1)) sentiments.append(sentiment) # print(positive_text) # print(negative_text) # with open("positive_text.txt","w", encoding='utf-8') as f: # f.write(positive_text) # with open("negative_text.txt","w", encoding='utf-8') as f: # f.write(negative_text) # 对城市数据和坐标文件中的地名进行处理 handle(cities) data = Counter(cities).most_common() # 使用Counter类统计出现的次数,并转换为元组列表 score_data = Counter(scores).most_common() # 使用Counter类统计出现的次数,并转换为元组列表 # 按0-10进行排序 score_data = sorted(score_data) gender_data = Counter(genders).most_common() # 使用Counter类统计出现的次数,并转换为元组列表 print(gender_data) date_data = Counter(dates).most_common() # 按日期进行排序 date_data = sorted(date_data) # print(data) sentiments_data = Counter(sentiments).most_common() # 使用Counter类统计出现的次数,并转换为元组列表 positive_probs_data = Counter(positive_probs).most_common() # 使用Counter类统计出现的次数,并转换为元组列表 positive_probs_data = sorted(positive_probs_data) negative_probs_data = Counter(negative_probs).most_common() # 使用Counter类统计出现的次数,并转换为元组列表 negative_probs_data = sorted(negative_probs_data) # 定义样式 style = Style( title_color='#fff', title_pos='center', width=800, height=600, background_color='#404a59' ) from pyecharts import Pie # 设置主标题与副标题,标题设置居中,设置宽度为900 pie = Pie("情感分析结果分布图", "数据来源:不正经程序员-采集自猫眼",title_pos='center',width=900) attr, value = pie.cast(sentiments_data) # 加入数据,设置坐标位置为【25,50】,上方的colums选项取消显示 pie.add("", attr, value ,visual_range=[0, 3500],is_legend_show=False, is_label_show=True) # 保存图表 pie.render('picture\情感分析结果分布图.html') # 根据正向情感分析指数生成柱状图 bar = Bar('正向情感分布', '数据来源:不正经程序员-采集自猫眼', title_pos='center', width=1200, height=600) attr, value = bar.cast(positive_probs_data) bar.add('', attr, value, is_visualmap=False, visual_range=[0, 3500], visual_text_color='#fff', is_more_utils=True, is_label_show=True) bar.render('picture\正向情感分布-柱状图.html') # 根据负向情感分析指数生成柱状图 bar = Bar('负向情感分布', '数据来源:不正经程序员-采集自猫眼', title_pos='center', width=1200, height=600) attr, value = bar.cast(negative_probs_data) bar.add('', attr, value, is_visualmap=False, visual_range=[0, 3500], visual_text_color='#fff', is_more_utils=True, is_label_show=True) bar.render('picture\负向情感分布-柱状图.html') # 根据城市数据生成地理坐标图 geo = Geo('观众地理分布', '数据来源:不正经程序员-采集自猫眼', **style.init_style) attr, value = geo.cast(data) geo.add('', attr, value, visual_range=[0, 600], visual_text_color='#fff', symbol_size=7, is_visualmap=True, is_piecewise=True, visual_split_number=10) geo.render( 'picture\观众地理分布-地理坐标图.html') # 根据城市数据生成柱状图 data_top20 = Counter(cities).most_common(20) # 返回出现次数最多的20条 bar = Bar('观众来源排行TOP20', '数据来源:不正经程序员-采集自猫眼', title_pos='center', width=1200, height=600) attr, value = bar.cast(data_top20) bar.add('', attr, value, is_visualmap=False, visual_range=[0, 3500], visual_text_color='#fff', is_more_utils=True, is_label_show=True) bar.render('picture\观众来源排行-柱状图.html') # 根据评分数据生成柱状图 bar = Bar('《哪吒》各评分数量', '数据来源:不正经程序员-采集自猫眼', title_pos='center', width=900, height=600) attr, value = bar.cast(score_data) # line = Line() # line.add('', attr, value) bar.add('', attr, value, is_visualmap=False, visual_range=[0, 3500], visual_text_color='#fff', is_more_utils=True, is_label_show=True) overlap = Overlap() overlap.add(bar) # overlap.add(line) overlap.show_config() overlap.render( 'picture\评分数量-柱状图.html') # 根据评分数据生成柱状图 bar = Bar('评价人数走势图', '数据来源:不正经程序员-采集自猫眼', title_pos='center', width=1200, height=600) attr, value = bar.cast(date_data) # line = Line() # line.add('', attr, value) bar.add('', attr, value, is_visualmap=False, visual_range=[0, 3500], visual_text_color='#fff', is_more_utils=True, is_label_show=True) overlap = Overlap() overlap.add(bar) # overlap.add(line) overlap.show_config() overlap.render( 'picture\评价人数走势图.html') from pyecharts import Pie # 设置主标题与副标题,标题设置居中,设置宽度为900 pie = Pie("观众性别分布图", "数据来源:不正经程序员-采集自猫眼",title_pos='center',width=900) attr, value = geo.cast(gender_data) # 加入数据,设置坐标位置为【25,50】,上方的colums选项取消显示 pie.add("", ["其他","男","女"], value ,visual_range=[0, 3500],is_legend_show=False, is_label_show=True) # 保存图表 pie.render('picture\观众性别分布图.html')