Fork me on GitHub

plotly-express-24-绘制漏斗图

Plotly-express-24-绘制漏斗图

本文中介绍的是如何利用plotly绘制漏斗,主要包含:

  • 基础漏斗图
  • 分组漏斗图
  • 面积漏斗图
  • 基于graph_objects实现的多种漏斗图

文中的数据大都是以电商中的UV-付款转化过程为漏斗进行分析。

基础漏斗

1
2
3
4
5
6
7
8
9
10
import plotly_express as px

data = dict( # 创建原始数据
number = [1000, 800, 400, 200, 100, 30],
stage = ["UV", "搜索", "搜藏", "加购", "下单", "付款"]
)

# 设置数据和数轴
fig = px.funnel(data, x="number", y="stage")
fig.show()

上面的例子是通过plotly_express实现的,如何使用graph_objects 实现呢?

1
2
3
4
5
6
7
from plotly import graph_objects as go

fig = go.Figure(go.Funnel(
x = [1000, 800, 400, 200, 100, 30],
y = ["UV", "搜索", "搜藏", "加购", "下单", "付款"]))

fig.show()

分组漏斗图

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import plotly_express as px
import pandas as pd

stages = ["UV", "搜索", "搜藏", "加购", "下单", "付款"]

df1 = pd.DataFrame(dict(
number = [1000, 800, 400, 200, 100, 60],
stage = stages
))


df1["time"] = "2021年1月"

df2 = pd.DataFrame(dict(
number = [900, 820, 440, 310, 80, 25],
stage = stages
))

df2["time"] = "2020年1月"

df = pd.concat([df1,df2],axis=0)

print(df)

fig = px.funnel(df, x="number", y="stage",color="time")
fig.show()

面积漏斗图

1
2
3
4
5
6
import plotly.express as px
fig = px.funnel_area(
values = [1000, 800, 400, 200, 100, 30],
names = ["UV", "搜索", "搜藏", "加购", "下单", "付款"]
)
fig.show()

使用graph_objects 实现漏斗图

基础漏斗

1
2
3
4
5
6
7
from plotly import graph_objects as go

fig = go.Figure(go.Funnel(
x = [1000, 800, 400, 200, 100, 30],
y = ["UV", "搜索", "搜藏", "加购", "下单", "付款"]))

fig.show()

改变漏斗颜色和大小

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
from plotly import graph_objects as go

fig = go.Figure(go.Funnel(
x = [1000, 800, 400, 200, 100, 30],
y = ["UV", "搜索", "搜藏", "加购", "下单", "付款"],
textposition = "inside",
textinfo = "value",
opacity = 0.65,
marker = {"color": ["deepskyblue", "lightsalmon", "tan", "teal", "silver"],
"line": {"width": [4, 2, 2, 3, 1, 1],
"color": ["wheat", "wheat", "blue", "wheat", "wheat"]}},
connector = {"line": {"color": "royalblue",
"dash": "dot",
"width": 3}})
)

fig.show()

多组并排漏斗

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
from plotly import graph_objects as go

fig = go.Figure()

fig.add_trace(go.Funnel(
name = '2020年1月',
x = [900, 700, 440, 220, 80, 20],
y = ["UV", "搜索", "搜藏", "加购", "下单", "付款"],
textposition = "inside",
textinfo = "value+percent initial"))

fig.add_trace(go.Funnel(
name = '2020年12月',
orientation = "h",
x = [1000, 900, 460, 300, 170, 50],
y = ["UV", "搜索", "搜藏", "加购", "下单", "付款"],
textposition = "inside",
textinfo = "value+percent previous"))

fig.add_trace(go.Funnel(
name = '2021年1月',
orientation = "h",
x = [940, 770, 400, 340, 100, 30],
y = ["UV", "搜索", "搜藏", "加购", "下单", "付款"],
textposition = "outside",
textinfo = "value+percent total"))

fig.show()

面积漏斗Funnelarea

1
2
3
4
5
6
7
8
from plotly import graph_objects as go

fig = go.Figure(go.Funnelarea(
values = [940, 770, 400, 340, 100, 30],
text= ["UV", "搜索", "搜藏", "加购", "下单", "付款"]
))

fig.show()

面积漏斗中做标记

1
2
3
4
5
6
7
8
9
10
11
12
13
14
from plotly import graph_objects as go

fig = go.Figure(
go.Funnelarea(
values = [940, 770, 400, 340, 100, 30],
text= ["UV","搜索","搜藏","加购","下单","付款"],
marker = {"colors": ["deepskyblue", "lightsalmon", "tan", "teal", "silver"],
"line": {"color": ["wheat", "wheat", "blue", "wheat", "wheat"],
"width": [0, 1, 5, 0, 3]}}, # 外围线性宽度
textfont = {"family": "Old Standard TT, serif",
"size": 13,
"color": "black"},
opacity = 0.65))
fig.show()

多组面积漏斗图

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
from plotly import graph_objects as go

fig = go.Figure() # 画布实例

fig.add_trace(go.Funnelarea(
scalegroup = "first", # 组别
values = [500, 450, 340, 230, 220, 110], # 数据
textinfo = "value", # 漏斗中显示的内容
title = {"position": "top center", # 标题位置:顶部居中
"text": "group 1"}, # 标题名称
domain = {"x": [0, 0.5], "y": [0, 0.5]})) # 图形位置

fig.add_trace(go.Funnelarea(
scalegroup = "first",
values = [600, 500, 400, 300, 200, 100],
textinfo = "value",
title = {"position": "top center",
"text": "group 2"},
domain = {"x": [0, 0.5], "y": [0.55, 1]}))

fig.add_trace(go.Funnelarea(
scalegroup = "first",
values = [510, 480, 440, 330, 220, 100],
textinfo = "value",
title = {"position": "top left",
"text": "group 3"},
domain = {"x": [0.55, 1], "y": [0, 0.5]}))

fig.add_trace(go.Funnelarea(
scalegroup = "first",
values = [360, 250, 240, 130, 120, 60],
textinfo = "value",
title = {"position": "top left",
"text": "group 4"},
domain = {"x": [0.55, 1], "y": [0.55, 1]}))

fig.update_layout(
margin = {"l": 200, "r": 200},
shapes = [
{"x0": 0, "x1": 0.5, "y0": 0, "y1": 0.5},
{"x0": 0, "x1": 0.5, "y0": 0.55, "y1": 1},
{"x0": 0.55, "x1": 1, "y0": 0, "y1": 0.5},
{"x0": 0.55, "x1": 1, "y0": 0.55, "y1": 1}
])

fig.show()

本文标题:plotly-express-24-绘制漏斗图

发布时间:2021年03月25日 - 14:03

原始链接:http://www.renpeter.cn/2021/03/25/plotly-express-24-%E7%BB%98%E5%88%B6%E6%BC%8F%E6%96%97%E5%9B%BE.html

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

Coffee or Tea