自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

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

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

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

[[425296]]

關(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ù), 

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

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

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

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

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

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

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

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

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

 

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

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

 

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

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

 

 

責(zé)任編輯:未麗燕 來源: 關(guān)于數(shù)據(jù)分析與可視化
相關(guān)推薦

2020-11-12 09:00:00

微服務(wù)架構(gòu)工具

2021-06-11 17:45:57

大數(shù)據(jù)可視化工具

2014-06-30 09:24:48

數(shù)據(jù)可視化

2021-10-11 08:04:22

Python數(shù)據(jù)行程

2013-01-29 09:57:23

數(shù)據(jù)分析

2020-03-11 14:39:26

數(shù)據(jù)可視化地圖可視化地理信息

2014-01-22 15:34:00

數(shù)據(jù)分析

2018-11-21 14:38:09

分析在數(shù)據(jù)電影

2017-06-19 08:30:35

大數(shù)據(jù)數(shù)據(jù)可視化報(bào)表

2018-12-03 16:50:23

數(shù)據(jù)可視化數(shù)據(jù)分析薪水

2020-06-29 15:40:53

PlotlyPython數(shù)據(jù)可視化

2020-05-14 10:19:23

Python可視化分析

2017-10-14 13:54:26

數(shù)據(jù)可視化數(shù)據(jù)信息可視化

2016-12-29 20:05:56

數(shù)據(jù)可視化大數(shù)據(jù)產(chǎn)品分析

2017-01-12 17:28:59

數(shù)據(jù)分析數(shù)據(jù)可視化可視化

2022-08-26 09:15:58

Python可視化plotly

2017-02-07 15:54:14

數(shù)據(jù)可視化數(shù)據(jù)分析

2022-02-23 09:50:52

PythonEchartspyecharts

2020-05-26 11:34:46

可視化WordCloud

2017-03-09 09:54:13

分析數(shù)據(jù)可視化
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)