1. 程式人生 > 實用技巧 >視覺化:pandas與pyecharts、堆疊圖、Stack_bar_percent

視覺化:pandas與pyecharts、堆疊圖、Stack_bar_percent

官網地址:https://gallery.pyecharts.org/#/Bar/stack_bar_percent

bar=Bar(init_opts = opts.InitOpts(height='350px'))

for label in pm_a["分段"].unique():
    p = pm_a[pm_a["分段"]==label]
    bar.add_xaxis(list(p["City1"]))
    bar.add_yaxis(label,list((p["比率"]*100).round(2)),stack="stack1") 

bar.set_series_opts(label_opts
=opts.LabelOpts(is_show=False)) bar.set_global_opts( title_opts=opts.TitleOpts(title="各地市分佈圖",pos_left="center"), legend_opts=opts.LegendOpts(pos_top="8%"), yaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(formatter="{value} %"),max_=100), tooltip_opts
=opts.TooltipOpts(formatter="{a}:{c} %") ) bar.render_notebook()

說明:

1.for label in pm_a["分段"].unique():

針對各地市的四種負荷情況,label的值:低負荷、中等負荷、高負荷、超高負荷。

2.bar.add_xaxis(list(p["City1"]))

x軸方向針對地市。

3.bar.add_yaxis(label,list((p["比率"]*100).round(2)),stack="stack1")

各種負荷中每個地市的比率,stack

4.

tooltip_opts=opts.TooltipOpts(formatter="{a}:{c} %")

# 標籤內容格式器,支援字串模板和回撥函式兩種形式,字串模板與回撥函式返回的字串均支援用 \n 換行。
# 字串模板 模板變數有:
# {a}:系列名。
# {b}:資料名。
# {c}:資料值。
# {@xxx}:資料中名為 'xxx' 的維度的值,如 {@product} 表示名為 'product'` 的維度的值。
# {@[n]}:資料中維度 n 的值,如{@[3]}` 表示維度 3 的值,從 0 開始計數。
# 示例:formatter: '{b}: {@score}'