Python3:pyecharts数据可视化插件
Python3:pyecharts数据可视化插件
⼀、简介
pyecharts 是⼀个⽤于⽣成 Echarts 图表的类库。 Echarts 是百度开源的⼀个数据可视化 JS 库。主要⽤于数据可视化。
⼆、安装
pip install pyecharts
在线安装不成功,采⽤离线插件whl安装:
(1)下载:pyecharts-0.1.9.4-py2.py3-none-any.whl
(2)然后进⼊到所咋的⽂件夹,执⾏安装命令:
D:\whl>pip install pyecharts-0.1.9.4-py2.py3-none-any.whl
三、⽰例
1,代码:
from pyecharts import Bar
bar =Bar("我的第⼀个图表", "这⾥是副标题")
bar.add("服装", ["衬衫", "⽺⽑衫", "雪纺衫", "裤⼦", "⾼跟鞋", "袜⼦"], [5, 20, 36, 10, 75, 90])
bar.show_config()
2,效果:
3,分析:
Tip:可以按右边的下载按钮将图⽚下载到本地;
add():主要⽅法,⽤于添加图表的数据和设置各种配置项
show_config():打印输出图表的所有配置项
render():默认将会在根⽬录下⽣成⼀个 render.html 的⽂件,⽀持 path 参数,设置⽂件保存位置,如 render(r"e:my_first_chart.html"),⽂件⽤浏览器打开。
默认的编码类型为 UTF-8,在 Python3 中是没什么问题的,Python3 对中⽂的⽀持好很多。但是在 Python2 中,编码的处理是个很头疼的问题,暂时没能到完美的解决⽅法,⽬前只能通过⽂本编辑器⾃⼰进⾏⼆次编码,我⽤的是 Visual Studio Code,先通过 Gbk 编码重新打开,然后再⽤ UTF-8 重新保存,这样⽤浏览器打开的话就不会出现中⽂乱码问题了。
基本上所有的图表类型都是这样绘制的:
1. chart_name = Type() 初始化具体类型图表。
2. add() 添加数据及配置项。
3. render() ⽣成 .html ⽂件。
四、图表类型
1,Bar(柱状图/条形图)
1.1 ⽰例1:
from pyecharts import Bar
attr =["衬衫", "⽺⽑衫", "雪纺衫", "裤⼦", "⾼跟鞋", "袜⼦"]
v1 =[20, 40, 60, 80, 100, 120]
v2 =[10, 20, 30, 40, 120, 80]
bar =Bar("标记线和标记点⽰例")
bar.add("商家A", attr, v1, mark_point=["average"])
bar.add("商家B", attr, v2, mark_line=["min", "max"])
效果:
1.2 ⽰例2:
from pyecharts import Bar
attr =["衬衫", "⽺⽑衫", "雪纺衫", "裤⼦", "⾼跟鞋", "袜⼦"]
v1 =[20, 40, 60, 80, 100, 120]
v2 =[10, 20, 30, 40, 120, 80]
bar =Bar("x 轴和 y 轴交换")
bar.add("商家A", attr, v1)
bar.add("商家B", attr, v2, is_convert=True)
效果:
2,EffectScatter(带有涟漪特效动画的散点图)
#⽰例2:EffectScatter(带有涟漪特效动画的散点图)
from pyecharts import EffectScatter
v1 =[10, 20, 30, 40, 50, 60]
v2 =[25, 20, 15, 10, 60, 33]
es =EffectScatter("动态散点图⽰例")
es.add("effectScatter", v1, v2)
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")
效果:
3,Funnel(漏⽃图)
#⽰例四:Funnel(漏⽃图)
from pyecharts import Funnel
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") der()
效果:
4,Gauge(仪表盘)
#⽰例五: Gauge(仪表盘)
from pyecharts import Gauge
gauge =Gauge("仪表盘⽰例")
gauge.add("业务指标", "完成率", 66.66)
gauge.show_config()
效果:
5,Geo(地理坐标系)
5.1 ⽰例1:
#⽰例5: Geo(地理坐标系)
#5.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()
office下载
效果:
5.2 ⽰例2:
#6.2
from pyecharts import Geo
data =[("海门", 9), ("鄂尔多斯", 12), ("招远", 12), ("⾈⼭", 12), ("齐齐哈尔", 14), ("盐城", 15)]
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, type="effectScatter", is_random=True, effect_scale=5)
geo.show_config()
效果:
6, Graph(关系图)
6.1 ⽰例1:
windows云服务器#⽰例6:Graph(关系图)
#6.1
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}, { 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=8000, layout='circular', label_text_color=None)
graph.show_config()
效果:
正割余割6.2 ⽰例2:
#6.2
from pyecharts import Graph
import json
with open("..jsonweibo.json","r",encoding="utf-8") as f:
j =json.load(f)
nodes, links,categories,cont,mid,userl=jgraph=Graph("微博转发关系图", width=1200, height=600)
graph.add("",nodes,links,categories,label_pos="right", repulsion=50, is_legend_show=False, line_curve=0.2, label_text_color=None)
graph.show_config()
效果:待测试
7,Line(折线/⾯积图)
7.1 ⽰例1:
#⽰例7:Line(折线/⾯积图)
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, mark_point=["average"])
line.add("商家B", attr, v2, is_smooth=True, mark_line=["max", "average"])
line.show_config()
效果:
7.2 ⽰例2:
#7.2
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_step=True, is_label_show=True)
line.show_config()python在线编辑器python3
效果:
7.3 ⽰例3:
#7.3
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()
效果:
8,Liquid(⽔球图)
8.1 ⽰例1:
#8.1
from pyecharts import Liquid
liquid =Liquid("⽔球图⽰例")
liquid.add("Liquid", [0.6])
liquid.show_config()
效果:
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论