一、适用条件
1、饼图:仅有一个要绘制的数据系列,没有负值,几乎没有零值,类别数目无限制,各类别分别代表整个饼图的一部分,
2、基础饼图、环形饼图、玫瑰图、环形玫瑰图、组合饼图
二、代码实现
1.导入所需包
from numpy.lib import index_tricks
from pyecharts import options as opts
from pyecharts.charts import Pie
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.数据整理
###导入数据
df = pd.read_excel('picture.xlsx',sheet_name='pie')
###观察数据
print(df.head())
###选择所需数据
df_country = df.groupby(by = ["商家"]).sum().reset_index().sort_values(by = "销量",ascending= True) ###进行排序
x_list = list(df_country["商家"])
y_list = list(df_country["销量"])
title1 = "商家销量统计"
subtitle1 = "纯属虚构"
df_country1 = df_country.tail(3)
x1_list = list(df_country1["商家"])
y1_list = list(df_country1["销量"])
3.1基础饼图
def pie_chart() -> Pie:
################### 这部分可以直接用,保存成网页
c = (
Pie(init_opts=opts.InitOpts(width="2000px", height="1000px"))
.add("",
data_pair=[list(z) for z in zip(x_list, y_list)],
radius=["0","50%"],
center=["50%", "50%"],
label_opts=opts.LabelOpts(is_show=True,formatter="{b}: {c} ({d}%)"),
)####展示数值
#.set_colors(["blue", "green", "yellow", "red", "pink", "orange", "purple"]) ####设置扇形颜色
.set_global_opts(
title_opts=opts.TitleOpts(
title="title1",
subtitle=subtitle1,
pos_left="center",
pos_top="20",
title_textstyle_opts=opts.TextStyleOpts(color="#fff"),####设置标题颜色
),
legend_opts=opts.LegendOpts(is_show=True),
)
.set_series_opts(
tooltip_opts=opts.TooltipOpts(
trigger="item", formatter="{a} <br/>{b}: {c} ({d}%)"
),
)
# .render("1.html")
)
###################
return c
make_snapshot(snapshot, pie_chart().render(), "3_1.gif")
if __name__ == '__main__':
pie_chart()
3_1.gif
3.2玫瑰图
def pie_chart() -> Pie:
################### 这部分可以直接用,保存成网页
c = (
Pie(init_opts=opts.InitOpts(width="2000px", height="1000px"))
.add("",
data_pair=[list(z) for z in zip(x_list, y_list)],
rosetype="radius", ####玫瑰图
radius=["0","50%"],
center=["50%", "50%"],
label_opts=opts.LabelOpts(is_show=True,formatter="{b}: {c} ({d}%)"),
)####展示数值
#.set_colors(["blue", "green", "yellow", "red", "pink", "orange", "purple"]) ####设置扇形颜色
.set_global_opts(
title_opts=opts.TitleOpts(
title="title1",
subtitle=subtitle1,
pos_left="center",
pos_top="20",
title_textstyle_opts=opts.TextStyleOpts(color="#fff"),####设置标题颜色
),
legend_opts=opts.LegendOpts(is_show=True),
)
.set_series_opts(
tooltip_opts=opts.TooltipOpts(
trigger="item", formatter="{a} <br/>{b}: {c} ({d}%)"
),
)
# .render("1.html")
)
###################
return c
make_snapshot(snapshot, pie_chart().render(), "3_2.gif")
if __name__ == '__main__':
pie_chart()
3_2.gif
3.3环形玫瑰图
def pie_chart() -> Pie:
################### 这部分可以直接用,保存成网页
c = (
Pie(init_opts=opts.InitOpts(width="2000px", height="1000px"))
.add("",
data_pair=[list(z) for z in zip(x_list, y_list)],
rosetype="radius", ####玫瑰图
radius=["40","50%"], ###前面是0 就是饼图,设置数字,就是环形图
center=["50%", "50%"],
label_opts=opts.LabelOpts(is_show=True,formatter="{b}: {c} ({d}%)"),
)####展示数值
#.set_colors(["blue", "green", "yellow", "red", "pink", "orange", "purple"]) ####设置扇形颜色
.set_global_opts(
title_opts=opts.TitleOpts(
title="title1",
subtitle=subtitle1,
pos_left="center",
pos_top="20",
title_textstyle_opts=opts.TextStyleOpts(color="#fff"),####设置标题颜色
),
legend_opts=opts.LegendOpts(is_show=True),
)
.set_series_opts(
tooltip_opts=opts.TooltipOpts(
trigger="item", formatter="{a} <br/>{b}: {c} ({d}%)"
),
)
# .render("1.html")
)
###################
return c
make_snapshot(snapshot, pie_chart().render(), "3_3.gif")
if __name__ == '__main__':
pie_chart()
3_3.gif
3.4组合饼图
c = (
Pie(init_opts=opts.InitOpts(width="1000px", height="800px"))
.add("",
[list(z) for z in zip(x_list, y_list)],
# rosetype="radius",
radius=["40%","50%"],
center=["50%", "50%"],
label_opts=opts.LabelOpts(is_show=True,formatter="{b}: {c} ({d}%)"),
)
.add(
series_name="",
data_pair=[list(z) for z in zip(x1_list, y1_list)],
radius=[0, "30%"],
label_opts=opts.LabelOpts(is_show=True, position="inner",formatter="{b}: {c}"),
)
#.set_colors(["blue", "green", "yellow", "red", "pink", "orange", "purple"]) ####设置扇形颜色
.set_global_opts(
title_opts=opts.TitleOpts(
title="title1",
subtitle=subtitle1,
pos_left="center",
pos_top="20",
title_textstyle_opts=opts.TextStyleOpts(color="#fff"),####设置标题颜色
),
legend_opts=opts.LegendOpts(pos_left="15%",is_show=True),
toolbox_opts=opts.ToolboxOpts(is_show=True),
)
.set_series_opts(
tooltip_opts=opts.TooltipOpts(
trigger="item", formatter="{a} <br/>{b}: {c} ({d}%)"
),
# label_opts=opts.LabelOpts(formatter="{b}: {c} ({d}%)",color="rgba(255, 255, 255, 0.3)")
)
.render("1.html")
)
3_4.gif
网友评论