一.柱形图-折线图
# 导入需要用的图表类
import pyecharts.options as opts
from pyecharts.charts import Bar,Line,Scatter,Pie
x_data = ["2014年", "2015年", "2016年", "2017年", "2018年", "2019年", "2020年", "2021年"]
#绘制柱形图
bar = (
Bar()
.add_xaxis(xaxis_data=x_data)
.add_yaxis(
series_name="诊疗量",
y_axis=[50000.0, 40000.9, 60000.0, 70000.2, 90200.2, 91000.7, 95000.6, 55000.6],
label_opts=opts.LabelOpts(is_show=False),
z =2,
)
.extend_axis(
yaxis=opts.AxisOpts(
name="同比增速(%)",
type_="value",
min_=-20,
max_=20,
interval=10,
axislabel_opts=opts.LabelOpts(formatter="{value} "),
)
)
.set_global_opts(
tooltip_opts=opts.TooltipOpts( #提示框配置项
is_show=True, trigger="axis", axis_pointer_type="cross" # trigger="axis" 触发类型。'axis': 坐标轴触发,主要在柱状图,折线图等会使用类目轴的图表中使用。
),
title_opts=opts.TitleOpts(title="2014-2021年中国中医类医疗卫生机构诊疗量", pos_top="5%",pos_bottom="80%",pos_left="30%",pos_right="45%"),
legend_opts=opts.LegendOpts(is_show=False,pos_top="20%"),
xaxis_opts=opts.AxisOpts(
type_="category", # 'category': 类目轴,适用于离散的类目数据,为该类型时必须通过 data 设置类目数据。
axispointer_opts=opts.AxisPointerOpts(is_show=True, type_="shadow"), #x轴的坐标轴指示器配置项
),
yaxis_opts=opts.AxisOpts(
name="诊疗量(万人次)",
type_="value",
min_=0, #设置刻度范围,最小值
max_=100000, #最大值
interval=50000, # 设置刻度间隔
axislabel_opts=opts.LabelOpts(formatter="{value} "),
axistick_opts=opts.AxisTickOpts(is_show=True), # 设置轴的刻度线
splitline_opts=opts.SplitLineOpts(is_show=True), # 设置轴的轴线
),
)
)
#绘制折线图
line = (
Line()
.add_xaxis(xaxis_data=x_data)
.add_yaxis(
series_name="同比增速(%)",
# 使用的 y 轴的 index,在单个图表实例中存在多个 y 轴的时候有用。
yaxis_index=1, # 第一条y轴索引为0,第二条y轴索引为1
y_axis=[2.0, 2.2, 3.3, 4.5, 6.3, 10.2, 10.3, 13.4, 23.0, 16.5, 12.0, 6.2],
z = 3, # z值小的图形会被z值大的图形覆盖
)
)
#使用overlap将柱形图和折线图叠加在同一张图上
#展示图表(渲染图表)
bar.overlap(line).render_notebook() # 柱形图 层叠 折线图