万能的python-画图

发布时间:2019-09-12 07:58:26编辑:auto阅读(2607)

    我们可以试用可视化包——Pyechart。

    Echarts是百度开源的一个数据可视化JS库,主要用于数据可视化。

    pyecharts是一个用于生成Echarts图标的类库。实际就是Echarts与Python的对接。

    安装

    pyecharts兼容Python2和Python3。执行代码:

    pip install pyecharts(快捷键Windows+R——输入cmd)

    初级图表

    1.柱状图/条形图

    [python] view plain copy
    1. from pyecharts import Bar  
    [python] view plain copy
    1. attr=["衬衫","羊毛衫","雪纺衫","裤子","高跟鞋","袜子"]  
    2. v1=[5,20,36,10,75,90]  
    3. v2=[10,25,8,60,20,80]  
    4. bar=Bar("各商家产品销售情况")  
    5. bar.add("商家A",attr,v1,is_stack=True)  
    6. bar.add("商家B",attr,v2,is_stack=True)  
    7. bar#bar.render()  

    2.饼图

    [python] view plain copy
    1. from pyecharts import Pie  
    2. attr=["衬衫","羊毛衫","雪纺衫","裤子","高跟鞋","鞋子"]  
    3. v1=[11,12,13,10,10,10]  
    4. pie=Pie("各产品销售情况")  
    5. pie.add("",attr,v1,is_label_show=True)  
    6. pie        #pie.render()  


    3.圆环图

    [python] view plain copy
    1. from pyecharts import Pie  
    2. attr=["衬衫","羊毛衫","雪纺衫","裤子","高跟鞋","鞋子"]  
    3. v1=[11,12,13,10,10,10]  
    4. pie=Pie("饼图—圆环图示例",title_pos="center")  
    5. pie.add("",attr,v1,radius=[40,75],label_text_color=None,  
    6.        is_label_show=True,legend_orient="vertical",  
    7.        legend_pos="left")  
    8. pie  

    4.散点图

    [python] view plain copy
    1. from pyecharts import Scatter  
    2. v1=[10,20,30,40,50,60]  
    3. v2=[10,20,30,40,50,60]  
    4. scatter=Scatter("散点图示例")  
    5. scatter.add("A",v1,v2)  
    6. scatter.add("B",v1[::-1],v2)  
    7. scatter  

    5.仪表盘

    [python] view plain copy
    1. from pyecharts import Gauge  
    2. gauge=Gauge("业务指标完成率—仪表盘")  
    3. gauge.add("业务指标","完成率",66.66)  
    4. gauge  

    6.热力图

    [python] view plain copy
    1. import random  
    2. from pyecharts import HeatMap  
    3. x_axis=[  
    4.     "12a","1a","2a","3a","4a","5a","6a","7a","8a","9a","10a","11a",  
    5.     "12p","1p","2p","3p","4p","5p","6p","7p","8p","9p","10p","11p",]  
    6. y_axis=[  
    7.     "Saturday","Friday","Thursday","Wednesday","Tuesday","Monday","Sunday"]  
    8. data=[[i,j,random.randint(0,50)] for i in range(24for j in range(7)]  
    9. heatmap=HeatMap()  
    10. heatmap.add("热力图直角坐标系",x_axis,y_axis,data,is_visualmap=True,  
    11.            visual_text_color="#000",visual_orient="horizontal")  
    12. heatmap  

    高级图表

    1.漏斗图

    [python] view plain copy
    1. from pyecharts import Funnel  
    2. attr=["潜在","接触","意向","明确","投入","谈判","成交"]  
    3. value=[140,120,100,80,60,40,20]  
    4. funnel=Funnel("销售管理分析漏斗图")  
    5. funnel.add("商品",attr,value,is_label_show=True,  
    6.           label_pos="inside",label_text_color="#fff")  
    7. funnel  

    2.词云图

    [python] view plain copy
    1. from pyecharts import WordCloud  
    2. name=[  
    3.     "Sam s  Club","Macys","Amy Schumer","Jurassic World","Charter Communications",  
    4.     "Chick Fil A","Planet Fitness","Pitch Perfect","Express","Home","Johnny Depp",  
    5.     "Lena Dunham","Lewis Hamilton","KXAN","Mary Ellen Mark","Farrah Abraham",  
    6.     "Rita Ora","Serena Williams","NCAA baseball tournament","Point Break"  
    7. ]  
    8. value=[  
    9.     10000,6181,4386,4055,2467,2244,1898,1484,1112,  
    10.     965,847,582,555,550,462,366,360,282,273,265]  
    11. wordcloud=WordCloud(width=1300,height=620)  
    12. wordcloud.add("",name,value,word_size_range=[20,100])  
    13. wordcloud  

    3.组合图

    [python] view plain copy
    1. from pyecharts import Line,Pie,Grid  
    2. line=Line("折线图",width=1200)  
    3. attr=["周一","周二","周三","周四","周五","周六","周日"]  
    4. line.add("最高气温",attr,[11,11,15,13,12,13,10],  
    5.         mark_point=["max","min"],mark_line=["average"])  
    6. line.add("最低气温",attr,[1,-2,2,5,3,2,0],  
    7.         mark_point=["max","min"],mark_line=["average"],  
    8.         legend_pos="20%")  
    9. attr=["衬衫","羊毛衫","雪纺衫","裤子","高跟鞋","袜子"]  
    10. v1=[11,12,13,10,10,10]  
    11. pie=Pie("饼图",title_pos="55%")  
    12. pie.add("",attr,v1,radius=[45,65],center=[65,50],  
    13.        legend_pos="80%",legend_orient="vertical")  
    14. grid=Grid()  
    15. grid.add(line,grid_right="55%")  
    16. grid.add(pie,grid_left="60%")  
    17. grid  



关键字