一、適用條件
1、柱狀圖:數(shù)據(jù)集不宜過大
2、堆積柱狀圖:兩者可以合起來看總量、各部分對比
3、組合柱狀圖:可以將比例放在副坐標(biāo)軸、看趨勢
二、代碼實現(xiàn)
1.導(dǎo)入所需包
from pyecharts import options as opts
from pyecharts.charts import Bar, Line
from pyecharts.commons.utils import JsCode
from pyecharts.globals import ThemeType
from pyecharts.render import make_snapshot
#from snapshot_phantomjs import snapshot
from snapshot_pyppeteer import snapshot
import pandas as pd
import numpy as np
2.數(shù)據(jù)整理
###導(dǎo)入數(shù)據(jù)
df = pd.read_excel('picture.xlsx',sheet_name='bar')
###觀察數(shù)據(jù)
print(df.head())
###選擇所需數(shù)據(jù)
df1 = df[['國家','成單量','退單']]
df_country = df1.groupby(by = ["國家"]).sum().reset_index().sort_values(ascending=False,by='成單量')###sort_value 可以按照大小進(jìn)行排序
# print(df_country["國家"])
#print(list(df_country["成單id"]))
x_list = list(df_country["國家"])
y_list = list(df_country["成單量"])
y1_list = list(df_country["退單"])
ratio_list = list(round((df_country["退單"]/df_country["成單量"])*100,2))
title1 = "國家成單退單統(tǒng)計"
subtitle1 = "子標(biāo)題"
3.1雙柱狀圖
###畫圖
def bar_chart() -> Bar:
################### 這部分可以直接用,保存成網(wǎng)頁
c = (
Bar(init_opts=opts.InitOpts(theme=ThemeType.VINTAGE))###括號內(nèi)設(shè)置主題風(fēng)格
.add_xaxis(x_list)
.add_yaxis("成單量",y_list)
.add_yaxis("退單", y1_list)
.reversal_axis() ####翻轉(zhuǎn)x,y軸
.set_global_opts(
xaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(rotate=-15)), ###設(shè)置旋轉(zhuǎn)角度問題
title_opts=opts.TitleOpts(title= title1, subtitle= subtitle1), ###設(shè)置標(biāo)題
brush_opts=opts.BrushOpts(),
)
.set_series_opts(label_opts=opts.LabelOpts(is_show=True)) ###設(shè)置標(biāo)簽展示
# .render("1.html") ###保存成網(wǎng)頁
)
###################
return c
make_snapshot(snapshot, bar_chart().render(), "1.gif")
if __name__ == '__main__':
bar_chart()

1.gif
3.2堆積柱狀圖
c = (
Bar(init_opts=opts.InitOpts(theme=ThemeType.VINTAGE))###括號內(nèi)設(shè)置主題風(fēng)格
.add_xaxis(x_list)
.add_yaxis("成單量",y_list,stack="stack1")
.add_yaxis("退單", y1_list,stack="stack1")
.set_global_opts(
xaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(rotate=-15)), ###設(shè)置旋轉(zhuǎn)角度問題
title_opts=opts.TitleOpts(title= title1, subtitle= subtitle1), ###設(shè)置標(biāo)題
brush_opts=opts.BrushOpts(),
toolbox_opts=opts.ToolboxOpts(), ####渲染成網(wǎng)頁時,直接下載
datazoom_opts=opts.DataZoomOpts(), ####可拉動x軸
)
.set_series_opts(
label_opts=opts.LabelOpts(is_show=True, ###設(shè)置標(biāo)簽展示
position="right",
formatter=JsCode(
"function(x){return Number(x.data).toFixed();}"
),
)
)
.render("1.html") ###保存成網(wǎng)頁
)

2.gif
3.3組合柱狀圖
c = (
Bar(init_opts=opts.InitOpts(theme=ThemeType.VINTAGE))###括號內(nèi)設(shè)置主題風(fēng)格
.add_xaxis(x_list)
.add_yaxis(
"成單量",
y_list,
label_opts=opts.LabelOpts(is_show=False),
)
.add_yaxis(
"退單",
y1_list,
label_opts=opts.LabelOpts(is_show=False),
)
.extend_axis(
yaxis=opts.AxisOpts(
name="比率",
type_="value",
min_=0,
max_=10,
interval=2.5,
axislabel_opts=opts.LabelOpts(formatter="{value}%"),
)
)
.set_global_opts(
tooltip_opts=opts.TooltipOpts(
is_show=True, trigger="axis", axis_pointer_type="cross"
),
xaxis_opts=opts.AxisOpts(
type_="category",
axispointer_opts=opts.AxisPointerOpts(is_show=True, type_="shadow"),
),
yaxis_opts=opts.AxisOpts(
name="單量",
type_="value",
min_=0,
max_=1500,
interval=300,
axislabel_opts=opts.LabelOpts(formatter="{value} rmb"),
axistick_opts=opts.AxisTickOpts(is_show=True),
splitline_opts=opts.SplitLineOpts(is_show=True),
),
title_opts=opts.TitleOpts(title= title1, subtitle= subtitle1), ###設(shè)置標(biāo)題
brush_opts=opts.BrushOpts(),
toolbox_opts=opts.ToolboxOpts(), ####渲染成網(wǎng)頁時,直接下載
datazoom_opts=opts.DataZoomOpts(), ####可拉動x軸
)
)
line = (
Line(init_opts=opts.InitOpts(theme=ThemeType.VINTAGE))
.add_xaxis(xaxis_data=x_list)
.add_yaxis(
series_name="比率",
yaxis_index=1,
y_axis=ratio_list,
label_opts=opts.LabelOpts(is_show=True),
)
)
c.overlap(line).render("1.html")

國家成單退單統(tǒng)計 (1).png