9.pyecharts漏斗图

一、适用条件
1、漏斗图:展示部分转化率,整体转化率;
2、有个缺点,内置的百分数{d}指的是全部的百分比,自己传入的需要是小数,整理好的比例;当然也可以直接传入数据,不过只能展示整体数据是多少
二、代码实现
1.导入所需包

from typing import ValuesView
from numpy.lib.function_base import append
from pyecharts import options as opts
from pyecharts.charts import Funnel
from pyecharts.faker import Faker
from pyecharts.commons.utils import JsCode
from pyecharts.globals import ThemeType
from pyecharts.render import make_snapshot
#from snapshot_phantomjs import snapshot
from snapshot_pyppeteer import snapshot
import pandas as pd
import numpy as np 

2.数据整理

df = pd.read_excel('picture.xlsx',sheet_name='funnel')
x_list = list(df["步骤"])
y_list = list(df["数量"].loc[:0])
b= list(df["数量"].loc[:3])
y_list.extend(b)
y1_list = list(df["数量"])
ratio_sec = list(("%.2f%%" % ((a/b)*100)) for a,b in zip (y1_list,y_list))
ratio_all = list(round(((a/10000)*100),2) for a in y1_list)
x_data = [[] for i in range(len(x_list))]
c = {}
for idx, i in enumerate(x_list):
    c[i] = ratio_sec[idx]
# print(c)
x1_list = []
for i in c:
    need = '{}({})'.format(i,c[i])
    x1_list.append(need)
# print(x1_list)

3 .漏斗图

c = (
    Funnel(init_opts=opts.InitOpts(width="1000px", height="500px"))
    .add("商家", 
        # data_pair=data,
        [list(z) for z in zip(x1_list, ratio_all)],
        sort_="descending", #"ascending"
        tooltip_opts=opts.TooltipOpts(trigger="item", formatter="{a}\n{b} : ({c})"),
        label_opts=opts.LabelOpts(
            formatter="{a}\n{b} : ({c}%)",
            is_show=True,
            position="inside",                                                
        ),
       itemstyle_opts=opts.ItemStyleOpts(border_color="#fff", border_width=1),
    )
    .set_global_opts(
        title_opts=opts.TitleOpts(title="Funnel-基本示例"),
        toolbox_opts=opts.ToolboxOpts(is_show=True),
    )

    .render("1.html")
)
#########
9_1.png

你可能感兴趣的:(9.pyecharts漏斗图)