前言:用pyecharts v1.x版本来绘制水球图。
注:Ppyecharts分为 v0.5.x 和 v1.x 两个大版本,v0.5.x 和 v1.x 间不兼容,v0.5.x是基于Python2.7+、3.4+版本开发的,而v1.x是一个全新的版本,它是基于Python3.6+版本开发的,另外经PyEcharts开发团队决定,0.5.x 版本将不再进行维护。
前提:
# 安装pyecharts模块,直接安装就是最新的版本
pip install pyecharts
#导包
from pyecharts import options as opts
from pyecharts.charts import Liquid
水球图数据很简单,就是一个完成率数字,所以不用定义或导入,也无需做转换。
接下来就可以绘制水球图了:
from pyecharts import options as opts
from pyecharts.charts import Liquid
c = (
#创建水球图对象
Liquid()
#设置数据系列名称及数据,is_outline_show是设置边框线,根据自身喜欢选择
.add("达成率", [0.66], is_outline_show = False)
# 设置图表标题及位置
.set_global_opts(title_opts = opts.TitleOpts(title = "蔬菜部门销售达成率",pos_left = "center"))
#通过render()方法将水球图渲染为html
# .render("圆形水球图.html")
)
c.render_notebook()
水球图的颜色可以设置,只需要add()中添加color参数,代码如下:
from pyecharts import options as opts
from pyecharts.charts import Liquid
c = (
Liquid()
.add("达成率", [0.88], is_outline_show = False, color = ['#DC143C'])
.set_global_opts(title_opts = opts.TitleOpts(title = "水果部门销售达成率",pos_left = "center"))
# .render("圆形水球图.html")
)
c.render_notebook()
红色的水球:
水球的形状也可以自定义:
from pyecharts import options as opts
from pyecharts.charts import Liquid
from pyecharts.globals import SymbolType
c = (
Liquid()
.add("达成率", [0.777], is_outline_show = False, shape = SymbolType.RECT)
.set_global_opts(title_opts = opts.TitleOpts(title = "水产销售达成率",pos_left = "center"))
# .render("矩形水球图.html")
)
c.render_notebook()
矩形水球:
from pyecharts import options as opts
from pyecharts.charts import Liquid
from pyecharts.globals import SymbolType
c = (
Liquid()
.add("达成率", [0.90], is_outline_show = False, shape = SymbolType.DIAMOND)
.set_global_opts(title_opts = opts.TitleOpts(title = "综合部门销售达成率",pos_left = "center"))
# .render("矩形水球图.html")
)
c.render_notebook()
看出区别了吗?只要在add()中添加shape=SymbolType.RECT的设置,同时需要导入对应的模块:from pyecharts.globals import SymbolType
另外shape=SymbolType.DIAMOND就是钻石水球图
from pyecharts import options as opts
from pyecharts.charts import Liquid
from pyecharts.globals import SymbolType
c = (
Liquid()
.add("达成率", [0.60], is_outline_show = False, shape = SymbolType.ROUND_RECT)
.set_global_opts(title_opts = opts.TitleOpts(title = "电商部门销售达成率",pos_left = "center"))
# .render("矩形水球图.html")
)
c.render_notebook()
shape=SymbolType.ROUND_RECT就是圆角矩形水球图
from pyecharts import options as opts
from pyecharts.charts import Liquid
from pyecharts.globals import SymbolType
c = (
Liquid()
.add("达成率", [0.67], is_outline_show = False, shape = SymbolType.TRIANGLE)
.set_global_opts(title_opts = opts.TitleOpts(title = "猪肉部门销售达成率",pos_left = "center"))
# .render("矩形水球图.html")
)
c.render_notebook()
shape=SymbolType.TRIANGLE就是三角形水球图
from pyecharts import options as opts
from pyecharts.charts import Liquid
from pyecharts.globals import SymbolType
c = (
Liquid()
.add("达成率", [0.56], is_outline_show = False, shape = SymbolType.ARROW)
.set_global_opts(title_opts = opts.TitleOpts(title = "物料部门销售达成率",pos_left = "center"))
# .render("矩形水球图.html")
)
c.render_notebook()
shape=SymbolType.ARROW就是箭头水球图
制作水球图不是很简单的吗(?_?)