使用Python matplotlib库绘制漏斗图

您所在的位置:网站首页 漏斗图制作工具 使用Python matplotlib库绘制漏斗图

使用Python matplotlib库绘制漏斗图

2023-12-27 10:38| 来源: 网络整理| 查看: 265

pyecharts

pyecharts有漏斗图的模板(点击链接),套用即可。

代码 from pyecharts import options as opts from pyecharts.charts import Funnel data = [135043, 113413, 74909, 10366, 9018, 4151] phase = ['总访客数量 ', '活跃访客数量', '注册用户数量', '预定用户数量', '支付用户数量', '复购用户数量'] c = ( Funnel() .add("阶段", [list(z) for z in zip(phase, data)]) .set_global_opts(title_opts=opts.TitleOpts(title="漏斗图")) .render("funnel.html") ) 图片

在这里插入图片描述 虽然绘制方便,但是美观程度一般。

matplotlib

使用matplotlib绘制的思路是,绘制两个横向柱状图,其中一个设置成背景色遮住另一个的部分柱长,再绘制多边形填充两个柱子之间的间隔。

代码 import pandas as pd import numpy as np import matplotlib.pyplot as plt import seaborn as sns from matplotlib.patches import Polygon # Polygon()可以用来传入按顺序组织的多边形顶点,从而生成出多边形 from matplotlib.collections import PatchCollection plt.style.use('seaborn-dark') # 设置主题 plt.rcParams['font.sans-serif'] = ['SimHei'] # 用来正常显示中文标签 plt.rcParams['axes.unicode_minus'] = False # 用来正常显示负号 visitor_num = 135043 data = [135043, 113413, 74909, 10366, 9018, 4151] phase = ['总访客数量 ', '活跃访客数量', '注册用户数量', '预定用户数量', '支付用户数量', '复购用户数量'] data1 = [visitor_num/2 - i/2 for i in data] data2 = [i+j for i,j in zip(data, data1)] color_list = [ '#5c1d1d', '#892c2c','#994a4a', '#c56161', '#d48989', '#e2b0b0'] # 柱子颜色 fig,ax = plt.subplots(figsize=(16, 9),facecolor='#f4f4f4') ax.barh(phase[::-1], data2[::-1], color = color_list, height=0.7) # 柱宽设置为0.7 ax.barh(phase[::-1], data1[::-1], color = '#f4f4f4', height=0.7) # 设置成背景同色 ax.axis('off') polygons = [] for i in range(len(data)): # 阶段 ax.text( 0, # 坐标 i, # 高度 phase[::-1][i], # 文本 color='black', alpha=0.8, size=16, ha="right") # 数量 ax.text( data2[0] / 2 , i, str(data[::-1][i]) +'(' +str(round(data[::-1][i] / data[0] * 100, 1)) + '%)', color='black', alpha=0.8, size=18, ha="center") if i "width": [4, 2, 2, 3, 1, 1], "color": ["wheat", "wheat", "blue", "wheat", "wheat"]}}, connector = {"line": {"color": "royalblue", "dash": "dot", "width": 3}}) ) fig.show()

在这里插入图片描述

带有各项指标的堆积漏斗图 from plotly import graph_objects as go fig = go.Figure() fig.add_trace(go.Funnel( name = 'Montreal', y = ["Website visit", "Downloads", "Potential customers", "Requested price"], x = [120, 60, 30, 20], textinfo = "value+percent initial")) fig.add_trace(go.Funnel( name = 'Toronto', orientation = "h", y = ["Website visit", "Downloads", "Potential customers", "Requested price", "invoice sent"], x = [100, 60, 40, 30, 20], textposition = "inside", textinfo = "value+percent previous")) fig.add_trace(go.Funnel( name = 'Vancouver', orientation = "h", y = ["Website visit", "Downloads", "Potential customers", "Requested price", "invoice sent", "Finalized"], x = [90, 70, 50, 30, 10, 5], textposition = "outside", textinfo = "value+percent total")) fig.show()

在这里插入图片描述



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3