数据可视化利器 pyecharts

作者: 洛水青柳2017 | 来源:发表于2018-08-04 05:57 被阅读18次

    一、基本图形
    1.简单柱状图

    from pyecharts import Bar, 
    page = Page()# 生成页面
    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)
    page.add(bar)# step 2
    page.render(r'C:\Users\mei-huang\Desktop\render.html')# 指定文件保存位置
    

    2.条形图

    from pyecharts import Bar, Scatter3D,Page
    page = Page()# st
    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,mark_point=['average'])
    bar.add("商家B", attr, v2,is_stack=True,is_convert=True) #对图表进行转置 
    page.add(bar)# step 2
    page.render()# step 3
    
    ![水平柱状图示例.png](https://img.haomeiwen.com/i5063233/ee60761cae02d9ef.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
    3.3D散点图
    from pyecharts import Scatter3D,Page
    import random
    page = Page()# st
    data = [[random.randint(0, 100), random.randint(0, 100), random.randint(0, 100)] for _ in range(80)]
    range_color = ['#313695', '#4575b4', '#74add1', '#abd9e9', '#e0f3f8', '#ffffbf',
                   '#fee090', '#fdae61', '#f46d43', '#d73027', '#a50026']
    scatter3D = Scatter3D("3D 散点图示例", width=1200, height=600) #设置图表的高和宽
    scatter3D.add("", data, is_visualmap=True, visual_range_color=range_color) #视觉映射和颜色选择
    page.add(scatter3D)
    page.render()#
    
    散点图.png

    4.数据地图1

    from pyecharts import Geo
    data = [
        ("海门", 9),("鄂尔多斯", 12),("招远", 12),("舟山", 12),("齐齐哈尔", 14),("盐城", 15),
        ("赤峰", 16),("青岛", 18),("乳山", 18),("金昌", 19),("泉州", 21),("莱西", 21),
        ("日照", 21),("胶南", 22),("南通", 23),("拉萨", 24),("云浮", 24),("梅州", 25)]
    geo = Geo("全国主要城市空气质量", "data from pm2.5", title_color="#fff", title_pos="center",
    width=1200, height=600, background_color='#404a59')
    attr, value = geo.cast(data)
    geo.add("", attr, value, visual_range=[0, 200], visual_text_color="#fff", symbol_size=15, is_visualmap=True)
    geo.show_config()
    geo.render() 
    
    数据地图1.png

    5.数据地图2

    from pyecharts import Map
    districts = ['运河区', '新华区', '泊头市', '任丘市', '黄骅市', '河间市', '沧县', '青县', '东光县', '海兴县', '盐山县', '肃宁县', '南皮县', '吴桥县', '献县', '孟村回族自治县']
    areas = [109.92, 109.47, 1006.5, 1023.0, 1544.7, 1333.0, 1104.0, 968.0, 730.0, 915.1, 796.0, 525.0, 794.0, 600.0, 1191.0, 387.0]
    map_1 = Map("沧州市图例-各区面积", width=1200, height=600)
    map_1.add("", districts, areas, maptype='沧州', is_visualmap=True, visual_range=[min(areas), max(areas)],
            visual_text_color='#000', is_map_symbol_show=False, is_label_show=True)
    map_1.render()
    
    数据地图2.png

    6.饼图

    from pyecharts import Pie
    attr=["衬衫","羊毛衫","雪纺衫","裤子","帽子"]
    v1=[10,20,30,40,50]
    pie=Pie("产品销售")
    pie.add("z",attr,v1,is_label_show=True)
    pie.render()
    
    饼图.png

    7.环形图

    from pyecharts import Pie
    attr=["衬衫","羊毛衫","雪纺衫","裤子","帽子"]
    v1=[10,20,30,40,50]
    pie=Pie("产品销售-环形图",title_pos='center')
    pie.add("z",attr,v1,is_label_show=True,radius=[30,60],label_text_color=None,\
            legend_orient='vertical',legend_pos='right')
    pie.render()
    
    环形图.png

    8.散点图

    from pyecharts import Scatter
    v1=list(range(1,5,1))
    v2=list(range(1,5,1))
    scatter=Scatter("散点图示例")
    scatter.add("A",v1,v2)
    scatter.add("B",v1[::-1],v2)
    scatter.render()
    
    散点图.png

    9.仪表盘

    from pyecharts import Gauge
    gauge=Gauge("指标完成率")
    gauge.add("业务指标","完成率",95)
    gauge.render()
    
    仪表盘.png

    10.漏斗图

    from pyecharts import Funnel
    attr=["浏览","点击","下载","安装","注册"]
    values=[300,200,150,100,80]
    funnel=Funnel("用户转化图",title_pos="center") #定义和设置标题
    funnel.add("用户",attr,values,is_label_show=True,label_pos="inside",label_text_color="#fff",legend_pos="top")
    funnel.render()
    
    漏斗图.png

    11.折线图

    from pyecharts import Line
    line=Line("折线图")
    attr=['7-1','7-2','7-3','7-4','7-5']
    line.add("最高在线",attr,[725,284,613,852,679],mark_point=["max","min"],mark_line=["average"])
    line.add("平均在线",attr,[30,20,40,80,70],legend_pos="20%")
    line.render()
    
    折线图.png

    12.关系图

    from pyecharts import Graph
    nodes =[{"name": "结点1", "symbolSize": 10}, {"name": "结点2", "symbolSize": 20}, {"name": "结点3", "symbolSize": 30}, {"name": "结点4", "symbolSize": 40}, {"name": "结点5", "symbolSize": 50}, {"name": "结点6", "symbolSize": 40}, {"name": "结点7", "symbolSize": 30}, {"name": "结点8", "symbolSize": 20}]
    links =[]
    for i in nodes:
        for j in nodes:
            links.append({"source": i.get('name'), "target": j.get('name')})
    graph =Graph("关系图-环形布局示例")
    graph.add("", nodes, links, is_label_show=True, repulsion=80000, layout='circular', label_text_color=None)
    graph.show_config()
    graph.render()
    
    
    关系图.png

    13.折线面积图

    from  pyecharts  import  Line
    attr =["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"]
    v1 =[5, 20, 36, 10, 10, 100]
    v2 =[55, 60, 16, 20, 15, 80]
    line =Line("折线图-面积图示例")
    line.add("商家A", attr, v1, is_fill=True, line_opacity=0.2, area_opacity=0.4, symbol=None)
    line.add("商家B", attr, v2, is_fill=True, area_color='#000', area_opacity=0.3, is_smooth=True)
    #line.show_config()
    line.render()
    
    折线面积图.png

    14.玫瑰饼图

    from pyecharts import Pie
    attr =["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"]
    v1 =[11, 12, 13, 10, 10, 10]
    v2 =[19, 21, 32, 20, 20, 33]
    pie =Pie("饼图-玫瑰图示例", title_pos='center', width=900)
    pie.add("商品A", attr, v1, center=[25, 50], is_random=True, radius=[30, 75], rosetype='radius')
    pie.add("商品B", attr, v2, center=[75, 50], is_random=True, radius=[30, 75], rosetype='area', is_legend_show=False, is_label_show=True)
    pie.render()
    
    image.png

    15.南丁格尔玫瑰图

    from  pyecharts import Polar
    radius =['周一', '周二', '周三', '周四', '周五', '周六', '周日']
    polar =Polar("极坐标系-堆叠柱状图示例", width=1200, height=600)
    polar.add("a", [1, 2, 3, 4, 3, 5, 1], radius_data=radius, type='barAngle', is_stack=True)
    polar.add("b", [2, 4, 6, 1, 2, 3, 1], radius_data=radius, type='barAngle', is_stack=True)
    polar.add("c", [1, 2, 3, 4, 1, 2, 5], radius_data=radius, type='barAngle', is_stack=True)
    polar.show_config()
    polar.render()
    
    玫瑰图.png

    16.雷达图

    from pyecharts  import Radar
    schema = [("销售", 6500), ("管理", 16000), ("信息技术", 30000), ("客服", 38000), ("研发", 52000), ("市场", 25000)]
    v1 = [[4300, 10000, 28000, 35000, 50000, 19000]]
    v2 = [[5000, 14000, 28000, 31000, 42000, 21000]]
    radar = Radar()
    radar.config(schema)
    radar.add("预算分配", v1, is_splitline=True, is_axisline_show=True)
    radar.add("实际开销", v2, label_color=["#4e79a7"], is_area_show=False)
    radar.show_config()
    radar.render()
    
    雷达图.png

    17.其他好玩的图形

    import  math
    from pyecharts import  Polar
    data =[]
    for i in range(361):
        t =i /180*math.pi
        r =math.sin(2*t) *math.cos(2*t)
        data.append([r, i])
    polar = Polar("极坐标系示例", width=1200, height=600)
    polar.add("Color-Flower", data, start_angle=0, symbol=None, axis_range=[0, None], area_color="#f71f24", area_opacity=0.6)
    polar.show_config()
    polar.render()
    
    极坐标.png

    18.词云

    from pyecharts import WordCloud
    name = [
            'Though','the answer','this question',
            'may at first','seem to border','on the',
            'absurd','reflection','will show','that there',
            'is a','good deal','more in','it than meets','the eye'
            ]
    value = [10000,6189,4556,2356,2233,
             1895,1456,1255,981,875,
             542,462,361,265,125]
    worldcloud = WordCloud(width = 1300,height = 620)
    worldcloud.add('',name,value,word_size_range = [20,100])
    worldcloud.render()
    
    词云.png

    19.1桑基图 (1)

    from   pyecharts  import    Sankey
    nodes = [
        {'name': 'category1'}, {'name': 'category2'}, {'name': 'category3'},
        {'name': 'category4'}, {'name': 'category5'}, {'name': 'category6'},
    ]
    links = [
        {'source': 'category1', 'target': 'category2', 'value': 10},
        {'source': 'category2', 'target': 'category3', 'value': 15},
        {'source': 'category3', 'target': 'category4', 'value': 20},
        {'source': 'category5', 'target': 'category6', 'value': 25}
    ]
    sankey = Sankey("桑基图示例", width=1200, height=600)
    sankey.add("sankey", nodes, links,
               line_opacity=0.4, #不透明度,颜色的深浅
               line_curve=0.9, #曲线的扭曲程度
               line_color='source',
               is_label_show=True, label_pos='right') #是否显示标签,标签的位置
    sankey.render()
    19.2桑基图2
    
    桑基图.png
    from   pyecharts  import    Sankey
    nodes =[
    {"name":"Andriod3"},
    {"name":"服务频道2"},
    {"name":"其它2"},
    {"name":"服务频道4"},
    {"name":"服务频道3"},
    {"name":"乙方2"},
    {"name":"乙方3"},
    {"name":"其它3"},
    {"name":"Andriod4"},
    {"name":"Andriod2"},
    {"name":"其它4"},
    {"name":"Andriod1"},
    {"name":"乙方4"},
    {"name":"乙方5"},
    {"name":"Andriod5"},
    {"name":"服务频道5"},
    {"name":"其它5"},
    ]
    links = [
    {"source":"Andriod1","target":"Andriod2","value":"65"},
    {"source":"乙方3","target":"Andriod4","value":"1"},
    {"source":"乙方2","target":"Andriod3","value":"1"},
    {"source":"服务频道3","target":"Andriod4","value":"2"},
    {"source":"Andriod2","target":"Andriod3","value":"48"},
    {"source":"服务频道2","target":"其它3","value":"1"},
    {"source":"乙方2","target":"服务频道3","value":"1"},
    {"source":"Andriod3","target":"Andriod4","value":"35"},
    {"source":"Andriod2","target":"服务频道3","value":"3"},
    {"source":"Andriod4","target":"服务频道5","value":"3"},
    {"source":"Andriod3","target":"乙方4","value":"1"},
    {"source":"Andriod1","target":"服务频道2","value":"6"},
    {"source":"服务频道2","target":"服务频道3","value":"2"},
    {"source":"其它2","target":"Andriod3","value":"1"},
    {"source":"服务频道4","target":"Andriod5","value":"1"},
    {"source":"Andriod2","target":"乙方3","value":"1"},
    {"source":"Andriod1","target":"乙方2","value":"2"},
    {"source":"服务频道2","target":"Andriod3","value":"1"},
    {"source":"Andriod1","target":"其它2","value":"1"},
    {"source":"乙方4","target":"Andriod5","value":"1"},
    {"source":"服务频道3","target":"服务频道4","value":"3"},
    {"source":"Andriod4","target":"Andriod5","value":"26"},
    ]
    sankey = Sankey("桑基图示例", width=1200, height=600)
    sankey.add("sankey", nodes, links,
               line_opacity=0.4, #不透明度,颜色的深浅
               line_curve=0.9, #曲线的扭曲程度
               line_color='source',
               is_label_show=True, label_pos='right') #是否显示标签,标签的位置
    sankey.render()
    
    桑基图2.png

    二、基本的类
    1.Overlap.add()类,组合图形
    add(chart,
    xaxis_index = 0,
    yaxis_index = 0,
    is_add_xaxis = False,
    is_add_yaxis = False)
    属性:
    is_add_xaxis / is_add_yaxis 是否新增坐标X/Y 默认 False

    from pyecharts import Bar,Line,Overlap
    attr = ['A','B','C','D','E','F']
    v1 = [10,20,30,40,50,60]
    v2 = [38,28,35,58,65,100]
    bar = Bar('Line - Bar示例')
    bar.add('bar',attr,v1)
    line = Line()
    line.add('line',attr,v2)
    
    overlop = Overlap()
    overlop.add(bar,yaxis_index = 10)
    overlop.add(line,is_add_yaxis=False) #是否增加新y轴
    overlop.render()
    
    重叠组合图.png

    2.Grid()类图形左右组合

    from pyecharts import Bar,Line,Grid
    attr = ['A','B','C','D','E','F']
    v1 = [10,20,30,40,50,60]
    v2 = [38,28,35,58,65,100]
    bar = Bar('Line - Bar示例')
    bar.add('bar',attr,v1)
    line = Line()
    line.add('line',attr,v2)
    
    grid = Grid()
    grid.add(bar,grid_right="50%")
    grid.add(line,grid_left="60%") #是否增加新y轴
    grid.render()
    
    Grid左右组合.png

    3.Page()类在网页上竖直排列

    #生成数据点图
    from pyecharts import Bar,Line,Page
    attr = ['A','B','C','D','E','F']
    v1 = [10,20,30,40,50,60]
    v2 = [38,28,35,58,65,100]
    bar = Bar('Line - Bar示例')
    bar.add('bar',attr,v1)
    line = Line()
    line.add('line',attr,v2)
    page = Page()
    page.add(bar)
    page.add(line) #是否增加新y轴
    page.render()
    
    page类竖直排列.png
    1. Timeline() 在时间维度上播放图形
    from pyecharts import Bar, Timeline
    from  random import  randint
    
    attr = ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"]
    bar_1 = Bar("2012 年销量", "数据纯属虚构")
    bar_1.add("春季", attr, [randint(10, 100) for _ in range(6)])
    bar_1.add("夏季", attr, [randint(10, 100) for _ in range(6)])
    bar_1.add("秋季", attr, [randint(10, 100) for _ in range(6)])
    bar_1.add("冬季", attr, [randint(10, 100) for _ in range(6)])
    
    bar_2 = Bar("2013 年销量", "数据纯属虚构")
    bar_2.add("春季", attr, [randint(10, 100) for _ in range(6)])
    bar_2.add("夏季", attr, [randint(10, 100) for _ in range(6)])
    bar_2.add("秋季", attr, [randint(10, 100) for _ in range(6)])
    bar_2.add("冬季", attr, [randint(10, 100) for _ in range(6)])
    
    bar_3 = Bar("2014 年销量", "数据纯属虚构")
    bar_3.add("春季", attr, [randint(10, 100) for _ in range(6)])
    bar_3.add("夏季", attr, [randint(10, 100) for _ in range(6)])
    bar_3.add("秋季", attr, [randint(10, 100) for _ in range(6)])
    bar_3.add("冬季", attr, [randint(10, 100) for _ in range(6)])
    
    bar_4 = Bar("2015 年销量", "数据纯属虚构")
    bar_4.add("春季", attr, [randint(10, 100) for _ in range(6)])
    bar_4.add("夏季", attr, [randint(10, 100) for _ in range(6)])
    bar_4.add("秋季", attr, [randint(10, 100) for _ in range(6)])
    bar_4.add("冬季", attr, [randint(10, 100) for _ in range(6)])
    
    bar_5 = Bar("2016 年销量", "数据纯属虚构")
    bar_5.add("春季", attr, [randint(10, 100) for _ in range(6)])
    bar_5.add("夏季", attr, [randint(10, 100) for _ in range(6)])
    bar_5.add("秋季", attr, [randint(10, 100) for _ in range(6)])
    bar_5.add("冬季", attr, [randint(10, 100) for _ in range(6)], is_legend_show=True)
    
    timeline = Timeline(width=800,height=600
                        ,is_auto_play=True #是否自动播放
                        ,is_loop_play= True #是否循环播放
                        ,is_timeline_show=True  #是否现实时间组件
                       ,is_rewind_play=False  #是否方向播放
                        ,timeline_play_interval=1000  #跳动的时间间隔
                        ,timeline_bottom=0)  #是否循环播放
    timeline.add(bar_1, '2012 年')
    timeline.add(bar_2, '2013 年')
    timeline.add(bar_3, '2014 年')
    timeline.add(bar_4, '2015 年')
    timeline.add(bar_5, '2016 年')
    timeline.render()
    
    在时间维度.png

    相关文章

      网友评论

      • 野指针中:又是沧州,你在echart gallery上上传过demo

      本文标题:数据可视化利器 pyecharts

      本文链接:https://www.haomeiwen.com/subject/beozmftx.html