高級(jí)可視化神器Plotly玩轉(zhuǎn)漏斗圖

可視化神器Plotly玩轉(zhuǎn)漏斗圖

本文中詳細(xì)介紹的是如何利用plotly來繪制漏斗圖


image

認(rèn)識(shí)漏斗圖

漏斗圖是銷售領(lǐng)域一種十分常用的圖表,主要是用來分析在各個(gè)階段的流失和轉(zhuǎn)化情況。比如在某個(gè)商城中,我們統(tǒng)計(jì)用戶在不同階段的人數(shù)來分析轉(zhuǎn)化率:

  • 商城UV:商城每天的訪問人數(shù)
  • 搜索人數(shù):在商城有過搜索行為的用戶數(shù)
  • 加購人數(shù):有加購行為的用戶數(shù)
  • 提交訂單:有多少用戶提交訂單
  • 點(diǎn)擊支付:提交訂單之后有多少用戶點(diǎn)擊支付按鈕
  • 支付成功:最終支付成功的用戶數(shù)

從搜索人數(shù)開始到支付成功,每個(gè)階段用戶都存在一定的流失,漏斗圖就能很好地將這種流失和轉(zhuǎn)化情況顯示出來。

除去柱狀圖、餅圖、折線圖,漏斗圖應(yīng)該是自己在工作畫的最為頻繁的一種圖表。下面我們通過模擬某個(gè)電商網(wǎng)站的用戶行為來繪制漏斗圖。

整體效果

導(dǎo)入庫

基于兩種方式實(shí)現(xiàn):

  • plotly_express :px
  • plotly.graph_objects:go
import pandas as pd
import numpy as np

# plotly兩個(gè)繪圖接口
import plotly_express  as px
import plotly.graph_objects as go

基于px實(shí)現(xiàn)

基礎(chǔ)漏斗圖

模擬一份商城數(shù)據(jù):

data1 = pd.DataFrame({
    "number": [1200,900,700,400,180,100],
    "stage": ["瀏覽網(wǎng)站","搜索","加購","提交訂單","點(diǎn)擊支付","支付成功"]}
)
data1
image

繪制一個(gè)基礎(chǔ)漏斗圖:

# 繪圖
fig = px.funnel(
    data1,  # 待繪圖數(shù)據(jù)
    x="number",  # x軸的數(shù)據(jù)
    y="stage"  # y軸的數(shù)據(jù)
)

fig.show()
image

我們還可以加上一個(gè)顏色參數(shù)color:

# 加上顏色參數(shù)color

fig = px.funnel(
  data1,
  x="number",
  y="stage",
  color="number"  # 顏色參數(shù)
)

fig.show()
image

如果我們的顏色設(shè)置成number列,用數(shù)值意義似乎不準(zhǔn)確,更好地應(yīng)該是不同的階段表示,換成stage:

# 加上顏色參數(shù)color

fig = px.funnel(
  data1,
  x="number",
  y="stage",
  color="stage"   # ?。?!改成stage
)

fig.show()
image

看生成的漏斗圖:最上面的是支付成功,通常我們希望的是數(shù)值最大的在最上面,于是我們將傳入的數(shù)據(jù)翻轉(zhuǎn)一下:

# 加上顏色參數(shù)color

fig = px.funnel(
  data1[::-1],   # ?。?!數(shù)據(jù)翻轉(zhuǎn)
  x="number",
  y="stage",
  color="stage"   # !?。「某蓅tage
)

fig.show()
image

分組漏斗

分組漏斗表示的含義將不同組別的漏斗圖放在一個(gè)畫布中,比如:看今年3月和2020年3月的數(shù)據(jù)對(duì)比情況,這叫做同比

同比:相同時(shí)期的對(duì)比,比如:2021年3月和2020年3月

環(huán)比:相鄰時(shí)期的對(duì)比,比如:2021年3月和2021年2月

1、2020年3月份數(shù)據(jù)

image

2、2021年3月份數(shù)據(jù)

image

3、使用concat函數(shù)合并兩組數(shù)據(jù)

image

4、繪制漏斗圖

# 繪圖

