这段代码使用了pyecharts库生成了一个漏斗图,用于展示学习比例的情况。具体的代码解释如下:
- 从
pyecharts.options
模块中导入opts
模块,从pyecharts.charts
模块中导入Funnel
类:
import pyecharts.options as opts
from pyecharts.charts import Funnel
- 定义X轴数据和Y轴数据,分别表示不同课程和学习比例:
x_data = [
"高等数学1,2",
"C语言程序设计",
"python程序设计",
"大数据导论",
"数据结构",
"大数据与人工智能",
]
y_data = [100, 80, 60, 40, 20, 30]
- 创建一个
Funnel
实例,并使用add
方法添加数据。通过data_pair
参数设置X轴和Y轴数据,gap
参数设置每个数据块之间的距离,tooltip_opts
参数设置鼠标悬停提示框的样式,label_opts
参数设置标签的样式,itemstyle_opts
参数设置漏斗图块的边框样式:
funnel = (
Funnel()
.add(
series_name="",
data_pair=data,
gap=2,
tooltip_opts=opts.TooltipOpts(trigger="item", formatter="{a} <br/>{b} : {c}%"),
label_opts=opts.LabelOpts(is_show=True, position="inside"),
itemstyle_opts=opts.ItemStyleOpts(border_color="#fff", border_width=1),
)
)
- 使用
set_global_opts
方法设置全局选项,其中通过title_opts
参数设置图表的标题:
funnel.set_global_opts(title_opts=opts.TitleOpts(title="学习比例"))
- 最后使用
render
方法将图表渲染为HTML文件:
funnel.render("funnel_chart.html")
这段代码将生成一个漏斗图,其中X轴为不同课程的名称,Y轴为学习比例。每个数据块表示一个课程的学习比例,数据块从上到下依次减小。图表的标题为"学习比例"。鼠标悬停在每个数据块上时,会显示对应的课程名称和学习比例。最终图表将被渲染为一个HTML文件。
import pyecharts.options as opts
from pyecharts.charts import Funnel
"""
Gallery 使用 pyecharts 1.1.0
参考地址: https://echarts.apache.org/examples/editor.html?c=funnel
目前无法实现的功能:
1、暂时无法对漏斗图的长宽等范围操作进行修改
"""
x_data = [
"高等数学1,2",
"C语言程序设计",
"python程序设计",
"大数据导论",
"数据结构",
"大数据与人工智能",
]
y_data = [100, 80, 60, 40, 20,30]
data = [[x_data[i], y_data[i]] for i in range(len(x_data))]
(
Funnel()
.add(
series_name="",
data_pair=data,
gap=2,
tooltip_opts=opts.TooltipOpts(trigger="item", formatter="{a} <br/>{b} : {c}%"),
label_opts=opts.LabelOpts(is_show=True, position="inside"),
itemstyle_opts=opts.ItemStyleOpts(border_color="#fff", border_width=1),
)
.set_global_opts(title_opts=opts.TitleOpts(title="学习比例"))
.render("funnel_chart.html")
)