pyecharts教程.docx
《pyecharts教程.docx》由会员分享,可在线阅读,更多相关《pyecharts教程.docx(25页珍藏版)》请在冰点文库上搜索。
pyecharts教程
这篇教程简单介绍一下pyecharts(echarts提供给python的接口包)简单使用,感兴趣的可以了解一下。
一.安装
pyecharts兼容python2.x和python3.x,直接输入安装命令”pipinstallpyecharts”就行。
二.使用
1.柱状图
frompyechartsimportBar
bar=Bar("我的第一个图表","这里是副标题")
bar.add("服装",["衬衫","羊毛衫","雪纺衫","裤子","高跟鞋","袜子"],[5,20,36,10,75,90])
bar.render()
程序截图:
2.水平柱状图
frompyechartsimportBar
bar=Bar("x轴和y轴交换")
attr=["衬衫","羊毛衫","雪纺衫","裤子","高跟鞋","袜子"]
v1=[5,20,36,10,75,90]
v2=[10,25,16,18,55,62]
bar.add("商家A",attr,v1)
bar.add("商家B",attr,v2,is_convert=True)
bar.render()
程序截图:
3.饼状图
frompyechartsimportPie
pie=Pie('饼状图')
pie.add('',['优秀','良好','及格'],[12,20,3],is_label_show=True)
pie.render()
程序截图:
4.带有涟漪特效动画的散点图
frompyechartsimportEffectScatter
es=EffectScatter("动态散点图各种图形示例")
es.add("",[10],[10],symbol_size=20,effect_scale=3.5,effect_period=3,symbol="pin")
es.add("",[20],[20],symbol_size=12,effect_scale=4.5,effect_period=4,symbol="rect")
es.add("",[30],[30],symbol_size=30,effect_scale=5.5,effect_period=5,symbol="roundRect")
es.add("",[40],[40],symbol_size=10,effect_scale=6.5,effect_brushtype='fill',symbol="diamond")
es.add("",[50],[50],symbol_size=16,effect_scale=5.5,effect_period=3,symbol="arrow")
es.add("",[60],[60],symbol_size=6,effect_scale=2.5,effect_period=3,symbol="triangle")
es.render()
程序截图:
5.漏斗图
frompyechartsimportFunnel
attr=["衬衫","羊毛衫","雪纺衫","裤子","高跟鞋","袜子"]
value=[20,40,60,80,100,120]
funnel=Funnel("漏斗图示例")
funnel.add("商品",attr,value,is_label_show=True,label_pos="inside",label_text_color="#fff")
funnel.render()
程序截图:
6.仪表盘
frompyechartsimportGauge
gauge=Gauge("仪表盘示例")
gauge.add("业务指标","完成率",66.66)
gauge.render()
程序截图:
7.地理坐标系图
程序一:
frompyechartsimportGeo
data=[("海门",9),("鄂尔多斯",12),("招远",12),("舟山",12),("齐齐哈尔",14),("盐城",15),("赤峰",16),("青岛",18),
("乳山",18),("金昌",19),("泉州",21),("莱西",21),("日照",21),("胶南",22),("南通",23),("拉萨",24),("云浮",24),("梅州",25)]
geo=Geo("全国主要城市空气质量","datafrompm2.5",title_color="#fff",title_pos="center",width=800,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.render()
程序运行截图:
程序二:
frompyechartsimportGeo
data=[("海门",9),("鄂尔多斯",12),("招远",12),("舟山",12),("齐齐哈尔",14),("盐城",15)]
geo=Geo("全国主要城市空气质量","datafrompm2.5",title_color="#fff",title_pos="center",width=800,height=500,background_color='#404a59')
attr,value=geo.cast(data)
geo.add("",attr,value,type="effectScatter",is_random=True,effect_scale=5)
geo.render()
程序截图:
8.关系图
程序一:
frompyechartsimportGraph
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=[]
foriinnodes:
forjinnodes:
links.append({"source":
i.get('name'),"target":
j.get('name')})
graph=Graph("关系图-环形布局示例")
graph.add("",nodes,links,is_label_show=True,repulsion=8000,layout='circular',label_text_color=None)
graph.render()
程序截图:
9.折线/面积图
程序一:
frompyechartsimportLine
attr=["衬衫","羊毛衫","雪纺衫","裤子","高跟鞋","袜子"]
v1=[5,20,36,10,10,100]
v2=[55,60,16,20,15,80]
line=Line("折线图-阶梯图示例")
line.add("商家A",attr,v1,is_step=True,is_label_show=True)
line.render()
程序截图:
程序二:
frompyechartsimportLine
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.render()
程序截图:
10.水球图
程序一:
frompyechartsimportLiquid
liquid=Liquid("水球图示例")
liquid.add("Liquid",[0.6,0.5,0.4,0.3],is_liquid_outline_show=False)
liquid.render()
程序截图:
程序二:
frompyechartsimportLiquid
liquid=Liquid("水球图示例")
liquid.add("Liquid",[0.6,0.5,0.4,0.3],is_liquid_animation=False,shape='diamond')
liquid.render()
程序截图:
11.地图
程序一:
frompyechartsimportMap
value=[155,10,66,78,33,80,190,53,49.6]
attr=["福建","山东","北京","上海","甘肃","新疆","河南","广西","西藏"]
map=Map("Map结合VisualMap示例",width=800,height=600)
map.add("",attr,value,maptype='china',is_visualmap=True,visual_text_color='#000')
map.render()
程序截图:
程序二:
frompyechartsimportMap
value=[20,190,253,77,65]
attr=['汕头市','汕尾市','揭阳市','阳江市','肇庆市']
map=Map("广东地图示例",width=800,height=500)
map.add("",attr,value,maptype='广东',is_visualmap=True,visual_text_color='#000')
map.render()
程序截图:
12.平行坐标系
frompyechartsimportParallel
c_schema=[{"dim":
0,"name":
"data"},{"dim":
1,"name":
"AQI"},{"dim":
2,"name":
"PM2.5"},{"dim":
3,"name":
"PM10"},
{"dim":
4,"name":
"CO"},{"dim":
5,"name":
"NO2"},{"dim":
6,"name":
"CO2"},
{"dim":
7,"name":
"等级","type":
"category","data":
['优','良','轻度污染','中度污染','重度污染','严重污染']}]
data=[[1,91,45,125,0.82,34,23,"良"],[2,65,27,78,0.86,45,29,"良"],[3,83,60,84,1.09,73,27,"良"],[4,109,81,121,1.28,68,51,"轻度污染"],
[5,106,77,114,1.07,55,51,"轻度污染"],[6,109,81,121,1.28,68,51,"轻度污染"],[7,106,77,114,1.07,55,51,"轻度污染"],[8,89,65,78,0.86,51,26,"良"],
[9,53,33,47,0.64,50,17,"良"],[10,80,55,80,1.01,75,24,"良"],[11,117,81,124,1.03,45,24,"轻度污染"],[12,99,71,142,1.1,62,42,"良"],
[13,95,69,130,1.28,74,50,"良"],[14,116,87,131,1.47,84,40,"轻度污染"]]
parallel=Parallel("平行坐标系-用户自定义指示器")
parallel.config(c_schema=c_schema)
parallel.add("parallel",data)
parallel.render()
程序截图:
13.极坐标系图
程序一:
frompyechartsimportPolar
radius=['周一','周二','周三','周四','周五','周六','周日']
polar=Polar("极坐标系-堆叠柱状图示例",width=1200,height=600)
polar.add("A",[1,2,3,4,3,5,1],radius_data=radius,type='barRadius',is_stack=True)
polar.add("B",[2,4,6,1,2,3,1],radius_data=radius,type='barRadius',is_stack=True)
polar.add("C",[1,2,3,4,1,2,5],radius_data=radius,type='barRadius',is_stack=True)
polar.render()
程序截图:
程序二:
frompyechartsimportPolar
radius=['周一','周二','周三','周四','周五','周六','周日']
polar=Polar("极坐标系-堆叠柱状图示例",width=1200,height=600)
polar.add("",[1,2,3,4,3,5,1],radius_data=radius,type='barAngle',is_stack=True)
polar.add("",[2,4,6,1,2,3,1],radius_data=radius,type='barAngle',is_stack=True)
polar.add("",[1,2,3,4,1,2,5],radius_data=radius,type='barAngle',is_stack=True)
polar.render()
程序截图:
14.雷达图
程序一:
frompyechartsimportRadar
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.render()
程序截图:
程序二:
frompyechartsimportRadar
value_bj=[[55,9,56,0.46,18,6,1],[25,11,21,0.65,34,9,2],[56,7,63,0.3,14,5,3],[33,7,29,0.33,16,6,4]]
value_sh=[[91,45,125,0.82,34,23,1],[65,27,78,0.86,45,29,2],[83,60,84,1.09,73,27,3],[109,81,121,1.28,68,51,4]]
c_schema=[{"name":
"AQI","max":
300,"min":
5},{"name":
"PM2.5","max":
250,"min":
20},{"name":
"PM10","max":
300,"min":
5},
{"name":
"CO","max":
5},{"name":
"NO2","max":
200},{"name":
"SO2","max":
100}]
radar=Radar()
radar.config(c_schema=c_schema,shape='circle')
radar.add("北京",value_bj,item_color="#f9713c",symbol=None)
radar.add("上海",value_sh,item_color="#b3e4a1",symbol=None)
radar.render()
程序截图:
15.散点图
frompyechartsimportScatter
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.render()
程序截图:
16.词云图
frompyechartsimportWordCloud
name=['SamSClub','Macys','AmySchumer','JurassicWorld','CharterCommunications','ChickFilA','PlanetFitness','PitchPerfect',
'Express','Home','JohnnyDepp','LenaDunham','LewisHamilton','KXAN','MaryEllenMark','FarrahAbraham','RitaOra',
'SerenaWilliams','NCAAbaseballtournament','PointBreak']
value=[10000,6181,4386,4055,2467,2244,1898,1484,1112,965,847,582,555,550,462,366,360,282,273,265]
wordcloud=WordCloud(width=1300,height=620)
wordcloud.add("",name,value,word_size_range=[20,100])
wordcloud.render()
程序截图:
17.更多图
程序一:
importmath
frompyechartsimportPolar
data=[]
foriinrange(101):
theta=i/100*360
r=5*(1+math.sin(theta/180*math.pi))
data.append([r,theta])
hour=[iforiinrange(1,25)]
polar=Polar("极坐标系示例",width=800,height=500)
polar.add("Love",data,angle_data=hour,boundary_gap=False,start_angle=0)
polar.render()
运行截图:
程序二:
importmath
frompyechartsimportPolar
data=[]
foriinrange(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.render()
程序截图:
程序三:
importrandom
frompyechartsimportPie
attr=['A','B','C','D','E','F']
pie=Pie("饼图示例",width=1000,height=600)
pie.add("",attr,[random.randint(0,100)for_inrange(6)],radius=[50,55],center=[25,50],is_random=True,is_label_show=True)
pie.add("",attr,[random.randint(20,100)for_inrange(6)],radius=[0,45],center=[25,50],rosetype='area',is_label_show=True)
pie.add("",attr,[random.randint(0,100)for_inrange(6)],radius=[50,55],center=[65,50],is_random=True