fig = px.funnel(df3,x="number",y="stages",color="time")
fig.show()
image

繪制面積漏斗圖

還是使用最上面的數(shù)據(jù):

image
fig = px.funnel_area(
    data1,
    names = "stage",
    values = "number",
)

fig.show()
image

我們觀察到:面積漏斗圖默認(rèn)繪制的百分比,而普通漏斗圖是數(shù)值

基于go實(shí)現(xiàn)

繪制基礎(chǔ)漏斗圖

from plotly import graph_objects as go

fig = go.Figure(go.Funnel(
    x=[1000,800,400,100],
    y=["瀏覽網(wǎng)站","加購","點(diǎn)擊支付","支付成功"]
))

fig.show()
image

改變漏斗圖的顏色和邊框

from plotly import graph_objects as go

fig = go.Figure(go.Funnel(
    x=[1000,800,400,100],  # 數(shù)據(jù)
    y=["瀏覽網(wǎng)站","加購","點(diǎn)擊支付","支付成功"], # 每個(gè)階段的名稱
    textposition = "inside",  #  文本位置:['inside', 'outside', 'auto', 'none'] 
    textinfo = "value + percent initial",   # 顯示文本信息 ['label', 'text', 'percent initial', 'percent previous', 'percent total', 'value'] 前面選項(xiàng)的任意組合
    opacity = 0.65, 
    marker = {"color": ["deepskyblue", "lightsalmon", "tan", "silver"],
              "line": {"width": [4, 2, 3, 1, 1], 
                       "color": ["wheat", "wheat", "blue", "yellow"]}},
    connector = {"line": {"color": "royalblue", "dash": "dot", "width": 3}})
    )

fig.show()
image

我們需要主題textinfo參數(shù)的使用,它有3種使用方式:

  • percent initial:相對(duì)于初始值
  • percent previous:相對(duì)于前一個(gè)值
  • percent total:相對(duì)于整體數(shù)值

上面這個(gè)漏斗圖使用的是percent initial(相對(duì)于初始值),百分比是這樣來的:

1000/1000 = 100%
800 / 1000 = 80%
400 / 1000 = 40%
100 / 1000 = 10%

如果是percent previous:

image
1000/1000 = 100%
800 / 1000 = 80%
400 / 800 = 50%
100 / 400 = 25%

如果是percent total:

image

我們看看第一個(gè)百分比是如何計(jì)算的:

image

分組漏斗

from plotly import graph_objects as go

stage = ["瀏覽網(wǎng)站","加購","點(diǎn)擊支付","支付成功"]

fig = go.Figure()

fig.add_trace(go.Funnel(
    name = "2020年3月",  # 圖形軌跡名稱
    x = [1000,800,400,200],  # 數(shù)據(jù)
    y = stage, # 每個(gè)階段名稱
    orientation = "h",  # 方位
    textposition = "inside",  # 文本內(nèi)容的位置
    textinfo = "value+percent previous"  # 顯示文本內(nèi)容
))


fig.add_trace(go.Funnel(
    name = "2021年2月",   # 名稱和數(shù)據(jù)需要改變
    x = [1200,900,500,240],  
    y = stage, 
    orientation = "h",  
    textposition = "inside",  
    textinfo = "value+percent total"  
))

fig.add_trace(go.Funnel(
    name = "2021年3月",  # 名稱和數(shù)據(jù)需要改變
    x = [1500,1000,450,300],  
    y = stage, 
    orientation = "h",  
    textposition = "inside",  
    textinfo = "label+percent initial"  
))


fig.show()
image

在上面的圖中,既可以觀察到同比情況(2020年3月和2021年3月),也可以觀察到環(huán)比情況(2021年3月和2月)

面積漏斗

from plotly import graph_objects as go

fig = go.Figure(go.Funnelarea(
    text = ["瀏覽網(wǎng)站","加購","點(diǎn)擊支付","支付成功"],
    values = [5000, 2000, 800, 500],

))

fig.show()
image

設(shè)置面積漏斗的顏色和邊框

from plotly import graph_objects as go

