from pyecharts.charts import Bar
from pyecharts import options as opt
from pyecharts.globals import ThemeType
from pyecharts.faker import Faker as fa
import random
# 生成随机数据
attr = fa.days_attrs
v1 = [random.randrange(10, 150) for _ in range(31)]
v2 = [random.randrange(10, 150) for _ in range(31)]
# 初始化一个Bar对象,并设定一写初始化设置
bar = Bar(init_opts=opt.InitOpts(theme=ThemeType.WHITE))
# 添加数据
bar.add_xaxis(attr)
# is_selected: 打开图表时是否默认加载 grap:不同系列的柱间距离,百分比; color:指定柱状图Label的颜色
bar.add_yaxis("test1", v1, gap="0", category_gap="20%", color=fa.rand_color())
bar.add_yaxis("test2", v2, is_selected=False, gap="0%", category_gap="20%", color=fa.rand_color())
# 全局配置
# title_opts:图标标题相关设置
# toolbox_opts: 工具栏相关设置
# yaxis_opts/xaxis_opts: 坐标轴相关设置
# axislabel_opts: 坐标轴签字相关设置
# axisline_opts: 坐标轴轴线相关设置
# datazoom_opts: 坐标轴轴线相关设置
# markpoint_opts: 标记点相关设置
# markpoint_opts:label_opts=opts.LabelOpts(is_show=False) 标签值是否叠加
# markline_opts:标记线相关设置
bar.set_global_opts(title_opts=opt.TitleOpts(title="主标题", subtitle="副标题"),
toolbox_opts=opt.ToolboxOpts(),
yaxis_opts=opt.AxisOpts(axislabel_opts=opt.LabelOpts(formatter="{value}/月"), name="这是y轴"),
xaxis_opts=opt.AxisOpts(
axisline_opts=opt.AxisLineOpts(linestyle_opts=opt.LineStyleOpts(color='blue')), name="这是x轴"),
datazoom_opts=opt.DataZoomOpts()
)
bar.set_series_opts(markpoint_opts=opt.MarkPointOpts(data=[opt.MarkPointItem(type_="max", name="最大值"),
opt.MarkPointItem(type_="min", name="最小值"),
opt.MarkPointItem(type_="average", name="平均值")]),
markline_opts=opt.MarkLineOpts(data=[opt.MarkLineItem(type_="min", name="最小值"),
opt.MarkLineItem(type_="max", name="最大值"),
opt.MarkLineItem(type_="average", name="平均值")]))
# 指定生成html文件路径
bar.render('test.html')
os.system("test.html")
效果如下: