from pyecharts import options as opts
from pyecharts.charts import Bar, Grid, Line, Radar, Pie# 定义数据
data1 = ['2014', '2015', '2016', '2017', '2018', '2019', '2020', '2021']
v1 = [87430, 90912, 96225, 101885, 107147, 116390, 105764, 120215]
v2 = [7.40, 4.00, 5.83, 5.81, 5.16, 8.63, -9.13, 13.66]# 创建柱状图
bar1 = (
Bar()
.add_xaxis(data1)
.add_yaxis("诊疗量", v1, z=0, color="pink", xaxis_index=0, yaxis_index=0) # 添加柱状图数据,设置颜色和坐标轴
.extend_axis(
yaxis=opts.AxisOpts(
name="同比增速(%)",
min_=-20,
max_=20,
axislabel_opts=opts.LabelOpts(formatter="{value}%"), # 设置坐标轴标签格式
axisline_opts=opts.AxisLineOpts(
linestyle_opts=opts.LineStyleOpts(color="pink") # 设置坐标轴线样式
),
)
)
.set_series_opts(label_opts=opts.LabelOpts(is_show=True)) # 设置数据标签显示
.set_global_opts(
yaxis_opts=opts.AxisOpts(
name="诊疗量(万人次)",
axisline_opts=opts.AxisLineOpts(
linestyle_opts=opts.LineStyleOpts(color="pink") # 设置坐标轴线样式
),
),
title_opts=opts.TitleOpts(title="2014-2021年中国中医类医疗卫生机构诊疗量"), # 设置标题
legend_opts=opts.LegendOpts(pos_right="10%"), # 设置图例位置
)
)# 创建折线图
line1 = Line().add_xaxis(data1).add_yaxis("同比增速", v2, color="pink", xaxis_index=0, yaxis_index=1) # 添加折线图数据,设置颜色和坐标轴# 创建数据列表
list1 = [
{"value": 20.3, "percent": 20.3 / (20.3 + 79.7)}, # 计算百分比
{"value": 22.0, "percent": 22.0 / (22.0 + 78.0)},
{"value": 23.5, "percent": 23.5 / (23.5 + 76.5)},
{"value": 22.5, "percent": 22.5 / (22.5 + 77.5)},
{"value": 22.3, "percent": 22.3 / (22.3 + 77.7)},
]list2 = [
{"value": 79.7, "percent": 79.7 / (20.3 + 79.7)}, # 计算百分比
{"value": 78.0, "percent": 78.0 / (22.0 + 78.0)},
{"value": 76.5, "percent": 76.5 / (23.5 + 76.5)},
{"value": 77.5, "percent": 77.5 / (22.5 + 77.5)},
{"value": 77.7, "percent": 77.7 / (22.3 + 77.7)},
]# 创建堆叠柱状图
bar3 = (
Bar()
.add_xaxis([2019, 2020, 2021, 2022, 2023])
.add_yaxis("跨国企业占比", list1, stack="stack1", xaxis_index=1, yaxis_index=2) # 添加堆叠柱状图数据,设置堆叠方式和坐标轴
.add_yaxis("本土企业占比", list2, stack="stack1", xaxis_index=1, yaxis_index=2) # 添加堆叠柱状图数据,设置堆叠方式和坐标轴
.set_global_opts(
title_opts=opts.TitleOpts(title='全国药店中药饮片供应商占比情况', pos_top="50%", pos_left="35%"), # 设置标题和位置
legend_opts=opts.LegendOpts(is_show=False), # 隐藏图例
)
)# 创建饼图
pie2 = (
Pie()
.add(
"",
[
('20岁以下', 2.2),
('20-30岁', 27.9),
('31-40岁', 56.2),
('41-50岁', 10.9),
('51岁以上', 2.8)
],
center=["20%", "80%"], # 设置饼图中心位置
radius=[30, 80] # 设置饼图半径范围
)
.set_global_opts(
title_opts=opts.TitleOpts(title='中药材消费者画像数据', pos_top="50%"), # 设置标题和位置
legend_opts=opts.LegendOpts(is_show=False), # 隐藏图例
)
)# 将柱状图和折线图叠加在一起
overlap_1 = bar1.overlap(line1) # 叠加柱状图和折线图# 创建网格布局
grid = (
Grid()
.add(overlap_1, grid_opts=opts.GridOpts(pos_bottom="58%"), is_control_axis_index=True) # 添加叠加图表到网格布局,设置位置和坐标轴
.add(
bar3,
grid_opts=opts.GridOpts(pos_top="58%", pos_bottom="5%", pos_left="40%", pos_right="35%"), # 设置位置
is_control_axis_index=True
)
.add(
pie2,
grid_opts=opts.GridOpts(pos_top="58%", pos_right="65%"), # 设置位置
is_control_axis_index=True
)
)# 在notebook中渲染图表
grid.render_notebook()