如何用Python數(shù)據(jù)可視化來分析用戶留存率
關(guān)于“漏斗圖”
漏斗圖常用于用戶行為的轉(zhuǎn)化率分析,例如通過漏斗圖來分析用戶購買流程中各個(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ù)如下圖所示:

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

而要繪制出我們想要的想要的漏斗圖的模樣,代碼示例如下
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()
繪制出來的漏斗圖如下圖所示

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

最后我們用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()

我們將數(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()

到此這篇關(guān)于如何用Python數(shù)據(jù)可視化來分析用戶留存率的文章就介紹到這了,更多相關(guān)用Python數(shù)據(jù)可視化來分析用戶留存率內(nèi)容請(qǐng)搜索本站以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持本站!
版權(quán)聲明:本站文章來源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請(qǐng)保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非maisonbaluchon.cn所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場(chǎng),如有內(nèi)容涉嫌侵權(quán),請(qǐng)聯(lián)系alex-e#qq.com處理。
關(guān)注官方微信