饼图用pyecharts有一个功能比matplotlib便捷一点,就是能够自动防止标签叠加,可以在例7了解
from pyecharts.charts import Page,Pie
from pyecharts.commons.utils import JsCode
from pyecharts.faker import Faker
from pyecharts import options as opts
# 例1 基本示例
pie1=(
Pie()
# 饼图添加数据用一个列表里遍历数组方式
.add("",[list(z)for z in zip(Faker.choose(),Faker.values())])
.set_global_opts(title_opts=opts.TitleOpts(title="Pie-基本示例"))
# formatter 饼图、仪表盘、漏斗图: {a}(系列名称),{b}(数据项名称),{c}(数值), {d}(百分比)
.set_series_opts(label_opts=opts.LabelOpts(formatter="{b}:{c}"))
)
pie1.render('pie.html')
# 例2 网页标题与背景颜色
pie2=(# init_opts初始化配置,page_title设置网页标题,bg_color设置画板背景颜色
Pie(init_opts=opts.InitOpts(page_title='网页标题与背景颜色',bg_color='yellow'))
.add("",[list(z)for z in zip(Faker.choose(),Faker.values())])
.set_global_opts(title_opts=opts.TitleOpts(title="网页标题与背景颜色"))
.set_series_opts(label_opts=opts.LabelOpts(formatter="{b}:{c}"))
)
pie2.render('pie.html')
# 例3 设置颜色
pie3=(
Pie()
.add("",[list(z)for z in zip(Faker.choose(),Faker.values())])
.set_colors(["black","orange","green","yellow","red","pink","red"])
# set_colors设置颜色,可以用列表形式
.set_global_opts(title_opts=opts.TitleOpts(title="设置颜色"))
.set_series_opts(label_opts=opts.LabelOpts(formatter="{b}:{c}"))
)
pie3.render('pie.html')
# 例4 调整圆心和图例位置
pie4 = (
Pie()
.add(
"",
[list(z) for z in zip(Faker.choose(), Faker.values())],
center=["65%", "0%"],# 调整饼图圆心位置,第一个参数水平位置,第2个参数设置垂直位置
)
.set_global_opts(
title_opts=opts.TitleOpts(title="Pie-调整位置"),
legend_opts=opts.LegendOpts(pos_bottom="0%"),# 图例调到最底部
)
.set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {c}"))
)
pie4.render('pie.html')
# 例5 环形图
pie5=(
Pie()
.add(
"",
[list(z)for z in zip(Faker.choose(),Faker.values())],
radius=["100%","10%"],# radius第一个外半径,第2个参数内半径
)
.set_global_opts(
title_opts=opts.TitleOpts(title="Pie-Radius"),
legend_opts=opts.LegendOpts(
orient="vertical",pos_top="15%",pos_left="2%"
# orient="vertical"设置图例垂直
),
)
)
pie5.render('pie.html')
# 例6 玫瑰图
pie6 = (
Pie()
.add(
"",
[list(z) for z in zip(Faker.choose(), Faker.values())],
radius=["30%", "75%"],
center=["25%", "50%"],
rosetype="radius",# 设置成玫瑰图,值只有2种,radius和area
# radius:扇区圆心角展现数据的百分比,
label_opts=opts.LabelOpts(is_show=False),
)
.add(
"",
[list(z) for z in zip(Faker.choose(), Faker.values())],
radius=["30%", "75%"],
center=["75%", "50%"],
rosetype="area",# area:所有扇区圆心角相同,半径展现数据大小
)
.set_global_opts(title_opts=opts.TitleOpts(title="Pie-玫瑰图示例"))
)
pie6.render('pie.html')
# 例7 滚动图例加多表数据合成
pie7 = (
Pie()
.add(
"",
[
list(z)
for z in zip(
Faker.choose() + Faker.choose() + Faker.choose()+Faker.choose()+ Faker.choose()+Faker.choose(),
Faker.values() + Faker.values() + Faker.values()+Faker.values()+ Faker.values()+Faker.values(),
)
],
center=["40%", "50%"],
)
.set_global_opts(
title_opts=opts.TitleOpts(title="Pie-Legend 滚动+多表数据合成示例"),
legend_opts=opts.LegendOpts(
type_="scroll", pos_left="80%", orient="vertical"
),
)
)
pie7.render('pie.html')
# 例8 多饼图4饼图
pie8 = (
Pie()# 添加饼图用.add就可以了
.add(
"",
[list(z) for z in zip(["第一", "其他"], [25, 75])],
center=["20%", "30%"],
radius=[60, 80],
label_opts=new_label_opts(),
)
.add(
"",
[list(z) for z in zip(["第二", "其他"], [24, 76])],
center=["55%", "30%"],
radius=[60, 80],
label_opts=new_label_opts(),
)
.add(
"",
[list(z) for z in zip(["第三", "其他"], [14, 86])],
center=["20%", "70%"],
radius=[60, 80],
label_opts=new_label_opts(),
)
.add(
"",
[list(z) for z in zip(["第四", "其他"], [11, 89])],
center=["55%", "70%"],
radius=[60, 80],
label_opts=new_label_opts(),
)
.set_global_opts(
title_opts=opts.TitleOpts(title="Pie-多饼图基本示例"),
legend_opts=opts.LegendOpts(
pos_top="20%", pos_left="80%", orient="vertical"
),
)
)
pie8.render('pie.html')