听说你不会用python画饼图?赶紧进来看一下刚出炉的教程
饼状图能够清晰的反映出各项之间、各项和总和之间的占比关系,常见的饼状图主要有以下6种类型:
1.基本饼状图
这是饼状图最常见的类型,代码如下:
'''
如有需要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.调整饼图标签位置
根据需要,我们可以随意调整饼状图的位置
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()
图例组件离容器上下左右的距离分别用:pos_left、pos_right、pos_top、pos_bottom表示
3.设置饼图颜色
感觉默认颜色不好看?不用怕,我们换了它!
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.南丁格尔图
来个造型奇特的柱状图
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:有2种类型(radius:扇区圆心角展现数据的百分比,半径展现数据的大小。area:所有扇区圆心角相同,仅通过半径展现数据大小) center:原点位置
5.标签滚动饼图
为了提高视觉效果,我们需要滚动突出某项重点
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参数介绍:type:图例类型,'plain'代表普通图例,'scroll'代表可滚动翻页图例 pos_left:图例组件离容器左侧的距离 orient:图例列表的布局朝向。可选:'horizontal', 'vertical'
6.玫瑰花饼图
一起来感受一下饼状图的美丽吧!
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:内半径和外半径