99999久久久久久亚洲,欧美人与禽猛交狂配,高清日韩av在线影院,一个人在线高清免费观看,啦啦啦在线视频免费观看www

熱線電話:13121318867

登錄
首頁(yè)精彩閱讀如何用Python數(shù)據(jù)可視化來(lái)分析用戶留存率,建議收藏
如何用Python數(shù)據(jù)可視化來(lái)分析用戶留存率,建議收藏
2021-12-20
收藏

作者:俊欣

來(lái)源:關(guān)于數(shù)據(jù)分析與可視化

今天和大家來(lái)分享一些數(shù)據(jù)可視化方向的干貨,我們來(lái)嘗試用Python來(lái)繪制一下“漏斗圖”,但愿大家在看完本篇文章之后會(huì)有所收獲。

關(guān)于“漏斗圖

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

首先我們先要導(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<a href='/map/shujukeshihua/' style='color:#000;font-size:inherit;'>數(shù)據(jù)可視化</a>來(lái)分析用戶留存率,建議收藏

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

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<a href='/map/shujukeshihua/' style='color:#000;font-size:inherit;'>數(shù)據(jù)可視化</a>來(lái)分析用戶留存率,建議收藏
如何用Python<a href='/map/shujukeshihua/' style='color:#000;font-size:inherit;'>數(shù)據(jù)可視化</a>來(lái)分析用戶留存率,建議收藏

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

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()

繪制出來(lái)的漏斗圖如下圖所示

如何用Python<a href='/map/shujukeshihua/' style='color:#000;font-size:inherit;'>數(shù)據(jù)可視化</a>來(lái)分析用戶留存率,建議收藏

當(dāng)然我們用plotly來(lái)繪制的話則會(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<a href='/map/shujukeshihua/' style='color:#000;font-size:inherit;'>數(shù)據(jù)可視化</a>來(lái)分析用戶留存率,建議收藏

最后我們用pyecharts模塊來(lái)繪制一下,當(dāng)中有專門用來(lái)繪制“漏斗圖”的方法,我們只需要調(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<a href='/map/shujukeshihua/' style='color:#000;font-size:inherit;'>數(shù)據(jù)可視化</a>來(lái)分析用戶留存率,建議收藏

我們將數(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<a href='/map/shujukeshihua/' style='color:#000;font-size:inherit;'>數(shù)據(jù)可視化</a>來(lái)分析用戶留存率,建議收藏

數(shù)據(jù)分析咨詢請(qǐng)掃描二維碼

若不方便掃碼,搜微信號(hào):CDAshujufenxi

數(shù)據(jù)分析師資訊
更多

OK
客服在線
立即咨詢
客服在線
立即咨詢
') } function initGt() { var handler = function (captchaObj) { captchaObj.appendTo('#captcha'); captchaObj.onReady(function () { $("#wait").hide(); }).onSuccess(function(){ $('.getcheckcode').removeClass('dis'); $('.getcheckcode').trigger('click'); }); window.captchaObj = captchaObj; }; $('#captcha').show(); $.ajax({ url: "/login/gtstart?t=" + (new Date()).getTime(), // 加隨機(jī)數(shù)防止緩存 type: "get", dataType: "json", success: function (data) { $('#text').hide(); $('#wait').show(); // 調(diào)用 initGeetest 進(jìn)行初始化 // 參數(shù)1:配置參數(shù) // 參數(shù)2:回調(diào),回調(diào)的第一個(gè)參數(shù)驗(yàn)證碼對(duì)象,之后可以使用它調(diào)用相應(yīng)的接口 initGeetest({ // 以下 4 個(gè)配置參數(shù)為必須,不能缺少 gt: data.gt, challenge: data.challenge, offline: !data.success, // 表示用戶后臺(tái)檢測(cè)極驗(yàn)服務(wù)器是否宕機(jī) new_captcha: data.new_captcha, // 用于宕機(jī)時(shí)表示是新驗(yàn)證碼的宕機(jī) product: "float", // 產(chǎn)品形式,包括:float,popup width: "280px", https: true // 更多配置參數(shù)說(shuō)明請(qǐng)參見(jiàn):http://docs.geetest.com/install/client/web-front/ }, handler); } }); } function codeCutdown() { if(_wait == 0){ //倒計(jì)時(shí)完成 $(".getcheckcode").removeClass('dis').html("重新獲取"); }else{ $(".getcheckcode").addClass('dis').html("重新獲取("+_wait+"s)"); _wait--; setTimeout(function () { codeCutdown(); },1000); } } function inputValidate(ele,telInput) { var oInput = ele; var inputVal = oInput.val(); var oType = ele.attr('data-type'); var oEtag = $('#etag').val(); var oErr = oInput.closest('.form_box').next('.err_txt'); var empTxt = '請(qǐng)輸入'+oInput.attr('placeholder')+'!'; var errTxt = '請(qǐng)輸入正確的'+oInput.attr('placeholder')+'!'; var pattern; if(inputVal==""){ if(!telInput){ errFun(oErr,empTxt); } return false; }else { switch (oType){ case 'login_mobile': pattern = /^1[3456789]\d{9}$/; if(inputVal.length==11) { $.ajax({ url: '/login/checkmobile', type: "post", dataType: "json", data: { mobile: inputVal, etag: oEtag, page_ur: window.location.href, page_referer: document.referrer }, success: function (data) { } }); } break; case 'login_yzm': pattern = /^\d{6}$/; break; } if(oType=='login_mobile'){ } if(!!validateFun(pattern,inputVal)){ errFun(oErr,'') if(telInput){ $('.getcheckcode').removeClass('dis'); } }else { if(!telInput) { errFun(oErr, errTxt); }else { $('.getcheckcode').addClass('dis'); } return false; } } return true; } function errFun(obj,msg) { obj.html(msg); if(msg==''){ $('.login_submit').removeClass('dis'); }else { $('.login_submit').addClass('dis'); } } function validateFun(pat,val) { return pat.test(val); }