天天看点

plotly怎么绘制漏斗图?

1、案例一

trace = go.Funnel(
    y = ["注册", "实名认证", "绑卡", "合格投资者", "投资", "复投"],
    x = [220, 180, 160, 80, 40, 20],
    textinfo = "value+percent initial",
    marker=dict(color=["deepskyblue", "lightsalmon", "tan", "teal", "silver", "yellow"]),
    connector = {"line": {"color": "royalblue", "dash": "solid", "width": 3}})
    
data =[trace]

fig = go.Figure(data)

fig.show()      

结果如下:

plotly怎么绘制漏斗图?

2、案例二:绘制不同网站的对比转化率漏斗图

import plotly.express as px
import pandas as pd

df = pd.read_excel("funnel.xlsx")
display(df)
fig = px.funnel(df, x='数值', y='状态', color='网站')
fig.show()

df1 = df[df["网站"] == "A"]
df2 = df[df["网站"] == "B"]

fig = go.Figure()

trace0 = go.Funnel(x = df1["数值"],y = df1["状态"],name = 'A网站',
                   textinfo = "value+percent initial",
                   marker=dict(color=["red"]*5),
                   connector = {"line": {"color": "lightsalmon", "dash": "solid", "width": 3}},
                   opacity=0.49)

trace1 = go.Funnel(x = df2["数值"],y = df2["状态"],name = 'B网站',
                   textinfo = "value+percent previous",orientation = "h",
                   marker=dict(color=["deepskyblue"]*5),
                   connector = {"line": {"color": "lightsalmon", "dash": "solid", "width": 3}})


fig.add_trace(trace0)
fig.add_trace(trace1)

fig.show()