干貨分享 | 看如何用Python數(shù)據(jù)可視化來(lái)分析用戶留存率,建議收藏


import matplotlib.pyplot as pltimport?pandas?as?pddf = 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]})

y = [5,4,3,2,1]x?=?[85,75,58,43,23]x_max = 100x_min = 0for 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 charty?=?[5,4,3,2,1]labels = df["環(huán)節(jié)"].tolist()x?=?df["人數(shù)"].tolist()x_range?=?100font?=?fm.FontProperties(fname="KAITI.ttf")
fig, ax = plt.subplots(1, figsize=(12,6))for idx, val in enumerate(x):left = (x_range - val)/2plt.barh(y[idx], x[idx], left = left, color='#808B96', height=.8, edgecolor='black')plt.text(50, y[idx]+0.1, labels[idx], ha='center',fontproperties=font, fontsize=16, color='#2A2A2A')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])/2shadow_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()

import plotly.express as pxdata = 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模塊來(lái)繪制一下,當(dāng)中有專門用來(lái)繪制“漏斗圖”的方法,我們只需要調(diào)用即可
from pyecharts.charts import Funnelfrom pyecharts import options as optsfrom pyecharts.globals import ThemeTypec = (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()

評(píng)論
圖片
表情
