您好,登錄后才能下訂單哦!
本篇內容主要講解“怎么用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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。