拥有圆圆脸蛋的饼图小可爱前来报道!

2020-12-22 11:33:20 浏览数 (1)

先给大家看看多样性的我~~

按照惯例的唠唠叨叨~

小编已经带大家绘制过柱状图,折线图,大家肯定对我们的 PyEcharts 有了一些了解,对其他图形的绘制也跃跃欲试,不要着急,今天我们就来看看圆圆脸蛋的饼图是怎么来的吧!

而且她可不止有一副面孔哦~~~

废话不多说,导包来一波

代码语言:javascript复制
# 导入options配置项
from pyecharts import options as opts
# 导入饼状图
from pyecharts.charts import Pie
# 导入Faker随机生成标签
from pyecharts.faker import Faker
# 使用random随机生成数值
import random

传统饼图来一个~~

代码语言:javascript复制
c = (
    Pie()
    #  将数据进行打包处理   
    .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}"))
)
c.render_notebook()

效果

有艺术细胞的同学还可以自己设置一下配色!我来试试彩虹色

代码语言:javascript复制
c = (
    Pie()
    .add("", [list(z) for z in zip(Faker.choose(), Faker.values())])
    .set_colors(["red", "orange", "yellow", "green", "navy", "blue", "purple"])
    .set_global_opts(title_opts=opts.TitleOpts(title="自定义颜色的饼图"))
    .set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {c}"))
)
c.render_notebook()

效果

来个玫瑰图比例差距更加明显!

代码语言:javascript复制
x_data = Faker.choose()
y_data = Faker.values()
data_pair = [list(z) for z in zip(x_data, y_data)]
data_pair.sort(key=lambda x: x[1])

c=(
    #  设置图片大小     
    Pie(init_opts=opts.InitOpts(width="1000px", height="500px"))
    .add(
        series_name="种类及占比",
        data_pair=data_pair,
        rosetype="radius",
        label_opts=opts.LabelOpts(is_show=True, position="center"),
    )
    .set_global_opts(
        title_opts=opts.TitleOpts(
            title="玫瑰图",
            pos_left="center",
            pos_top="20",
        ),
        legend_opts=opts.LegendOpts(is_show=True),
    )
    .set_series_opts(
        tooltip_opts=opts.TooltipOpts(
            #  设置触发显示的数据格式            
            trigger="item", formatter="{a} <br/>{b}: {c} ({d}%)"
        ),
        label_opts=opts.LabelOpts(),
    )
)
c.render_notebook()

效果

两个玫瑰图怎么同时绘制呢?

代码语言:javascript复制
v = Faker.choose()
c = (
    Pie(init_opts=opts.InitOpts(width="1000px", height="500px"))
    .add(
        "",
        [list(z) for z in zip(v, Faker.values())],
        radius=["30%", "75%"],
        center=["25%", "50%"],
        rosetype="radius",
        label_opts=opts.LabelOpts(is_show=True),
    )
    .add(
        "",
        [list(z) for z in zip(v, Faker.values())],
        radius=["30%", "75%"],
        center=["75%", "50%"],
        rosetype="area",
    )
    .set_global_opts(title_opts=opts.TitleOpts(title="玫瑰对比图"))
)
c.render_notebook()

效果

来试一试环形图

代码语言:javascript复制
c = (Pie()
    .add(
        "种类及占比",
        [list(z) for z in zip(Faker.choose(), Faker.values())],
        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="环形图"))

)
c.render_notebook()

效果

四个环形图排排坐展示一下~~

代码语言:javascript复制
a=random.randint(0,100)
b=random.randint(0,100)
c=random.randint(0,100)
d=random.randint(0,100)

P = (
    Pie(init_opts=opts.InitOpts(width="1000px", height="500px")
)
    .add(
        "",
        [list(z) for z in zip(["A", "其他"], [a, 100-a])],
        center=["20%", "30%"],
        radius=[60, 80],
        label_opts=new_label_opts(),
    )
    .add(
        "",
        [list(z) for z in zip(["B", "其他"], [b, 100-b])],
        center=["55%", "30%"],
        radius=[60, 80],
        label_opts=new_label_opts(),
    )
    .add(
        "",
        [list(z) for z in zip(["C", "其他"], [c, 100-c])],
        center=["20%", "70%"],
        radius=[60, 80],
        label_opts=new_label_opts(),
    )
    .add(
        "",
        [list(z) for z in zip(["D", "其他"], [d, 100-d])],
        center=["55%", "70%"],
        radius=[60, 80],
        label_opts=new_label_opts(),
    )
    .set_global_opts(
        title_opts=opts.TitleOpts(title="多个环形图"),
        legend_opts=opts.LegendOpts(
            type_="scroll", pos_top="20%", pos_left="80%", orient="vertical"
        ),
    )
)
P.render_notebook()

文章开头的嵌套图代码来啦~~

代码语言:javascript复制
inner_x_data = Faker.choose()
inner_y_data = Faker.values()
inner_data_pair = [list(z) for z in zip(inner_x_data, inner_y_data)]

outer_x_data = Faker.choose()
outer_y_data = Faker.values()
outer_data_pair = [list(z) for z in zip(outer_x_data, outer_y_data)]

c=(
    Pie(init_opts=opts.InitOpts())
    .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}%)"
        )
    )
)
c.render_notebook()

可可爱爱的饼图就给大家展示完啦,感兴趣的小伙伴可以下来试一下,也期待大家做出更加精美的图和小编交流哦!

下次小编给大家带来更精彩的图表!敬请期待!

0 人点赞