實現功能:
python繪制金字塔圖,一種排過序的分組水平柱狀圖barplot,可很好展示不同分組之間的差異,可可視化逐級過濾或者漏鬥的每個階段。
實現代碼:
|
1 |
import pandas as pd |
|
2 |
import matplotlib.pyplot as plt |
|
3 |
import seaborn as sns |
|
4 | |
|
5 |
# Read data |
|
6 |
df = pd.read_csv("D:\數據雜壇\datasets\email_campaign_funnel.csv") |
|
7 | |
|
8 |
# Draw Plot |
|
9 |
plt.figure(figsize=(12, 8), dpi=80) |
|
10 |
group_col = 'Gender' |
|
11 |
order_of_bars = df.Stage.unique()[::-1] |
|
12 |
colors = [ |
|
13 |
plt.cm.Set1(i / float(len(df[group_col].unique()) - 1)) |
|
14 |
for i in range(len(df[group_col].unique())) |
|
15 |
] |
|
16 | |
|
17 |
for c, group in zip(colors, df[group_col].unique()): |
|
18 |
sns.barplot(x='Users', |
|
19 |
y='Stage', |
|
20 |
data=df.loc[df[group_col] == group, :], |
|
21 |
order=order_of_bars, |
|
22 |
color=c, |
|
23 |
label=group) |
|
24 | |
|
25 |
# Decorations |
|
26 |
plt.xlabel("$Users$") |
|
27 |
plt.ylabel("Stage of Purchase") |
|
28 |
plt.yticks(fontsize=12) |
|
29 |
plt.title("Population Pyramid of the Marketing Funnel", fontsize=18) |
|
30 |
plt.legend() |
|
31 |
plt.show() |
實現效果:

喜歡記得點贊,在看,收藏,
關注V訂閱号:數據雜壇,獲取數據集,完整代碼和效果,将持續更新!
,更多精彩资讯请关注tft每日頭條,我们将持续为您更新最新资讯!