Plotly-express-24-绘制漏斗图
本文中介绍的是如何利用plotly
绘制漏斗,主要包含:
- 基础漏斗图
- 分组漏斗图
- 面积漏斗图
- 基于
graph_objects
实现的多种漏斗图
文中的数据大都是以电商中的UV-付款
转化过程为漏斗进行分析。
基础漏斗
代码语言:txt复制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
实现呢?
from plotly import graph_objects as go
fig = go.Figure(go.Funnel(
x = [1000, 800, 400, 200, 100, 30],
y = ["UV", "搜索", "搜藏", "加购", "下单", "付款"]))
fig.show()
分组漏斗图
代码语言:txt复制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()
面积漏斗图
代码语言:txt复制import plotly.express as px
fig = px.funnel_area(
values = [1000, 800, 400, 200, 100, 30],
names = ["UV", "搜索", "搜藏", "加购", "下单", "付款"]
)
fig.show()
使用graph_objects
实现漏斗图
基础漏斗
代码语言:txt复制from plotly import graph_objects as go
fig = go.Figure(go.Funnel(
x = [1000, 800, 400, 200, 100, 30],
y = ["UV", "搜索", "搜藏", "加购", "下单", "付款"]))
fig.show()
改变漏斗颜色和大小
代码语言:txt复制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()
多组并排漏斗
代码语言:txt复制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
代码语言:txt复制from plotly import graph_objects as go
fig = go.Figure(go.Funnelarea(
values = [940, 770, 400, 340, 100, 30],
text= ["UV", "搜索", "搜藏", "加购", "下单", "付款"]
))
fig.show()
面积漏斗中做标记
代码语言:txt复制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()
多组面积漏斗图
代码语言:txt复制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()
一切看似逝去的,都不曾离开,你所给与的爱与温暖,让我执着地守护着这里。
尤而小屋,一个温馨的小屋。小屋主人,一手代码谋求生存,一手掌勺享受生活,欢迎你的光临