11.9.2 不同职业群体的购买力分析
为了分析该企业不同职业群体的购买力情况,绘制了不同群体的销售额的玫瑰图,Python代码如下:
# -*- coding: utf-8 -*-
#声明Notebook类型,必须在引入pyecharts.charts等模块前声明
from pyecharts.globals import CurrentConfig, NotebookType
CurrentConfig.NOTEBOOK_TYPE = NotebookType.JUPYTER_LAB
from pyecharts import options as opts
from pyecharts.charts import Page, Pie
from impala.dbapi import connect
#连接Hadoop数据库
v1 = []
v2 = []
conn = connect(host='192.168.1.7', port=10000, database='sales',auth_mechanism='NOSASL',user='root')
cursor = conn.cursor()
#读取Hadoop表数据
sql_num = "SELECT occupation,ROUND(SUM(sales/10000),2) FROM customers,orders WHERE customers.cust_id=orders.cust_id and dt=2019 GROUP BY occupation"
cursor.execute(sql_num)
sh = cursor.fetchall()
for s in sh:
v1.append(s[0])
v2.append(s[1])
#画玫瑰图
def rosetype() -> Pie:
c = (
Pie()
.add(
"",
[list(z) for z in zip(v1, v2)],
radius=["30%", "75%"],
center=["50%", "55%"],
rosetype="radius",
label_opts=opts.LabelOpts(is_show=False),
)
.set_colors(["blue", "green", "purple", "red", "silver"]) #设置颜色
.set_global_opts(title_opts=opts.TitleOpts(title="2019年不同职业群体的购买力分析", subtitle="2019年销售经营状况分析"),
legend_opts=opts.LegendOpts(orient="horizontal", pos_top="5%", pos_left="30%"),
toolbox_opts=opts.ToolboxOpts())
.set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {c}"))
)
return c
#第一次渲染时候调用load_javasrcript文件
rosetype().load_javascript()
#展示数据可视化图表
rosetype().render_notebook()
在Jupyter lab中运行上述代码,生成如图11-9所示的玫瑰图。
图11-9 玫瑰图