I heard you don’t know how to draw pie charts in python? Come in and take a look at the tutorial just released

Preface

The text and pictures in this article are from the Internet and are for learning and communication purposes only, and do not have any commercial use. If you have any questions, please contact us for processing.

PS: If you need Python learning materials, you can click on the link below to get it by yourself

Python free learning materials, codes and exchange answers click to join


The pie chart can clearly reflect the relationship between each item and the proportion between each item and the total. The common pie chart mainly has the following 6 types:

1. Basic pie chart

This is the most common type of pie chart, the code is as follows:

'''
如有需要Python学习资料的小伙伴可以加群领取:1136201545
'''

#绘制高中同学现在职业占比饼状图
from pyecharts import options as opts
from pyecharts.charts import Pie
x=['程序员','教师','医生','护士','警察','老板','律师','翻译','运动员']
y=[18,5,3,4,8,2,2,5,1]
pie = (
    Pie()
    .add('高中同学职业占比',[(i,j)for i,j in zip(x,y)])
    .set_global_opts(title_opts=opts.TitleOpts(title="Pie-基本示例"))
    .set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {c}"))
)
pie.render_notebook()

2. Adjust the position of the pie chart label

According to the needs, we can adjust the position of the pie chart at will

from pyecharts import options as opts
from pyecharts.charts import Pie
x=['程序员','教师','医生','护士','警察','老板','律师','翻译','运动员']
y=[18,5,3,4,8,2,2,5,1]
pie = (
    Pie()
    .add('高中同学职业占比',[(i,j)for i,j in zip(x,y)])
    .set_global_opts(
        title_opts=opts.TitleOpts(title="Pie-调整位置"),
        legend_opts=opts.LegendOpts(pos_left="25%"),)
    .set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {c}"))
)
pie.render_notebook()

 

The distances of the legend component from the top, bottom, left, and right of the container are represented by: pos_left, pos_right, pos_top, and pos_bottom respectively

3. Set the color of the pie chart

Feel the default color is not good? Don't be afraid, we changed it!

from pyecharts import options as opts
from pyecharts.charts import Pie
x=['程序员','教师','医生','护士','警察','老板','律师','翻译','运动员']
y=[18,5,3,4,8,2,2,5,1]
pie = (
    Pie()
    .add('高中同学职业占比',[(i,j)for i,j in zip(x,y)])
    .set_colors(["blue","green","yellow","red","pink","orange","purple","lilac","pansy"])
    .set_global_opts(title_opts=opts.TitleOpts(title="Pie-基本示例"))
    .set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {c}"))
)
pie.render_notebook()

4. Nightingale diagram

Let's have a peculiar histogram


from pyecharts import options as opts
from pyecharts.charts import Pie
x=['程序员','教师','医生','护士','警察','老板','律师','翻译','运动员']
y=[18,5,3,4,8,2,2,5,1]
pie = (
    Pie()
    .add(
        series_name='高中同学职业占比',
        data_pair=[(i,j)for i,j in zip(x,y)],
        rosetype="radius",
        radius="85%",
        center=["50%", "50%"],
        label_opts=opts.LabelOpts(is_show=False, position="center"),)
    .set_global_opts(
        title_opts=opts.TitleOpts(
            title="Customized Pie",
            pos_left="center",
            pos_top="20",
            title_textstyle_opts=opts.TextStyleOpts(color="#fff"),
        ),
        legend_opts=opts.LegendOpts(is_show=False),
    )
    .set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {c}"))
)
pie.render_notebook()

 

rosetype: There are 2 types (radius: the percentage of the central angle of the sector to show the data, and the radius shows the size of the data. area: the central angle of all sectors is the same, and only the data size is shown by the radius) center: the origin position

5. Label scrolling pie chart

In order to improve the visual effect, we need to scroll to highlight an important point

from pyecharts import options as opts
from pyecharts.charts import Pie
x=['程序员','教师','医生','护士','警察','老板','律师','翻译','运动员']
y=[18,5,3,4,8,2,2,5,1]
pie = (
    Pie()
    .add('高中同学职业占比',[(i,j)for i,j in zip(x,y)])
    .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}"))
)
pie.render_notebook()

 

LegendOpts parameter introduction: type: legend type,'plain' stands for normal legend,'scroll' stands for scrollable page turning legend pos_left: the distance of the legend component from the left side of the container orient: the layout orientation of the legend list. Optional:'horizontal','vertical'

6.Roses Pie Chart

Let's feel the beauty of the pie chart together!

from pyecharts import options as opts
from pyecharts.charts import Pie
x=['程序员','教师','医生','护士','警察','老板','律师','翻译','运动员']
y=[18,5,3,4,8,2,2,5,1]
pie = (
    Pie()
   .add(
        "",
        [(i,j)for i,j in zip(x,y)],
        radius=["30%", "75%"],
        center=["25%", "50%"],
        rosetype="radius",
        label_opts=opts.LabelOpts(is_show=False),
    )
    .add(
        "",
        [(i,j)for i,j in zip(x,y)],
        radius=["30%", "75%"],
        center=["75%", "50%"],
        rosetype="area",
    )
    .set_global_opts(title_opts=opts.TitleOpts(title="Pie-玫瑰图示例"))
    .set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {c}"))
)
pie.render_notebook()

 

radius: inner radius and outer radius

Guess you like

Origin blog.csdn.net/pythonxuexi123/article/details/114931952