# 导入库并重命名
from pyecharts import options as opts
from pyecharts.charts import Bar, Line, Pie, Grid,Radar
# 插入数据
x_data = ['2014', '2015', '2016', '2017', '2018', '2019', '2020', '2021']
bar = (
Bar()
# 设置x轴与y轴
.add_xaxis(x_data)
.add_yaxis(
"诊疗量",
[87430, 90912, 96225, 101885, 107147, 116390, 105764, 120215],
yaxis_index=0,# 设置y轴索引,为第一条y轴
z=0,# z轴的值调整为0,使z值更大的折线图能覆盖在柱形图上
color="orange",
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),
#设置刻度样式
axisline_opts=opts.AxisLineOpts(
linestyle_opts=opts.LineStyleOpts(color="black")
),
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,
axisline_opts=opts.AxisLineOpts(
linestyle_opts=opts.LineStyleOpts(color="black")
),
),
title_opts=opts.TitleOpts(title="2014-2021年中国中医类医疗卫生机构诊疗量", pos_left="35%", pos_top="2%"),
legend_opts=opts.LegendOpts(is_show=False),
tooltip_opts=opts.TooltipOpts(trigger="axis", axis_pointer_type="cross"),
)
)
# 绘制折线图
line = (
Line()
.add_xaxis(x_data)
.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轴
color="Magenta",# 设置线条颜色
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))
)
# 绘制堆积柱形图
data1 = (20.3, 22.0, 23.5, 22.5, 22.3)
data2 = (79.7, 78.0, 76.5, 77.5, 77.7)
bar1 = (
Bar()
# 设置x轴与y轴
.add_xaxis(['2019', '2020', '2021', '2022', '2023'])
.add_yaxis("供应商A", data1, stack="stack1", xaxis_index=1, yaxis_index=2)
.add_yaxis("供应商B", data2, 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_=120,# 刻度最大值
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将他们并在一张画布上,再用pos_调整各图在画布上的位置
grid = (
Grid()
.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()
用pyecharts绘制组合图
最新推荐文章于 2023-12-23 15:15:03 发布