简介
PyeCharts 是一个用于生成 Echarts 图表的类库,
用其生成的图可视化效果非常棒,而且使用起来非常简单。
下面是一些常用图的pyecharts实现方法
柱状图
bar = pye.Bar("柱状图")#新建柱状图
bar.add("服装", #图例名称
["衬衫", "羊毛衫", "雪纺衫", "裤子" , "高跟鞋" , "袜子"],#x
[5, 20, 36, 10, 75, 90],#y
bar_category_gap="20%",
is_more_utils=True,#右边工具栏显示更多按钮
)#添加数据
# bar.render("bar.html")#渲染到文件
bar
输出:
堆叠柱状图
attr = ["衬衫", "羊毛衫", "雪纺衫", "裤子" , "高跟鞋" , "袜子"]
v1 = [5, 20, 36, 10, 75, 90]
v2 = [10, 25, 8, 60, 20, 80]
bar = pye.Bar("堆叠柱状图")
bar.add("商家A", attr, v1, is_stack=True)
bar.add("商家B", attr, v2, is_stack=True)
bar
输出:
条形图
attr = ["衬衫", "羊毛衫", "雪纺衫", "裤子" , "高跟鞋" , "\
袜子"]
v1 = [5, 20, 36, 10, 75, 90]
v2 = [10, 25, 8, 60, 20, 80]
bar = pye.Bar()
bar.add("商家A", attr, v1, mark_point=["average", "max", "min"], is_stack=True)
bar.add("商家B", attr, v2, mark_line=["max"], is_convert=True, is_stack=True)
bar
输出:
折线图
attr = ["衬衫", "羊毛衫", "雪纺衫", "裤子" , "高跟鞋" , "\
袜子"]
v1 = [5, 20, 36, 10, 75, 90]
v2 = [10, 25, 8, 60, 20, 80]
bar = pye.Line("折线图")
bar.add("商家A", attr, v1,
mark_point=["average"],
mark_point_symbol="arrow",
mark_point_textcolor="#cf0",
mark_point_symbolsize=30,
is_fill=True,#是否填充
line_opacity=0.2,#线的不透明度
area_opacity=0.4,#填充区域的不透明度
)
bar.add("商家B", attr, v2,
mark_point=["max"],
mark_point_symbol="diamond",
is_smooth=True, #绘制平滑曲线
is_fill=True,
#line_opacity=1,
area_opacity=0.3,
area_color="#000",
symbol=None
)
bar
输出:
饼图
attr = ["衬衫", "羊毛衫", "雪纺衫", "裤子" , "高跟鞋" , "\
袜子"]
v1 = [5, 20, 36, 10, 75, 90]
pie = pye.Pie("饼图", title_pos="center")
pie.add("服装销量", attr, v1,
is_label_show=True,
center=[50, 50],#中心点位置
# rosetype="area",#南丁格尔图
radius=[40, 75],#内环,外环,
legend_orient="vertical",
legend_pos="left"
)
pie
输出:
散点图
v1 = v2 = np.arange(0, 70, 10)#散点图
scatter = pye.Scatter()
scatter.add("", v1, v2, )
scatter.add("", v1[::-1],
v2, is_visualmap=True, #是否使用第三维度
visual_type="size", #第三维度是点的大小
visual_range_size=[10, 60])#取值范围
scatter
输出:
地图
map = pye.Map()
attr = ["浦东新区", "闵行区"]
value = [50, 180]
map.add("", attr, value,
maptype="上海",#地图类型
is_visualmap=True,
is_label_show=True#显示地区标注
)
map
输出:
云词图
import pandas as pd
from bs4 import BeautifulSoup as bs
import re
df = pd.read_csv("labeledTrainData.tsv", sep='\t', escapechar='\\')#读取文件
reviews = df["review"].head(1000).tolist()#转换成list
review_str = "".join(reviews)#合并成str
bs_text = str.lower(bs(review_str, "lxml").get_text())#1去掉html标签并且转为小写
only_words_list = re.sub(r"[^\w\s]", "", bs_text).replace(" ", " ").split(" ")#删除特殊字符 重新拆分成list
stop_words_list = open("stopwords.txt").read().split(" \n")#获取停用词列表
no_stop_words_list = [w for w in only_words_list if w not in stop_words_list]#删除所有停用词
from collections import Counter
only_words_dict = Counter(no_stop_words_list)#转换成字典
# only_words_dict
word_cloud = pye.WordCloud()
word_cloud.add("", only_words_dict.keys(), only_words_dict.values(), word_size_range=[10, 100])
word_cloud.render("ccc.html")#存在文件中
输出:
组合图
使用Overlap
attr = ['A' , 'B' , 'C' , 'D' , 'E' , 'F']
v1 = [10, 20, 30, 40, 50, 60]
v2 = [38, 28, 58, 48, 78, 68]
bar = pye.Bar("Line - Bar 示例")
bar.add("bar" , attr, v1)
line = pye.Line()
line.add("line" , attr, v2)
overlap = pye.Overlap()
overlap.add(bar)
overlap.add(line)
overlap
输出: