from pyecharts import options as opts
from pyecharts.charts import Bar, Grid, Line
# 创建bar对象,并制定画布大小
bar = Bar(init_opts=opts.InitOpts(width='1200px',height='300px'))
# 插入数据
x_data = ['2014', '2015', '2016', '2017', '2018', '2019', '2020', '2021']
# 柱形图设置
bar = (
Bar()
.add_xaxis(x_data)
.add_yaxis(
"蒸发量",
[87430,90912,96225,101885,107147,116390,105764,120215],
# 表示y轴的索引,
yaxis_index=0,
# 柱形图组织的所有图形的z值,控制图形的前后顺序,默认值为2,z值小的图形会被z值大的图形覆盖
z=0,
#稍微修改下颜色
color="orange",
)
# Bar.extend_axis是pyecharts中柱形图组件一个方法,用于扩展坐标轴
.extend_axis(
yaxis=opts.AxisOpts(
# y轴为数据轴
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="blank")
),
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,
max_=130000,
# 显示y轴刻度间隔,默认为1
interval=50000,
axislabel_opts=opts.LabelOpts(font_size=14),
#这里是Y轴间距,由80改为0即两个Y轴重合,当然我们已经删除了原来的一个Y轴,所以相当于把第二Y轴左移
offset=0,
# 设置刻度线
axisline_opts=opts.AxisLineOpts(
linestyle_opts=opts.LineStyleOpts(color="blank")
),
),
# 标题设置
title_opts=opts.TitleOpts(title="2014-2021年中国中医类医疗卫生机构诊疗量",pos_left="center",pos_top="top"),
# 图例设置
legend_opts=opts.LegendOpts(pos_left='40%',pos_bottom='89%'),
# 设置提示框和指示器
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,
#删了一个Y轴,Y轴索引由2改为1
yaxis_index=1,
color="Magenta",
# 显示标签
label_opts=opts.LabelOpts(is_show=False),
)
)
# 将折线图叠加在柱状图上
bar.overlap(line)
# 渲染图表
bar.render_notebook()
04-12
2万+
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
02-27
1万+
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)