import pyecharts.options as opts
from pyecharts.charts import Bar, Line
#插入数据
x_data = ["2014", "2015","2016", "2017","2018", "2019","2020", "2021"]
h_data=[87430,90912,96225,101885,107147,116390,105764,120215]
y_data=[7.40,4.00,5.83,5.81,5.16,8.63,-9.13,13.66]
#绘制柱形图
bar = (
Bar()
#配置x轴与y轴
.add_xaxis(x_data)
.add_yaxis("诊疗量(万人次)",
h_data,
yaxis_index=0,#设置y轴索引,为第一条y轴
color="orange",
z=0,#将z轴的值调整为0,使得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),
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(
title_opts=opts.TitleOpts(title="中国中医类医疗卫生机构诊疗量"), # 标题
tooltip_opts=opts.TooltipOpts(is_show=True, trigger="axis", axis_pointer_type="cross")
)
)
#绘制折线图
line=(
Line()
.add_xaxis(x_data)
.add_yaxis(
'同比增速(%)',
y_axis=y_data,
symbol='triangle',
symbol_size=10,
yaxis_index=1,#设置y轴索引,为第二条y轴
label_opts=opts.LabelOpts(is_show=False)
)
)
#在jupyter notebook里渲染
bar.overlap(line).render_notebook()