PyEcharts 基本图表之饼图 educoder
程序员文章站
2022-05-26 21:32:26
...
第1关:Pie:饼图(一)
from PreTest import *
from pyecharts import options as opts
from pyecharts.render import make_snapshot
from snapshot_phantomjs import snapshot
from pyecharts.charts import Pie
from pyecharts.faker import Faker
data = [list(z) for z in zip(Faker.choose(), Faker.values())]
def pie_chart() -> Pie:
# ********* Begin *********#
pie = (
Pie()
.add("",data)
.set_colors(["blue", "green", "yellow", "red", "pink", "orange", "purple"])
.set_global_opts(
title_opts=opts.TitleOpts(title="Pie-基本示例")
)
.set_series_opts(
label_opts=opts.LabelOpts(formatter="{b}: {c}")
)
)
# ********** End **********#
return pie
make_snapshot(snapshot, pie_chart().render("Result/render.html"), 'StudentAnswer/student_answer.png') # 输出图片
make_snapshot(snapshot, pie_base(data).render(), "StandardAnswer/task1/standard_answer_1.png")
第2关:Pie:饼图(二)
from PreTest import *
from pyecharts import options as opts
from pyecharts.faker import Faker
from pyecharts.render import make_snapshot
from snapshot_phantomjs import snapshot
from pyecharts.charts import Pie
data = [
list(z)
for z in zip(
Faker.choose() + Faker.choose() + Faker.choose() + Faker.choose() + Faker.choose(),
Faker.values() + Faker.values() + Faker.values() + Faker.values() + Faker.values(),
)
]
def pie_chart() -> Pie:
# ********* Begin *********#
pie = (
Pie()
.add("",data,center=["40%", "50%"])
.set_global_opts(
title_opts=opts.TitleOpts(title="Pie-Legend 滚动"),
legend_opts=opts.LegendOpts(type_="scroll",pos_left="80%",orient="vertical")
)
.set_series_opts(
label_opts=opts.LabelOpts(formatter="{b}: {c}")
)
)
# ********** End **********#
return pie
make_snapshot(snapshot, pie_chart().render("Result/render.html"), "StudentAnswer/student_answer.png") # 输出图片
make_snapshot(snapshot, pie_scroll_legend(data).render(), "StandardAnswer/task2/standard_answer_2.png")
第3关:Pie:饼图(三)
from PreTest import *
from pyecharts import options as opts
from pyecharts.render import make_snapshot
from snapshot_phantomjs import snapshot
from pyecharts.charts import Pie
from pyecharts.faker import Faker
# ********* Begin *********#
data = [list(z) for z in zip(Faker.choose(), Faker.values())]
# ********** End **********#
def pie_chart() -> Pie:
# ********* Begin *********#
pie = (
Pie()
.add(
"",
data,
radius=["30%", "75%"],
center=["25%", "50%"],
rosetype="radius",
label_opts=opts.LabelOpts(is_show=False),
)
.add(
"",
data,
radius=["30%", "75%"],
center=["75%", "50%"],
rosetype="area",
)
.set_global_opts(title_opts=opts.TitleOpts(title="Pie-玫瑰图示例"))
)
# ********** End **********#
return pie
make_snapshot(snapshot, pie_chart().render("Result/render.html"), 'StudentAnswer/student_answer.png') # 输出图片
make_snapshot(snapshot, pie_rose_type(data).render(), "StandardAnswer/task3/standard_answer_3.png")
第4关:Pie:饼图(四)
from PreTest import *
from pyecharts import options as opts
from pyecharts.render import make_snapshot
from snapshot_phantomjs import snapshot
from pyecharts.charts import Pie
from pyecharts.faker import Faker
# ********* Begin *********#
data = [list(z) for z in zip(Faker.choose(), Faker.values())]
# ********** End **********#
def pie_chart() -> Pie:
# ********* Begin *********#
pie = (
Pie()
.add(
"",
data,
radius=["40%", "55%"],
label_opts=opts.LabelOpts(
position="outside",
formatter="{a|{a}}{abg|}\n{hr|}\n {b|{b}: }{c} {per|{d}%} ",
background_color="#eee",
border_color="#aaa",
border_width=1,
border_radius=4,
rich={
"a": {"color": "#999", "lineHeight": 22, "align": "center"},
"abg": {
"backgroundColor": "#e3e3e3",
"width": "100%",
"align": "right",
"height": 22,
"borderRadius": [4, 4, 0, 0],
},
"hr": {
"borderColor": "#aaa",
"width": "100%",
"borderWidth": 0.5,
"height": 0,
},
"b": {"fontSize": 16, "lineHeight": 33},
"per": {
"color": "#eee",
"backgroundColor": "#334455",
"padding": [2, 4],
"borderRadius": 2,
},
},
),
)
.set_global_opts(title_opts=opts.TitleOpts(title="Pie-富文本示例"))
)
# ********** End **********#
return pie
make_snapshot(snapshot, pie_chart().render("Result/render.html"), 'StudentAnswer/student_answer.png') # 输出图片
make_snapshot(snapshot, pie_rich_label(data).render(), "StandardAnswer/task4/standard_answer_4.png")
第5关:Pie:饼图(五)
from PreTest import *
from pyecharts import options as opts
from pyecharts.faker import Faker
from pyecharts.render import make_snapshot
from snapshot_phantomjs import snapshot
from pyecharts.charts import Pie
inner_x_data = ["直达", "营销广告", "搜索引擎"]
inner_y_data = [335, 679, 1548]
inner_data_pair = [list(z) for z in zip(inner_x_data, inner_y_data)]
outer_x_data = ["直达", "营销广告", "搜索引擎", "邮件营销", "联盟广告", "视频广告", "百度", "谷歌", "必应", "其他"]
outer_y_data = [335, 310, 234, 135, 1048, 251, 147, 102]
outer_data_pair = [list(z) for z in zip(outer_x_data, outer_y_data)]
def pie_chart() -> Pie:
# ********* Begin *********#
pie = (
Pie(init_opts=opts.InitOpts(width="1600px", height="800px"))
.add(
series_name="访问来源",
data_pair=inner_data_pair,
radius=[0, "30%"],
label_opts=opts.LabelOpts(position="inner"),
)
.add(
series_name="访问来源",
radius=["40%", "55%"],
data_pair=outer_data_pair,
label_opts=opts.LabelOpts(
position="outside",
formatter="{a|{a}}{abg|}\n{hr|}\n {b|{b}: }{c} {per|{d}%} ",
background_color="#eee",
border_color="#aaa",
border_width=1,
border_radius=4,
rich={
"a": {"color": "#999", "lineHeight": 22, "align": "center"},
"abg": {
"backgroundColor": "#e3e3e3",
"width": "100%",
"align": "right",
"height": 22,
"borderRadius": [4, 4, 0, 0],
},
"hr": {
"borderColor": "#aaa",
"width": "100%",
"borderWidth": 0.5,
"height": 0,
},
"b": {"fontSize": 16, "lineHeight": 33},
"per": {
"color": "#eee",
"backgroundColor": "#334455",
"padding": [2, 4],
"borderRadius": 2,
},
},
),
)
.set_global_opts(legend_opts=opts.LegendOpts(pos_left="left", orient="vertical"))
.set_series_opts(
tooltip_opts=opts.TooltipOpts(
trigger="item", formatter="{a} <br/>{b}: {c} ({d}%)"
)
)
)
# ********** End **********#
return pie
make_snapshot(snapshot, pie_chart().render("Result/render.html"), "StudentAnswer/student_answer.png") # 输出图片
make_snapshot(snapshot, pie_nested(inner_data_pair, outer_data_pair).render(), "StandardAnswer/task5/standard_answer_5.png")
上一篇: PyEcharts -