如何用Python數(shù)據(jù)可視化來分析用戶留存率,建議收藏
今天和大家來分享一些數(shù)據(jù)可視化方向的干貨,我們來嘗試用Python來繪制一下“漏斗圖”,但愿大家在看完本篇文章之后會(huì)有所收獲。
關(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ì)稍顯簡單與粗糙,制作的原理也比較簡單,先繪制出水平方向的直方圖,然后利用plot.barh()當(dāng)中的“left”參數(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ì)更加的簡單一些,代碼示例如下:
- 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()