Python笔记之绘制饼图

文章资讯 2020-07-19 11:27:34

Python笔记之绘制饼图

from pyecharts.charts import Pie
from pyecharts import options as opts

# 示例数据
cate = ["中山巧康蔬果仓","广州江高蔬果仓","广州花地湾水产仓","深圳龙岗水产仓","深圳三联蔬果仓","东莞横沥蔬果仓","中山三角水产仓"]
data = [7,5,3,2,2,1,1]
pie = (Pie()
.add('', [list(z) for z in zip(cate, data)],
radius=["30%", "75%"],
rosetype="radius")
.set_global_opts(title_opts=opts.TitleOpts(title="各部门打折单品占比", subtitle="2020年6月份第4周", pos_left="left", pos_top="10%"))
.set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {d}%"))
)

pie.render_notebook()

# 环形图

from pyecharts.charts import Pie

def pie_base() -> Pie:
c = (
Pie(init_opts=opts.InitOpts(page_title="各部门占比"))
.add(
"pie",
[("【综合】 共7个", 33.3), ("【蔬菜】 共6个", 28.6),("【水产】 共6个", 28.6),("【水果】 共2个", 9.5)],
radius=["40%", "60%"],
center=["50%", "50%"],
label_opts=opts.LabelOpts(formatter="{b} ,占比 : {c}%")
)
.set_global_opts(
title_opts=opts.TitleOpts(title="各部门特采占比", pos_left="left", pos_top="0%"),
legend_opts=opts.LegendOpts(orient="vertical", pos_left="00%", pos_top="20%")
)
)
return c
pie_base().render_notebook()

更换主题:

别具一格的绘图风格:

from cutecharts.charts import Pie
attr = ["19点前销售额", "19点后销售额"]
v1 = [80,70]
def pie_legend_font():
chart = Pie("不同岗位平均薪资")
chart.set_options(
labels=attr,
legend_pos="upRight",
font_family='"Times New Roman",Georgia,Serif;',
)
chart.add_series(v1)
return chart
pie_legend_font().render_notebook()

# 导入一系列相关的包
from cutecharts.charts import *
from cutecharts.components import Page
from cutecharts.faker import Faker
import random

attr = ["蔬菜", "水果", "水产", "肉类", "综合"]
v2 = [100, 200, 300, 400, 500]

from cutecharts.charts import Bar
# 绘制条形图
bar = Bar('不同技能提供的工作数量')
bar.set_options(labels = attr,
x_label='技能',y_label='工作数量')
bar.add_series('',data =v2)
bar.render_notebook()

# Pie(init_opts=opts.InitOpts(theme=ThemeType.WALDEN))

# Bar(init_opts=opts.InitOpts(theme=ThemeType.LIGHT))