本篇内容主要讲解“怎么用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绘制帕累托图”有了更深的了解,不妨来实际操作一番吧!这里是创新互联网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!