官网链接:http://pyecharts.org/#/zh-cn/charts
工具:jupyter
python环境:python3
安装库:
pip install pyecharts
还需要安装第三方库:
pip install echarts-countries-pypkg
pip install echarts-china-provinces-pypkg
pip install echarts-china-cities-pypkg
pip install echarts-china-counties-pypkg
pip install echarts-china-misc-pypkg
pip install echarts-united-kingdom-pypkg
from pyecharts import Bar,Line,Overlap,EffectScatter,Map
# 画简单的柱状图
bar = Bar("主标题", "这里是副标题")
# pyecharts 遵循所有图表都先定义数据在进行展示
bar.add("服装", ["衬衫", "羊毛衫", "雪纺衫","裤子", "高跟鞋", "袜子"],
[5, 20, 36, 10, 75, 90])
#方法一: 将图表渲染输出到html页面
# bar.render() # 如果不穿参数,默认保存到当前文件夹下
# 方法二: 直接再jupyter中展示
bar
# 2、根据柱状图绘制堆积图
attr = ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"]
v1 = [5, 20, 36, 10, 75, 90]
v2 = [10, 25, 8, 60, 20, 80]
# 添加标题
bar = 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 = Bar("标记线和标记点示例")
# 为柱状图添加标记点
bar.add("商家A", attr, v1, mark_point=["max"]) #mark_point=["max"] 标注出最大点
# 为柱状图添加标注线
bar.add("商家B", attr, v2, mark_line=["min", "max"],is_convert=True) #is_convert=True 行和列转换
bar
attr = ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋","袜子"]
v1 = [5, 20, 36, 10, 75, 90]
v2 = [10, 25, 8, 60, 20, 80]
myLine =Line('折线图示例')
# is_smooth 点之间是否进行平滑曲线连接
myLine.add('商家A',attr,v1,mark_point=['max'],is_smooth=True)
myLine.add('商家B',attr,v2,is_smooth=True)
myLine
attr = ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋","袜子"]
v1 = [5, 20, 36, 10, 75, 90]
v2 = [10, 25, 8, 60, 20, 80]
markline = Line('添加标注点的折线图')
markline.add('商家A',attr,v1,
mark_point=['min','average','max'],
mark_point_symbol='diamond',
mark_point_textcolor='#40ff27')
markline.add('商家B',attr,v2,
mark_point=['min','average','max'],
mark_point_symbol='arrow',
mark_point_symbolsize=20)
'''
mark_point 标注点
mark_point_symbol 标注点的形状
mark_point_symbolsize 标注点的大小
mark_point_textcolor 标注点的字体颜色
'''
markline
# 5、折线图--面积图示例
attr = ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋","袜子"]
v1 = [5, 20, 36, 10, 75, 90]
v2 = [10, 25, 8, 60, 20, 80]
arealine = Line('面积图示例')
arealine.add('商家A',attr,v1,
is_fill=True,
area_color='#00ff00',
line_opacity=1,
area_opacity=0.4,
symbol= None,
)
arealine.add('商家B',attr,v2,
is_fill=True,
area_color='#000',
area_opacity=0.3,
is_smooth=True)
'''
is_fill 为折线图所包裹的区域填充颜色
area_color 填充区域的颜色
area_opacity 填充区域的透明度
line_opacity=0.2 线条的不透明度
'''
arealine
#6、画饼图
from pyecharts import Pie
attr = ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋","袜子"]
v1 = [5, 20, 36, 10, 75, 90]
v2 = [10, 25, 8, 60, 20, 80]
pie = Pie('饼图')
pie.add('',attr,v1,is_label_show=True)
pie
# 饼图圆环
attr = ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"]
v1 = [11, 12, 13, 10, 10, 10]
pie = Pie("饼图-圆环图示例",title_pos='center')
pie.add("", attr, v1, radius=[40, 75],
label_text_color=None,
is_label_show=True,
legend_orient='vertical', #legend_orient 图例展开的方向
legend_pos='left') # 图例的位置
pie
v1 = [10, 20, 30, 40, 50, 60]
v2 = [10, 20, 30, 40, 50, 60]
scatter = Scatter("散点图示例")
scatter.add("A", v1, v2)
scatter.add("B", v1[::-1], v2)
scatter
v1 = [10, 20, 30, 40, 50, 60]
v2 = [10, 20, 30, 40, 50, 60]
scatter = Scatter("散点图示例")
scatter.add("A", v1, v2)
scatter.add("B", v1[::-1], v2, is_visualmap=True,
visual_type='size',
visual_range_size=[20, 80])
scatter
from pyecharts import Map
value = [155, 10, 66, 78]
attr = ["北京", "台湾", "河南", "西藏"]
map2 = Map("全国地图示例", width=1200,height=500)
map2.add("", attr, value, maptype='china')
map2
注意:如果以上不显示地图,则要安装第三方库
pip install echarts-countries-pypkg
pip install echarts-china-provinces-pypkg
pip install echarts-china-cities-pypkg
pip install echarts-china-counties-pypkg
pip install echarts-china-misc-pypkg
pip install echarts-united-kingdom-pypkg
# 省地图示例
value = [20,190,253,78]
attr = ["郑州市", "濮阳市", "安阳市", "洛阳市"]
map2 = Map("全国地图示例", width=1200,height=500)
# 添加地图
#根据对应的数值大小,绘制颜色深浅分布地图
map2.add("", attr, value,
is_visualmap=True,
visual_text_color='#ff0000',
is_label_show=True,
maptype='河南')
map2
from pyecharts import WordCloud
wd = WordCloud('绘制词云',width=1000, height=500)
name = ['Sam S Club', 'Macys', 'Amy Schumer', 'Jurassic World',
'Charter Communications',
'Chick Fil A', 'Planet Fitness', 'Pitch Perfect', 'Express',
'Home', 'Johnny Depp',
'Lena Dunham', 'Lewis Hamilton', 'KXAN', 'Mary EllenMark', 'Farrah Abraham',
'Rita Ora', 'Serena Williams', 'NCAA baseball tournament',
'Point Break']
value = [10000, 6181, 4386, 4055, 2467, 2244, 1898, 1484, 1112,
965, 847, 582, 555, 550, 462, 366, 360, 282, 273, 265]
wd.add("", name, value, word_size_range=[20, 100])
wd
# overlap 将多个张图表整合到一个画板上
# 绘制柱状图
hero1 = ['鲁班','妲己','程咬金','后裔']
death_times = [1200,600,90,1000]
bar = Bar("王者荣耀英雄死亡次数")
bar.add('',hero1,death_times)
bar
# 绘制折线图
hero2 = ['鲁班','王昭君','孙悟空','杨贵妃']
hurts = [2000,1200,3500,3000]
line = Line("六神装伤害输出")
line.add('',hero2,hurts)
line
# 创建overlap实例
overlap = Overlap()
overlap.add(bar)
overlap.add(line,is_add_xaxis=True) #is_add_xaxis=True 是否新增一个 x 坐标轴,默认为 False
overlap
# 组合散点图和折线图
attr = ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"]
v1 = [5, 20, 36, 10, 10, 100]
line = Line("line - es 示例")
line.add("", attr, v1, is_random=True)
es = EffectScatter()
es.add("", attr, v1, effect_scale=8)
overlap = Overlap()
overlap.add(line)
overlap.add(es)
overlap