溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

如何用Python數(shù)據(jù)可視化來分析用戶留存率

發(fā)布時(shí)間:2021-09-26 09:55:07 來源:億速云 閱讀:168 作者:柒染 欄目:開發(fā)技術(shù)

如何用Python數(shù)據(jù)可視化來分析用戶留存率,很多新手對(duì)此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。

關(guān)于“漏斗圖”

漏斗圖常用于用戶行為的轉(zhuǎn)化率分析,例如通過漏斗圖來分析用戶購(gòu)買流程中各個(gè)環(huán)節(jié)的轉(zhuǎn)化率。當(dāng)然在整個(gè)分析過程當(dāng)中,我們會(huì)把流程優(yōu)化前后的漏斗圖放在一起,進(jìn)行比較分析,得出相關(guān)的結(jié)論,今天小編就用“matplotlib”、“plotly”以及“pyecharts”這幾個(gè)模塊來為大家演示一下怎么畫出好看的漏斗圖首先我們先要導(dǎo)入需要用到的模塊以及數(shù)據(jù),

import matplotlib.pyplot as plt 
import pandas as pd 
df = pd.DataFrame({"環(huán)節(jié)": ["環(huán)節(jié)一", "環(huán)節(jié)二", "環(huán)節(jié)三", "環(huán)節(jié)四", "環(huán)節(jié)五"],  
                   "人數(shù)": [1000, 600, 400, 250, 100],  
                   "總體轉(zhuǎn)化率": [1.00, 0.60, 0.40, 0.25, 0.1]})

需要用到的數(shù)據(jù)如下圖所示:

如何用Python數(shù)據(jù)可視化來分析用戶留存率

matplotlib來制作漏斗圖,制作出來的效果可能會(huì)稍顯簡(jiǎn)單與粗糙,制作的原理也比較簡(jiǎn)單,先繪制出水平方向的直方圖,然后利用plot.barh()當(dāng)中的“l(fā)eft”參數(shù)將直方圖向左移,便能出來類似于漏斗圖的模樣

y = [5,4,3,2,1] 
x = [85,75,58,43,23] 
x_max = 100 
x_min = 0 
for idx, val in enumerate(x): 
    plt.barh(y[idx], x[idx], left = idx+5) 
plt.xlim(x_min, x_max)

如何用Python數(shù)據(jù)可視化來分析用戶留存率

要繪制出我們想要的想要的漏斗圖的模樣,代碼示例如下

from matplotlib import font_manager as fm 
# funnel chart 
y = [5,4,3,2,1] 
labels = df["環(huán)節(jié)"].tolist() 
x = df["人數(shù)"].tolist() 
x_range = 100 
font = fm.FontProperties(fname="KAITI.ttf") 
fig, ax = plt.subplots(1, figsize=(12,6)) 
for idx, val in enumerate(x): 
    left = (x_range - val)/2 
    plt.barh(y[idx], x[idx], left = left, color='#808B96', height=.8, edgecolor='black') 
    # label 
    plt.text(50, y[idx]+0.1, labels[idx], ha='center', 
             fontproperties=font, fontsize=16, color='#2A2A2A') 
    # value 
    plt.text(50, y[idx]-0.3, x[idx], ha='center', 
             fontproperties=font, fontsize=16, color='#2A2A2A') 
     
    if idx != len(x)-1: 
        next_left = (x_range - x[idx+1])/2 
        shadow_x = [left, next_left,  
                    100-next_left, 100-left, left] 
        shadow_y = [y[idx]-0.4, y[idx+1]+0.4,  
                    y[idx+1]+0.4, y[idx]-0.4, y[idx]-0.4] 
        plt.plot(shadow_x, shadow_y) 
plt.xlim(x_min, x_max) 
plt.axis('off') 
plt.title('每個(gè)環(huán)節(jié)的流失率', fontproperties=font, loc='center', fontsize=24, color='#2A2A2A') 
plt.show()

繪制出來的漏斗圖如下圖所示

如何用Python數(shù)據(jù)可視化來分析用戶留存率

當(dāng)然我們用plotly來繪制的話則會(huì)更加的簡(jiǎn)單一些,代碼示例如下

import plotly.express as px 
data = dict(values=[80,73,58,42,23], 
            labels=['環(huán)節(jié)一', '環(huán)節(jié)二', '環(huán)節(jié)三', '環(huán)節(jié)四', '環(huán)節(jié)五']) 
fig = px.funnel(data, y='labels', x='values') 
fig.show()

如何用Python數(shù)據(jù)可視化來分析用戶留存率

最后我們用pyecharts模塊來繪制一下,當(dāng)中有專門用來繪制“漏斗圖”的方法,我們只需要調(diào)用即可

from pyecharts.charts import Funnel 
from pyecharts import options as opts 
from pyecharts.globals import ThemeType 
 
c = ( 
    Funnel(init_opts=opts.InitOpts(width="900px", height="600px",theme = ThemeType.INFOGRAPHIC )) 
    .add( 
        "環(huán)節(jié)", 
        df[["環(huán)節(jié)","總體轉(zhuǎn)化率"]].values, 
        sort_="descending", 
        label_opts=opts.LabelOpts(position="inside"), 
    ) 
    .set_global_opts(title_opts=opts.TitleOpts(title="Pyecharts漏斗圖", pos_bottom = "90%", pos_left = "center")) 
) 
c.render_notebook()

如何用Python數(shù)據(jù)可視化來分析用戶留存率

我們將數(shù)據(jù)標(biāo)注上去之后

c = ( 
    Funnel(init_opts=opts.InitOpts(width="900px", height="600px",theme = ThemeType.INFOGRAPHIC )) 
    .add( 
        "商品", 
        df[["環(huán)節(jié)","總體轉(zhuǎn)化率"]].values, 
        sort_="descending", 
        label_opts=opts.LabelOpts(position="inside"), 
    ) 
    .set_global_opts(title_opts=opts.TitleOpts(title="Pyecharts漏斗圖", pos_bottom = "90%", pos_left = "center")) 
    .set_series_opts(label_opts=opts.LabelOpts(formatter=":{c}")) 
) 
c.render_notebook()

如何用Python數(shù)據(jù)可視化來分析用戶留存率

看完上述內(nèi)容是否對(duì)您有幫助呢?如果還想對(duì)相關(guān)知識(shí)有進(jìn)一步的了解或閱讀更多相關(guān)文章,請(qǐng)關(guān)注億速云行業(yè)資訊頻道,感謝您對(duì)億速云的支持。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI