pyecharts 是一个用于生成Echarts图表的Python库。Echarts是百度开源的一个数据可视化JS库,可以生成一些非常酷炫的图表。
-
环境安装
- pip install pyecharts
-
检查版本
import pyecharts
print(pyecharts.version)
2.0.3
- 柱状图绘制
from pyecharts.charts import Bar
from pyecharts import options as opts
from pyecharts.globals import ThemeType
#v1版本开始支持链式调用
bar = (Bar(init_opts = opts.InitOpts(theme = ThemeType.WESTEROS))
.add_xaxis([“Apple”,“Huawei”,“Xiaomi”,“Oppo”,“Vivo”,“Meizu”]) #指定x轴
#可以指定多个y轴
.add_yaxis(“电商渠道”,[133,163,99,117,108,33])
.add_yaxis(“门店”,[66,87,103,78,55,77])
#全局配置操作
.set_global_opts(title_opts=opts.TitleOpts(title=“Bar-基本示例”,subtitle=“我是副标题”))
)
#在juputer notebook中渲染
bar.render_notebook()
- 添加工具箱配置
- 在全局配置set_global_opts中添加:
- toolbox_opts=opts.ToolboxOpts()
- 在全局配置set_global_opts中添加:
from pyecharts.charts import Bar
from pyecharts import options as opts
#示例数据
cate = [‘Apple’,‘Huawei’,‘Xiaomi’,‘Oppo’,‘Vivo’,‘Meizu’]
data1 = [133,163,99,117,108,33]
data2 = [66,87,103,78,55,77]
#1.x版本支持链式调用
bar = (Bar()
.add_xaxis(cate) #指定x轴
#可以指定多个y轴
.add_yaxis(‘电商渠道’,data1)
.add_yaxis(‘门店’,data2)
.set_global_opts(title_opts=opts.TitleOpts(title=“Bar-基本示例”,subtitle=“我是副标题”)
,toolbox_opts=opts.ToolboxOpts() #添加工具箱代码
)
)
#在jupyter notebook中渲染
bar.render_notebook()
- 饼图绘制
cate = [‘Apple’, ‘Huawei’, ‘Xiaomi’, ‘Oppo’, ‘Vivo’, ‘Meizu’]
data = [163, 134, 117, 109, 99, 56]
[list(z) for z in zip(cate,data)]
from pyecharts.charts import Pie
from pyecharts import options as opts
#示例数据
cate = [‘Apple’, ‘Huawei’, ‘Xiaomi’, ‘Oppo’, ‘Vivo’, ‘Meizu’]
data = [163, 134, 117, 109, 99, 56]
pie = (Pie()
.add(‘’,[list(z) for z in zip(cate,data)],
radius=[“30%”,“75%”], #设置半径(内外圈半径)
rosetype=“radius” #半径形式的玫瑰型样式(经典)
)
.set_global_opts(title_opts=opts.TitleOpts(title=“Pie-基本示例”,subtitle=“我是副标题”))
.set_series_opts(label_opts=opts.LabelOpts(formatter=“{b}:{d}%”))
)
pie.render_notebook()
- 折线图
from pyecharts.charts import Line
from pyecharts import options as opts
#示例数据
cate = [‘Apple’, ‘Huawei’, ‘Xiaomi’, ‘Oppo’, ‘Vivo’, ‘Meizu’]
data1 = [133,163,99,117,108,33]
data2 = [66,87,103,78,55,77]
“”"
折线图示例:
- is_smooth 折线 OR 平滑
- markline_opts 标记线 OR 标记点
“”"
line = (Line()
.add_xaxis(cate)
.add_yaxis(‘电商渠道’,data1,
#均值标记线
markline_opts=opts.MarkLineOpts(data=[opts.MarkAreaItem(type_=“average”)]))
.add_yaxis(‘门店’,data2,
is_smooth=True,
markline_opts=opts.MarkLineOpts(data=[opts.MarkLineItem(type_=“average”)])
)
.set_global_opts(title_opts=opts.TitleOpts(title=“Line-基本示例”,subtitle=“我是副标题”))
)
line.render_notebook()
- 漏斗图
from pyecharts.charts import Funnel
from pyecharts import options as opts
示例数据
cate = [‘访问’, ‘注册’, ‘加入购物车’, ‘提交订单’, ‘付款成功’]
data = [30408, 15240, 10055, 8119, 5708]
“”"
漏斗图示例:
- sort_控制排序,默认降序;
- label_opts标签显示位置
“”"
funnel = (Funnel()
.add(“用户数”,[list(z) for z in zip(cate,data)],
sort_=‘ascending’,
label_opts=opts.LabelOpts(position=“inside”)
)
.set_global_opts(title_opts=opts.TitleOpts(title=“Funnel-基本示例”,subtitle=“我是副标题”))
)
funnel.render_notebook()
- MapGlobe
import pyecharts.options as opts
from pyecharts.charts import MapGlobe
from pyecharts.faker import POPULATION
data = [x for _, x in POPULATION[1:]]
low, high = min(data), max(data)
m = (
MapGlobe()
.add_schema()
.add(
maptype=“world”,
series_name=“World Population”,
data_pair=POPULATION[1:],
is_map_symbol_show=False,
label_opts=opts.LabelOpts(is_show=False),
)
.set_global_opts(
visualmap_opts=opts.VisualMapOpts(
min_=low,
max_=high,
range_text=[“max”, “min”],
is_calculable=True,
range_color=[“lightskyblue”, “yellow”, “green”],
)
)
)
m.render_notebook()
- 仪表盘
import pyecharts.options as opts
from pyecharts.charts import Gauge
g = (
Gauge()
.add(series_name=“业务指标”, data_pair=[[“完成率”, 59.5]])
.set_global_opts(
legend_opts=opts.LegendOpts(is_show=False),
tooltip_opts=opts.TooltipOpts(is_show=True, formatter=“{a}
{b} : {c}%”),
)
)
g.render_notebook()
- 关系图
from pyecharts import options as opts
from pyecharts.charts import Graph
nodes = [
{“name”: “结点1”, “symbolSize”: 20},
{“name”: “结点2”, “symbolSize”: 30},
{“name”: “结点3”, “symbolSize”: 40},
{“name”: “结点4”, “symbolSize”: 50},
{“name”: “结点5”, “symbolSize”: 60},
{“name”: “结点6”, “symbolSize”: 50},
{“name”: “结点7”, “symbolSize”: 40},
{“name”: “结点8”, “symbolSize”: 30},
]
links = []
for i in nodes:
for j in nodes:
links.append({“source”: i.get(“name”), “target”: j.get(“name”)})
g = (
Graph()
.add(“”, nodes, links, repulsion=8000)
.set_global_opts(title_opts=opts.TitleOpts(title=“我是关系图”))
)
g.render_notebook()
- 水球图
from pyecharts import options as opts
from pyecharts.charts import Grid, Liquid
from pyecharts.commons.utils import JsCode
l1 = (
Liquid()
.add(“lq”, [0.7, 0.8], center=[“70%”, “60%”])
.set_global_opts(title_opts=opts.TitleOpts(title=“我们是水球图”))
)
l2 = Liquid().add(
“lq”,
[0.4318],
center=[“35%”, “60%”],
label_opts=opts.LabelOpts(
font_size=50,
formatter=JsCode(
“”“function (param) {
return (Math.floor(param.value * 10000) / 100) + ‘%’;
}”“”
),
position=“inside”,
),
)
grid = Grid().add(l1, grid_opts=opts.GridOpts()).add(l2, grid_opts=opts.GridOpts())
grid.render_notebook()
源文件可在这里下载:
https://download.csdn.net/download/ak2111/89039929?spm=1001.2014.3001.5501