使用Pyecharts绘制2014-2021年中国中医类医疗卫生机构诊疗量及同比增速图表
下面我们来实现它:
1.导入需要的库
import pyecharts.options as opts
from pyecharts.charts import Bar, Line
2.创建一个年份列表:
x_data = ["2014", "2015", "2016", "2017", "2018", "2019", "2020", "2021"]
3.创建柱状图
bar = (
Bar()
.add_xaxis(xaxis_data=x_data)
.add_yaxis(series_name="诊疗量(万人次)",
y_axis=[87.43, 90.91, 96.22, 101.89, 107.15, 116.39, 105.76, 120.22],
label_opts=opts.LabelOpts(is_show=True),
)
.extend_axis(
yaxis=opts.AxisOpts(
name="同比增速(%)",
type_="value",
min_=-75,
max_=20,
interval=5,
axislabel_opts=opts.LabelOpts(formatter="{value} %")
)
)
4.设置全局选项
.set_global_opts(
tooltip_opts=opts.TooltipOpts(
is_show=True, trigger="axis", axis_pointer_type="cross"
),
xaxis_opts=opts.AxisOpts(
type_="category",
axispointer_opts=opts.AxisPointerOpts(is_show=True, type_="shadow"),
),
yaxis_opts=opts.AxisOpts(
name="诊疗量(万人次)",
type_="value",
min_=50,
max_=150,
interval=10,
axislabel_opts=opts.LabelOpts(formatter="{value} 万人次"),
axistick_opts=opts.AxisTickOpts(is_show=True),
splitline_opts=opts.SplitLineOpts(is_show=True),
),
)
)
5.创建折线图
line = (
Line()
.add_xaxis(xaxis_data=x_data)
.add_yaxis(
series_name="同比增速(%)",
yaxis_index=1,
y_axis=[7.4, 4.0, 5.83, 5.81, 5.16, 8.63, -9.13, 13.66],
label_opts=opts.LabelOpts(is_show=True)
)
)
6.保存为html文件
bar.overlap(line).render("2014-2021年中国中医类医疗卫生机构诊疗量.html")
运行结果如图所示: