您好,登錄后才能下訂單哦!
本篇內(nèi)容主要講解“怎么用Python繪制帕累托圖”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實(shí)用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“怎么用Python繪制帕累托圖”吧!
# 隨機(jī)顏色, 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':["蔬菜","水果","豬肉","電商","綜合","水產(chǎn)"],'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坐標(biāo)軸,即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), # 刻度尺朝內(nèi) 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繪制帕累托圖”有了更深的了解,不妨來實(shí)際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。