fig = go.Figure(go.Funnelarea(
    values = [3000, 2000, 800, 500], 
    text = ["瀏覽網(wǎng)站","加購","點(diǎn)擊支付","支付成功"],
    marker = {"colors": ["deepskyblue", "lightsalmon", "teal", "silver"],  # 顏色
              "line": {"color": ["red", "blue", "wheat", "wheat"],   # 邊框顏色和線條寬度
                       "width": [0, 1, 3, 5]}},
    textfont = {"family": "Old Standard TT, serif", "size": 13,   # 字體設(shè)置
                "color": "black"}, 
    opacity = 0.65))  # 透明度
fig.show()
image

多組面積漏斗

不同組別的漏斗圖我們也可以分開放,將它們放在一個(gè)大的畫布中:

from plotly import graph_objects as go

fig = go.Figure()

# 添加四組數(shù)據(jù):第一季度到第四季度
fig.add_trace(go.Funnelarea(
    scalegroup = "first",   # 組別名稱
    text = ["瀏覽網(wǎng)站","搜索","加購","提交訂單","點(diǎn)擊支付","支付成功"],
    values = [500, 450, 340, 230, 220, 110],  # 數(shù)據(jù)
    textinfo = "value",  # 顯示文本信息
    title = {"position": "top center",  # 標(biāo)題頂部居中
             "text": "第一季度"  # 標(biāo)題內(nèi)容
            },
    domain = {"x": [0, 0.5], # 圖形位置
              "y": [0, 0.5]
             }))

fig.add_trace(go.Funnelarea(
    scalegroup = "first", 
    text = ["瀏覽網(wǎng)站","搜索","加購","提交訂單","點(diǎn)擊支付","支付成功"],
    values = [600, 500, 400, 300, 200, 100], 
    textinfo = "value",
    title = {"position": "top center", 
             "text": "第二季度"},
    domain = {"x": [0, 0.5], 
              "y": [0.55, 1]}))

fig.add_trace(go.Funnelarea(
    scalegroup = "second", 
    text = ["瀏覽網(wǎng)站","搜索","加購","提交訂單","點(diǎn)擊支付","支付成功"],
    values = [510, 480, 440, 330, 220, 100], 
    textinfo = "value",
    title = {"position": "top left", 
             "text": "第三季度"},
    domain = {"x": [0.55, 1], 
              "y": [0, 0.5]}))

fig.add_trace(go.Funnelarea(
    scalegroup = "second", 
    text = ["瀏覽網(wǎng)站","搜索","加購","提交訂單","點(diǎn)擊支付","支付成功"],
    values = [360, 250, 240, 130, 120, 60],
    textinfo = "value", 
    title = {"position": "top right", 
             "text": "第四季度"},
    domain = {"x": [0.55, 1], 
              "y": [0.55, 1]}))

fig.update_layout(
    margin = {"l": 100, "r": 100},   # 整個(gè)圖形到左右邊框的距離
    shapes = [
            {"x0": 0, "x1": 0.5, "y0": 0, "y1": 0.5},   # 添加4組位置
            {"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()
image

漏斗圖真的很好用,能夠觀察到數(shù)據(jù)在不同階段的轉(zhuǎn)化情況。畢竟領(lǐng)導(dǎo)每個(gè)月都要看一下??

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 夜鶯2517閱讀 128,155評(píng)論 1 9
  • 我是黑夜里大雨紛飛的人啊 1 “又到一年六月,有人笑有人哭,有人歡樂有人憂愁,有人驚喜有人失落,有的覺得收獲滿滿有...
    陌忘宇閱讀 8,834評(píng)論 28 54
  • 兔子雖然是枚小碩 但學(xué)校的碩士四人寢不夠 就被分到了博士樓里 兩人一間 在學(xué)校的最西邊 靠山 兔子的室友身體不好 ...
    待業(yè)的兔子閱讀 2,767評(píng)論 2 9
  • 信任包括信任自己和信任他人 很多時(shí)候,很多事情,失敗、遺憾、錯(cuò)過,源于不自信,不信任他人 覺得自己做不成,別人做不...
    吳氵晃閱讀 6,365評(píng)論 4 8

友情鏈接更多精彩內(nèi)容