都21世纪30年代了,还有人问我饼状图怎么画
于是我马不停蹄写了这篇饼状图教程,希望能够帮助你们。
饼状图能够清晰的反映出各项之间、各项和总和之间的占比关系,常见的饼状图主要有以下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)])
.set_global_opts(title_opts=opts.TitleOpts(title="Pie-基本示例"))
.set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {c}"))
)
pie.render_notebook()
根据需要,我们可以随意调整饼状图的位置
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表示
感觉默认颜色不好看?不用怕,我们换了它!
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()
来个造型奇特的柱状图
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:原点位置
为了提高视觉效果,我们需要滚动突出某项重点
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'
一起来感受一下饼状图的美丽吧!
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:内半径和外半径
完整代码请扫码关注公众号并回复饼图获取
-完-
推荐阅读:
用python制作炫酷的滚动地球
python数据分析——pyecharts折线图全解(小白必看)
python数据分析之——pyecharts柱状图进阶篇,带你玩转柱状图
python数据分析——pyecharts柱状图全解(小白必看)
python数据分析之环境搭建——小白也能看懂的Jupyter Notebook安装与使用教程
公众号后台回复下方【关键词】,获取优质资源:
回复关键词【 福利1】,立即获取python数据分析教学视频回复关键词【福利2】,立即获取python学习电子书