自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

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

大數(shù)據(jù) 數(shù)據(jù)可視化
從搜索人數(shù)開始到支付成功,每個階段用戶都存在一定的流失,漏斗圖就能很好地將這種流失和轉(zhuǎn)化情況顯示出來。除去柱狀圖、餅圖、折線圖,漏斗圖應該是自己在工作畫的最為頻繁的一種圖表。下面我們通過模擬某個電商網(wǎng)站的用戶行為來繪制漏斗圖。

[[408887]]

本文轉(zhuǎn)載自微信公眾號「尤而小屋」,作者Peter  。轉(zhuǎn)載本文請聯(lián)系Peter的公眾號。

認識漏斗圖

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

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

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

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

整體效果

看一個漏斗圖的整體效果:

視頻

導入庫

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

  • plotly_express :px
  • plotly.graph_objects:go
  1. import pandas as pd 
  2. import numpy as np 
  3.  
  4. # plotly兩個繪圖接口 
  5. import plotly_express  as px 
  6. import plotly.graph_objects as go 

基于px實現(xiàn)

基礎漏斗圖

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

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

繪制一個基礎漏斗圖:

  1. # 繪圖 
  2. fig = px.funnel( 
  3.     data1,  # 待繪圖數(shù)據(jù) 
  4.     x="number",  # x軸的數(shù)據(jù) 
  5.     y="stage"  # y軸的數(shù)據(jù) 
  6.  
  7. fig.show() 

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

  1. # 加上顏色參數(shù)color 
  2.  
  3. fig = px.funnel( 
  4.   data1, 
  5.   x="number"
  6.   y="stage"
  7.   color="number"  # 顏色參數(shù) 
  8.  
  9. fig.show() 

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

  1. # 加上顏色參數(shù)color 
  2.  
  3. fig = px.funnel( 
  4.   data1, 
  5.   x="number"
  6.   y="stage"
  7.   color="stage"   # ?。?!改成stage 
  8.  
  9. fig.show() 

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

  1. # 加上顏色參數(shù)color 
  2.  
  3. fig = px.funnel( 
  4.   data1[::-1],   # ?。?!數(shù)據(jù)翻轉(zhuǎn) 
  5.   x="number"
  6.   y="stage"
  7.   color="stage"   # ?。?!改成stage 
  8.  
  9. fig.show() 

分組漏斗

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

同比:相同時期的對比,比如:2021年3月和2020年3月

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

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

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

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

4、繪制漏斗圖

  1. # 繪圖 
  2.  
  3. fig = px.funnel(df3,x="number",y="stages",color="time"
  4. fig.show() 

繪制面積漏斗圖

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

  1. fig = px.funnel_area( 
  2.     data1, 
  3.     names = "stage"
  4.     values = "number"
  5.  
  6. fig.show() 

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

基于go實現(xiàn)

繪制基礎漏斗圖

  1. from plotly import graph_objects as go 
  2.  
  3. fig = go.Figure(go.Funnel( 
  4.     x=[1000,800,400,100], 
  5.     y=["瀏覽網(wǎng)站","加購","點擊支付","支付成功"
  6. )) 
  7.  
  8. fig.show() 

改變漏斗圖的顏色和邊框

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

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

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

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

  1. 1000/1000 = 100% 
  2. 800 / 1000 = 80% 
  3. 400 / 1000 = 40% 
  4. 100 / 1000 = 10% 

如果是percent previous:

  1. 1000/1000 = 100% 
  2. 800 / 1000 = 80% 
  3. 400 / 800 = 50% 
  4. 100 / 400 = 25% 

如果是percent total:

我們看看第一個百分比是如何計算的:

分組漏斗

  1. from plotly import graph_objects as go 
  2.  
  3. stage = ["瀏覽網(wǎng)站","加購","點擊支付","支付成功"
  4.  
  5. fig = go.Figure() 
  6.  
  7. fig.add_trace(go.Funnel( 
  8.     name = "2020年3月",  # 圖形軌跡名稱 
  9.     x = [1000,800,400,200],  # 數(shù)據(jù) 
  10.     y = stage, # 每個階段名稱 
  11.     orientation = "h",  # 方位 
  12.     textposition = "inside",  # 文本內(nèi)容的位置 
  13.     textinfo = "value+percent previous"  # 顯示文本內(nèi)容 
  14. )) 
  15.  
  16.  
  17. fig.add_trace(go.Funnel( 
  18.     name = "2021年2月",   # 名稱和數(shù)據(jù)需要改變 
  19.     x = [1200,900,500,240],   
  20.     y = stage,  
  21.     orientation = "h",   
  22.     textposition = "inside",   
  23.     textinfo = "value+percent total"   
  24. )) 
  25.  
  26. fig.add_trace(go.Funnel( 
  27.     name = "2021年3月",  # 名稱和數(shù)據(jù)需要改變 
  28.     x = [1500,1000,450,300],   
  29.     y = stage,  
  30.     orientation = "h",   
  31.     textposition = "inside",   
  32.     textinfo = "label+percent initial"   
  33. )) 
  34.  
  35.  
  36. fig.show() 

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

面積漏斗

  1. from plotly import graph_objects as go 
  2.  
  3. fig = go.Figure(go.Funnelarea( 
  4.     text = ["瀏覽網(wǎng)站","加購","點擊支付","支付成功"], 
  5.     values = [5000, 2000, 800, 500], 
  6.  
  7. )) 
  8.  
  9. fig.show() 

設置面積漏斗的顏色和邊框

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

多組面積漏斗

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

  1. from plotly import graph_objects as go 
  2.  
  3. fig = go.Figure() 
  4.  
  5. # 添加四組數(shù)據(jù):第一季度到第四季度 
  6. fig.add_trace(go.Funnelarea( 
  7.     scalegroup = "first",   # 組別名稱 
  8.     text = ["瀏覽網(wǎng)站","搜索","加購","提交訂單","點擊支付","支付成功"], 
  9.     values = [500, 450, 340, 230, 220, 110],  # 數(shù)據(jù) 
  10.     textinfo = "value",  # 顯示文本信息 
  11.     title = {"position""top center",  # 標題頂部居中 
  12.              "text""第一季度"  # 標題內(nèi)容 
  13.             }, 
  14.     domain = {"x": [0, 0.5], # 圖形位置 
  15.               "y": [0, 0.5] 
  16.              })) 
  17.  
  18. fig.add_trace(go.Funnelarea( 
  19.     scalegroup = "first",  
  20.     text = ["瀏覽網(wǎng)站","搜索","加購","提交訂單","點擊支付","支付成功"], 
  21.     values = [600, 500, 400, 300, 200, 100],  
  22.     textinfo = "value"
  23.     title = {"position""top center",  
  24.              "text""第二季度"}, 
  25.     domain = {"x": [0, 0.5],  
  26.               "y": [0.55, 1]})) 
  27.  
  28. fig.add_trace(go.Funnelarea( 
  29.     scalegroup = "second",  
  30.     text = ["瀏覽網(wǎng)站","搜索","加購","提交訂單","點擊支付","支付成功"], 
  31.     values = [510, 480, 440, 330, 220, 100],  
  32.     textinfo = "value"
  33.     title = {"position""top left",  
  34.              "text""第三季度"}, 
  35.     domain = {"x": [0.55, 1],  
  36.               "y": [0, 0.5]})) 
  37.  
  38. fig.add_trace(go.Funnelarea( 
  39.     scalegroup = "second",  
  40.     text = ["瀏覽網(wǎng)站","搜索","加購","提交訂單","點擊支付","支付成功"], 
  41.     values = [360, 250, 240, 130, 120, 60], 
  42.     textinfo = "value",  
  43.     title = {"position""top right",  
  44.              "text""第四季度"}, 
  45.     domain = {"x": [0.55, 1],  
  46.               "y": [0.55, 1]})) 
  47.  
  48. fig.update_layout( 
  49.     margin = {"l": 100, "r": 100},   # 整個圖形到左右邊框的距離 
  50.     shapes = [ 
  51.             {"x0": 0, "x1": 0.5, "y0": 0, "y1": 0.5},   # 添加4組位置 
  52.             {"x0": 0, "x1": 0.5, "y0": 0.55, "y1": 1}, 
  53.             {"x0": 0.55, "x1": 1, "y0": 0, "y1": 0.5}, 
  54.             {"x0": 0.55, "x1": 1, "y0": 0.55, "y1": 1} 
  55.     ]) 
  56.  
  57. fig.show() 

漏斗圖真的很好用,能夠觀察到數(shù)據(jù)在不同階段的轉(zhuǎn)化情況。

 

責任編輯:武曉燕 來源: 尤而小屋
相關推薦

2022-08-26 09:15:58

Python可視化plotly

2024-04-01 11:53:42

PlotlyPython數(shù)據(jù)可視化

2020-03-23 14:55:52

Python可視化Plotly

2025-04-01 08:30:00

Plotly數(shù)據(jù)可視化數(shù)據(jù)分析

2022-02-23 09:50:52

PythonEchartspyecharts

2020-06-29 15:40:53

PlotlyPython數(shù)據(jù)可視化

2024-07-11 16:16:27

ChatGPTLLM

2022-06-28 09:34:24

可視化Python代碼

2021-10-08 09:27:11

Python圖形化工具

2020-03-11 14:39:26

數(shù)據(jù)可視化地圖可視化地理信息

2015-09-14 13:48:35

數(shù)據(jù)挖掘數(shù)據(jù)可視化

2018-02-04 22:22:46

大數(shù)據(jù)開發(fā)工具

2019-03-20 13:44:30

Web 開發(fā)代碼

2023-03-27 23:42:29

樹狀圖開發(fā)可視化

2020-05-22 13:32:24

可視化詞云圖數(shù)據(jù)

2023-04-14 08:21:55

2021-06-24 13:00:35

微軟開源可視化

2017-10-14 13:54:26

數(shù)據(jù)可視化數(shù)據(jù)信息可視化

2009-04-21 14:26:41

可視化監(jiān)控IT管理摩卡

2023-08-14 23:45:55

點贊
收藏

51CTO技術棧公眾號