1、安装pyecharts
pip install pyecharts
2、柱状图(个人笔记)
完成效果图:
效果图
代码:
# 柱状图
from pyecharts.charts import Bar
from pyecharts import options as opt
from pyecharts.globals import ThemeType
from example.commons 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)
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())
# 全局配置
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('chart/test.html')
方法参数:
Bar(): 图表初始化时一些参数设定,此处指定了图表主题类型
add_xaxis:添加x轴数据
add_yaxis:is_selected: 打开图表时是否默认加载
gap: 不同系列的柱间距离,为百分比
category_gap: 同一系列的柱间距离,默认为类目间距的 20%
color: 指定柱状图Label(柱子)的颜色
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:标记线相关设置
网友评论