目录
柱形折线融合图
import pyecharts.options as opts
from pyecharts.charts import Bar, Line
x_data = ["2014", "2015", "2016", "2017", "2018", "2019", "2020", "2021"]
# 创建柱状图
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),
)
# 扩展额外的y轴来显示同比增速
.extend_axis(
yaxis=opts.AxisOpts(
name="同比增速(%)",
type_="value",
min_=-75,
max_=20,
interval=5,
axislabel_opts=opts.LabelOpts(formatter="{value} %"),
)
)
# 设置全局选项
.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),
),
)
)
# 创建折线图
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),
)
)
# 将折线图与柱状图结合并渲染为HTML文件
bar.overlap(line).render("2014-2021年中国中医类医疗卫生机构诊疗量.html")
结果如下:
圆环图
# 导入pyecharts库中的options和Pie类
from pyecharts import options as opts
from pyecharts.charts import Pie
# 定义数据,每个年龄组别以及占比
age_data = [("20岁以下", 2.2), ("20-30岁", 27.9), ("31-40岁", 56.2), ("41-50岁", 10.9), ("51岁以上", 2.8)]
# 创建Pie对象,并通过add()向图表中添加数据
c = (
Pie() # 创建饼形图对象
.add( # 添加数据
"", # 图例名称为空
age_data, # 数据源
radius=["40%", "55%"], # 内径和外径的大小
label_opts=opts.LabelOpts( # 标签样式和位置
position="outside", # 标签放置在外部
formatter="{a|{a}}{abg|}\n{hr|}\n {b|{b}: }{c} {per|{d}%} ", # 富文本标签格式化字符串
background_color="#eee", # 标签背景色
border_color="#aaa", # 边框颜色
border_width=1, # 边框宽度
border_radius=4, # 边框圆角
rich={ # 富文本配置
"a": {"color": "#999", "lineHeight": 22, "align": "center"}, # 饼图外文本颜色和对齐方式
"abg": { # 扇形区域背景的配置
"backgroundColor": "#e3e3e3",
"width": "100%",
"align": "right",
"height": 22,
"borderRadius": [4, 4, 0, 0],
},
"hr": { # 水平分隔线的配置
"borderColor": "#aaa",
"width": "100%",
"borderWidth": 0.5,
"height": 0,
},
"b": {"fontSize": 16, "lineHeight": 33}, # 饼图外标签文字大小和行高
"per": { # 饼图扇形区域的占比文字样式
"color": "#eee",
"backgroundColor": "#334455",
"padding": [2, 4],
"borderRadius": 2,
},
},
),
)
# 设置全局参数,包括标题
.set_global_opts(title_opts=opts.TitleOpts(title="中药材消费者画像数据"))
# 将图表渲染到HTML文件中
.render("02.html")
)
结果如下:
柱形堆积图
from pyecharts import options as opts # 导入 pyecharts 库的 options 模块
from pyecharts.charts import Bar # 导入 pyecharts 库的 Bar 类
x_data = ["2019", "2020", "2021", "2022", "2023"] # 定义年份数据
y_data1 = [20.3, 22.0, 23.5, 22.5, 22.3] # 定义跨国企业占比数据
y_data2 = [79.7, 78.0, 76.5, 77.5, 77.7] # 定义本土企业占比数据
c = (
Bar() # 创建一个 Bar 类实例
.add_xaxis(x_data) # 添加 x 轴数据
.add_yaxis("跨国企业占比", y_data1, stack="stack1") # 添加跨国企业占比数据
.add_yaxis("本土企业占比", y_data2, stack="stack1") # 添加本土企业占比数据
.set_series_opts(label_opts=opts.LabelOpts(is_show=False)) # 设置系列选项,隐藏标签
.set_global_opts(
title_opts=opts.TitleOpts(title="跨国企业与本土企业占比"), # 设置标题为“跨国企业与本土企业占比”
xaxis_opts=opts.AxisOpts(name="年份"), # 设置 x 轴名称
yaxis_opts=opts.AxisOpts(name="占比(%)"), # 设置 y 轴名称
legend_opts=opts.LegendOpts(pos_left="right"), # 设置图例位置为右侧
)
.render("03.html") # 将图表渲染成 html 文件,保存为“03.html”
)
结果如下:
雷达图
from pyecharts import options as opts # 导入 pyecharts 库的配置模块
from pyecharts.charts import Radar # 导入雷达图模块
data = [{"value": [33, 45, 3, 9, 6, 4], "name": "药品类型"}] # 定义雷达图的数据,包括数值和名称
c_schema = [ # 定义雷达图的各个维度的配置
{"name": "化学药", "max": 50, "min": 0},
{"name": "中成药", "max": 50, "min": 0},
{"name": "生物制品", "max": 50, "min": 0},
{"name": "保健品", "max": 50, "min": 0},
{"name": "中药饮片", "max": 50, "min": 0},
{"name": "其他", "max": 50, "min": 0},
]
c = ( # 创建雷达图对象
Radar()
.add_schema( # 添加雷达图的配置
schema=c_schema, # 设置维度的配置
shape="circle", # 设置雷达图的形状为圆形
center=["50%", "50%"], # 设置雷达图的中心位置为页面的中心
radius="80%", # 设置雷达图的半径为页面宽度的80%
angleaxis_opts=opts.AngleAxisOpts( # 设置角度轴的配置
max_=360, # 角度轴的最大值为360度
is_clockwise=False, # 不顺时针显示角度轴
interval=5, # 设置角度轴刻度间隔为5度
axistick_opts=opts.AxisTickOpts(is_show=False), # 不显示角度轴刻度线
axisline_opts=opts.AxisLineOpts(is_show=False), # 不显示角度轴线
axislabel_opts=opts.LabelOpts(is_show=False), # 不显示角度轴标签
splitline_opts=opts.SplitLineOpts(is_show=False), # 不显示角度轴分割线
),
radiusaxis_opts=opts.RadiusAxisOpts( # 设置半径轴的配置
min_=0, # 半径轴的最小值为0
max_=50, # 半径轴的最大值为50
interval=10, # 设置半径轴刻度间隔为10
splitarea_opts=opts.SplitAreaOpts(is_show=True, areastyle_opts=opts.AreaStyleOpts(opacity=1)), # 显示半径轴的分隔区域,设置透明度为1
),
polar_opts=opts.PolarOpts(), # 极坐标系配置项
splitarea_opt=opts.SplitAreaOpts(is_show=False), # 不显示分割区域
splitline_opt=opts.SplitLineOpts(is_show=False), # 不显示分割线
)
.add( # 添加雷达图的数据系列
series_name="药品类型", # 数据系列的名称
data=data, # 数据
areastyle_opts=opts.AreaStyleOpts(opacity=0.1), # 设置数据区域的透明度为0.1
linestyle_opts=opts.LineStyleOpts(width=1), # 设置线条的宽度为1
)
.set_colors(["#4587E7"]) # 设置颜色
.set_global_opts(title_opts=opts.TitleOpts(title="中药材消费者画像数据")) # 设置全局配置,包括标题
.render("04.html") # 将图表渲染为 HTML 文件
)
结果如下:
并行多图
# 引入必要的库
from pyecharts import options as opts
from pyecharts.charts import Bar, Grid, Pie, Line, Radar
# 图表01:创建柱状图和折线图组合的Grid
# 定义x轴和y轴数据
x_data = ["2014", "2015", "2016", "2017", "2018", "2019", "2020", "2021"]
bar = (
Bar() # 创建柱状图
.add_xaxis(xaxis_data=x_data) # 添加x轴数据
.add_yaxis(
series_name="诊疗量(万人次)",
y_axis=[87.43, 90.91, 96.22, 101.89, 107.15, 116.39, 105.76, 120.22], # 添加y轴数据
label_opts=opts.LabelOpts(is_show=True),
)
.extend_axis(
yaxis=opts.AxisOpts( # 添加另一个y轴
name="同比增速(%)",
type_="value",
min_=-75,
max_=20,
interval=5,
axislabel_opts=opts.LabelOpts(formatter="{value} %"),
)
)
.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),
),
legend_opts=opts.LegendOpts(orient="horizontal", pos_top="top", pos_right=0) # 设置图例的位置和样式
)
)
line = (
Line() # 创建折线图
.add_xaxis(xaxis_data=x_data) # 添加x轴数据
.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], # 添加y轴数据
label_opts=opts.LabelOpts(is_show=True),
)
)
bar.set_global_opts(title_opts=opts.TitleOpts(title="2014-2021年中国中医类医疗卫生机构诊疗量")) # 设置图表的标题
bar.overlap(line) # 将折线图与柱状图重叠在一起
# 图表02:创建环形图
# 定义饼图的数据
age_data = [("20岁以下", 2.2), ("20-30岁", 27.9), ("31-40岁", 56.2), ("41-50岁", 10.9), ("51岁以上", 2.8)]
pie = (
Pie() # 创建饼图
.add(
"",
age_data,
radius=["40%", "55%"], # 设置内外半径
label_opts=opts.LabelOpts(
position="outside",
formatter="{a|{a}}{abg|}\n{hr|}\n {b|{b}: }{c} {per|{d}%} ", # 标签的样式
background_color="#eee",
border_color="#aaa",
border_width=1,
border_radius=4,
rich={
"a": {"color": "#999", "lineHeight": 22, "align": "center"},
"abg": {
"backgroundColor": "#e3e3e3",
"width": "100%",
"align": "right",
"height": 22,
"borderRadius": [4, 4, 0, 0],
},
"hr": {
"borderColor": "#aaa",
"width": "100%",
"borderWidth": 0.5,
"height": 0,
},
"b": {"fontSize": 16, "lineHeight": 33},
"per": {
"color": "#eee",
"backgroundColor": "#334455",
"padding": [2, 4],
"borderRadius": 2,
},
},
),
)
.set_global_opts(title_opts=opts.TitleOpts(title="中药材消费者画像数据")) # 设置图表标题
)
# 图表03:创建堆叠柱状图
# 定义x轴和y轴数据
x_data = ["2019", "2020", "2021", "2022", "2023"]
y_data1 = [20.3, 22.0, 23.5, 22.5, 22.3]
y_data2 = [79.7, 78.0, 76.5, 77.5, 77.7]
bar_stack = (
Bar() # 创建柱状图
.add_xaxis(x_data) # 添加x轴数据
.add_yaxis("跨国企业占比", y_data1, stack="stack1") # 添加第一组y轴数据,并设置堆叠方式
.add_yaxis("本土企业占比", y_data2, stack="stack1") # 添加第二组y轴数据,并设置堆叠方式
.set_series_opts(label_opts=opts.LabelOpts(is_show=False)) # 设置系列的标签样式
.set_global_opts(
title_opts=opts.TitleOpts(title="跨国企业与本土企业占比"), # 设置图表标题
xaxis_opts=opts.AxisOpts(name="年份"), # 设置x轴的名称
yaxis_opts=opts.AxisOpts(name="占比(%)"), # 设置y轴的名称
legend_opts=opts.LegendOpts(pos_left="right"), # 设置图例的位置和样式
)
)
# 图表04:创建雷达图
# 定义雷达图的数据
data = [{"value": [33, 45, 3, 9, 6, 4], "name": "药品类型"}]
radar = (
Radar() # 创建雷达图
.add_schema( # 添加雷达图的基本配置
schema=[
opts.RadarIndicatorItem(name="化学药", max_=50, min_=0),
opts.RadarIndicatorItem(name="中成药", max_=50, min_=0),
opts.RadarIndicatorItem(name="生物制品", max_=50, min_=0),
opts.RadarIndicatorItem(name="保健品", max_=50, min_=0),
opts.RadarIndicatorItem(name="中药饮片", max_=50, min_=0),
opts.RadarIndicatorItem(name="其他", max_=50, min_=0),
],
shape="circle",
center=["50%", "50%"],
radius="80%",
angleaxis_opts=opts.AngleAxisOpts(
max_=360,
is_clockwise=False,
interval=5,
axistick_opts=opts.AxisTickOpts(is_show=False),
axisline_opts=opts.AxisLineOpts(is_show=False),
axislabel_opts=opts.LabelOpts(is_show=False),
splitline_opts=opts.SplitLineOpts(is_show=False),
),
radiusaxis_opts=opts.RadiusAxisOpts(
min_=0,
max_=50,
interval=10,
splitarea_opts=opts.SplitAreaOpts(is_show=True, areastyle_opts=opts.AreaStyleOpts(opacity=1)),
),
polar_opts=opts.PolarOpts(),
splitarea_opt=opts.SplitAreaOpts(is_show=False),
splitline_opt=opts.SplitLineOpts(is_show=False),
)
.add(
series_name="药品类型",
data=data,
areastyle_opts=opts.AreaStyleOpts(opacity=0.1),
linestyle_opts=opts.LineStyleOpts(width=1),
)
.set_colors(["#4587E7"])
.set_global_opts(title_opts=opts.TitleOpts(title="中药材消费者画像数据")) # 设置图表标题
)
# 创建水平排列的 Grid,包含图表02、图表03和图表04
grid_h = (
Grid()
.add(
chart=bar,
grid_opts=opts.GridOpts(pos_left="5%", pos_right="55%", pos_top="10%", pos_bottom="60%"),
)
.add(
chart=pie,
grid_opts=opts.GridOpts(pos_left="60%", pos_right="5%", pos_top="10%", pos_bottom="60%"),
)
.add(
chart=bar_stack,
grid_opts=opts.GridOpts(pos_left="5%", pos_right="55%", pos_top="65%", pos_bottom="5%"),
)
.add(
chart=radar,
grid_opts=opts.GridOpts(pos_left="60%", pos_right="5%", pos_top="65%", pos_bottom="5%"),
)
)
# 创建垂直排列的 Grid,包含图表01和组合图表05
grid_v = (
Grid()
.add(
chart=grid_h,
grid_opts=opts.GridOpts(pos_left="5%", pos_right="5%", pos_top="5%", pos_bottom="5%"),
)
.add(
chart=bar,
grid_opts=opts.GridOpts(pos_left="5%", pos_right="5%", pos_top="65%", pos_bottom="5%"),
)
.render("grid.html")
)
结果如下: