大家好!
今天来分享一下如何用python给表格加上双层饼图,让同事的图表黯然失色,如下图所示。
案例中数据表格结构如下:
代码及演示:
import pyecharts.options as opts
from pyecharts.charts import Pie
import numpy as np
import pandas as pd
from pyecharts.globals import ThemeTypedf_pie4 = pd.read_excel("D:01会Excel的隔壁老王Excel报表20200529-Python个人中心可视化报表20200529-Python个人中心可视化报表.xlsx",sheet_name = "分渠道企划任务达成-1")inner_x_data = df_pie4[["渠道"]][ (df_pie4[["渠道"]].渠道 == "SX") | (df_pie4[["渠道"]].渠道 == "SSY") | (df_pie4[["渠道"]].渠道 == "SXDX")].values.tolist()inner_y_data = df_pie4[["1月"]][ (df_pie4[["渠道","1月"]].渠道 == "SX") | (df_pie4[["渠道","1月"]].渠道 == "SSY") | (df_pie4[["渠道","1月"]].渠道 == "SXDX")].values.tolist()inner_data_pair = df_pie4[["渠道","1月"]][ (df_pie4[["渠道","1月"]].渠道 == "SX") | (df_pie4[["渠道","1月"]].渠道 == "SSY") | (df_pie4[["渠道","1月"]].渠道 == "SXDX")].values.tolist()outer_x_data = df_pie4[["渠道"]][ (df_pie4[["渠道"]].渠道 == "SX新保") | (df_pie4[["渠道"]].渠道 == "SX续保") | (df_pie4[["渠道"]].渠道 == "SSY新保") | (df_pie4[["渠道"]].渠道 == "SSY续保") | (df_pie4[["渠道"]].渠道 == "SXDX新保") | (df_pie4[["渠道"]].渠道 == "SXDX续保")].values.tolist()outer_y_data = df_pie4[["1月"]][ (df_pie4[["渠道","1月"]].渠道 == "SX新保") | (df_pie4[["渠道","1月"]].渠道 == "SX续保") | (df_pie4[["渠道","1月"]].渠道 == "SSY新保") | (df_pie4[["渠道","1月"]].渠道 == "SSY续保") | (df_pie4[["渠道","1月"]].渠道 == "SXDX新保") | (df_pie4[["渠道","1月"]].渠道 == "SXDX续保")].values.tolist()outer_data_pair = df_pie4[["渠道","1月"]][ (df_pie4[["渠道","1月"]].渠道 == "SX新保") | (df_pie4[["渠道","1月"]].渠道 == "SX续保") | (df_pie4[["渠道","1月"]].渠道 == "SSY新保") | (df_pie4[["渠道","1月"]].渠道 == "SSY续保") | (df_pie4[["渠道","1月"]].渠道 == "SXDX新保") | (df_pie4[["渠道","1月"]].渠道 == "SXDX续保")].values.tolist()pie4 = (Pie(init_opts=opts.InitOpts(width="800px", height="400px",theme = ThemeType.CHALK)).add(series_name=" ",data_pair=inner_data_pair,radius=[0, "30%"],label_opts=opts.LabelOpts(position="inside",formatter="{c}万",color = "#555555",font_weight = "bold"),).add(series_name=" ",radius=["40%", "55%"],data_pair=outer_data_pair,label_opts=opts.LabelOpts(position="outside",formatter="{b}:{c}万n占比:{d}%",),).set_global_opts(legend_opts=opts.LegendOpts(pos_left="0%",pos_top = "middle", orient="vertical"),title_opts=opts.TitleOpts("销售中心分渠道业绩(微信公众号:会Excel的隔壁老王)".format(i),pos_bottom = "88%", pos_right = "17%")).set_series_opts(tooltip_opts=opts.TooltipOpts(trigger="item", formatter="{b}:{c}万n占比:{d}%"))#.render("nested_pies.html")
)pie4.render_notebook()