代码如下:
#导入库并重命名
from pyecharts import options as opts
from pyecharts.charts import Bar, Line, Pie, Radar, Grid
from pyecharts.globals import ThemeType
#插入柱形图数据
bar = (
Bar()
#设置x轴与y轴
.add_xaxis(['2014', '2015', '2016', '2017', '2018', '2019', '2020', '2021'])
.add_yaxis(
"诊疗量",
[87430, 90912, 96225, 101885, 107147, 116390, 105764, 120215],
yaxis_index=0,#设置y轴索引,为第一条y轴
z=0,#z值大的覆盖z值小的
label_opts=opts.LabelOpts(is_show=False)
)
#拓展并配置第二条y轴
.extend_axis(
yaxis=opts.AxisOpts(
type_="value",
name="同比增速(%)",
name_location="center",
name_gap=30,
min_=-20,
max_=20,
axislabel_opts=opts.LabelOpts(font_size=14),
splitline_opts=opts.SplitLineOpts(
is_show=True, linestyle_opts=opts.LineStyleOpts(opacity=1)
),
)
)
#设置全局配置
.set_global_opts(
yaxis_opts=opts.AxisOpts(
name="诊疗量(万人次)",
name_location="center",
name_gap=60,
min_=0,
interval=50000,
axislabel_opts=opts.LabelOpts(font_size=14),
offset=0,
),
title_opts=opts.TitleOpts(title="2014-2021年中国中医类医疗卫生机构诊疗量", pos_left="30%", pos_top="2%"),
legend_opts=opts.LegendOpts(is_show=True,pos_right='8%'),
tooltip_opts=opts.TooltipOpts(trigger="axis", axis_pointer_type="cross"),
)
)
#绘制折线图
line = (
Line()
.add_xaxis(['2014', '2015', '2016', '2017', '2018', '2019', '2020', '2021'])
.add_yaxis(
"同比增速(%)",
[7.40, 4.00, 5.83, 5.81, 5.16, 8.63, -9.13, 13.66],
symbol='triangle',#设置线条样式
symbol_size=15,
yaxis_index=1,#设置y轴索引,为第二条y轴
label_opts=opts.LabelOpts(is_show=False),
)
)
#绘制饼图
pie_demo = (
Pie()
.add("", [('20岁以下', 2.2), ('20-20岁', 27.9),
('31-40岁', 56.2), ('41-50岁', 10.9),
('51岁以上', 2.8)], center=["18%", "75%"], radius=[30, 56],#设置内圆与外圆半径
label_opts=opts.LabelOpts(formatter="{b}: {d}%"))
.set_global_opts(title_opts=opts.TitleOpts(title="中药材消费者画像", pos_left="10%", pos_top="54%"),#设置标题
legend_opts=opts.LegendOpts(is_show=False))
)
#绘制堆积柱形图
h1 = (20.3, 22.0, 23.5, 22.5, 22.3)
h2 = (79.7, 78.0, 76.5, 77.5, 77.7)
bar1 = (
Bar()
#设置x轴与y轴
.add_xaxis(['2019', '2020', '2021', '2022', '2023'])
.add_yaxis("供应商A", h1, stack="stack1", xaxis_index=1, yaxis_index=2)
.add_yaxis("供应商B", h2, stack="stack1", xaxis_index=1, yaxis_index=2)
.set_series_opts(label_opts=opts.LabelOpts(is_show=False))
.set_global_opts(
yaxis_opts=opts.AxisOpts(
min_=0,#设置刻度最小值
max_=125,#设置刻度最大值
interval=25#设置刻度间差值
),
title_opts=opts.TitleOpts(title="全国药店中药饮片供应商占比", pos_left='40%', pos_top='54%'),
legend_opts=opts.LegendOpts(is_show=False),
xaxis_opts=opts.AxisOpts(max_=4)
)
)
overlap = bar.overlap(line)
grid = (
Grid(init_opts=opts.InitOpts(theme=ThemeType.ROMA))
.add(overlap, grid_opts=opts.GridOpts(pos_bottom="50%", pos_right="7%", pos_left="20%", pos_top="8%"),
is_control_axis_index=True)
.add(pie_demo, grid_opts=opts.GridOpts(pos_bottom="10%", pos_left="40%", pos_top="60%", pos_right="30%"))
.add(bar1, grid_opts=opts.GridOpts(pos_bottom="20%", pos_right="20%", pos_left="50%", pos_top="20%"),
is_control_axis_index=True)
)
#渲染图表
grid.render_notebook()
运行结果图如下: