您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
本篇内容主要讲解“怎么用Python绘制帕累托图”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“怎么用Python绘制帕累托图”吧!
# 随机颜色, from faker def rand_color() -> str: return random.choice( [ "#c23531", "#2f4554", "#61a0a8", "#d48265", "#749f83", "#ca8622", "#bda29a", "#6e7074", "#546570", "#c4ccd3", "#f05b72", "#444693", "#726930", "#b2d235", "#6d8346", "#ac6767", "#1d953f", "#6950a1", ] ) df_origin = pd.DataFrame({'categories':["蔬菜","水果","猪肉","电商","综合","水产"],'sales': [random.randint(10, 100) for _ in range(6)]}) print(df_origin) # 按销量降序排列 df_sorted = df_origin.sort_values(by='sales' , ascending=False) print(df_sorted) # 折线图x轴 x_line_categories = [*range(7)] # 折线图y轴--向下累积频率 cum_percent = df_sorted['sales'].cumsum() / df_sorted['sales'].sum() * 100 cum_percent = cum_percent.append(pd.Series([0])) # 添加起始频率0 cum_percent = cum_percent.sort_values(ascending=True) print(df_sorted.categories.values.tolist()) print(cum_percent.values.tolist()) def pareto_bar() -> Bar: line = ( Line() .add_xaxis(x_line_categories) .add_yaxis("累计百分比", cum_percent.values.tolist(), xaxis_index=1, yaxis_index=1, # 使用次y坐标轴,即bar中的extend_axis label_opts=opts.LabelOpts(is_show=False), is_smooth=True, ) ) bar = ( Bar() .add_xaxis(df_sorted.categories.values.tolist()) .add_yaxis('销售额', df_sorted.sales.values.tolist(), category_gap=0) # .add_yaxis('总额百分比', cum_percent.values.tolist()) .extend_axis(xaxis=opts.AxisOpts(is_show=False, position='top')) .extend_axis(yaxis=opts.AxisOpts(axistick_opts=opts.AxisTickOpts(is_inside=True), # 刻度尺朝内 axislabel_opts=opts.LabelOpts(formatter='{value}%'), position='right') ) .set_series_opts(label_opts=opts.LabelOpts(is_show=True, font_size=14)) .set_global_opts(title_opts=opts.TitleOpts(title='帕累托图示例-销售额\n Make By tengyulong', subtitle=''), xaxis_opts=opts.AxisOpts(name='商品类型', type_='category'), yaxis_opts=opts.AxisOpts( axislabel_opts=opts.LabelOpts(formatter="{value} 件") ) ) ) bar.overlap(line) return bar pareto_bar().render('帕累托图.html') # 或者 pareto_bar().render_notebook()
渲染效果:
到此,相信大家对“怎么用Python绘制帕累托图”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。