如果X和Y轴的数据都是int或float类型,图形会出错。
from pyecharts.charts.basic_charts import bar
from pyecharts.charts.basic_charts import line
from pyecharts import options as opts
from pyecharts.globals import SymbolType, ThemeType
if __name__ == '__main__':
X = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24"]
data1 = ['0', '0', '0', '0', '0', '0', '0', '3', '7', '18', '21', '20', '18', '8', '5', '5', '1','0', '0', '0', '0', '0', '0', '0' ]
data2 = ['0', '0', '0', '0', '0', '0', '0', '16', '11', '7', '13', '10', '16', '11', '7', '13', '10','0', '0', '0', '0', '0', '0', '0' ]
bar = (
bar.Bar(
init_opts=opts.InitOpts(
animation_opts=opts.AnimationOpts(
animation_delay=500, animation_easing="elasticOut" # 延时动画效果
),
theme=ThemeType.LIGHT # 主题选择 WHITE ,LIGHT...
),
)
.add_xaxis(
X,
) # X轴数据
.add_yaxis(
"A", # 系列名
data1, # 数据
# gap='0%', # 不同系列柱间距
# category_gap=0, # 相同系列柱间距 category_gap='80%' 百分数或者数值
# stack='stack1', # 相同stack名字的系列堆叠在一起
# color='#003366', # 柱子颜色
itemstyle_opts=opts.ItemStyleOpts(opacity='0.5') # 柱子的属性
)
.set_series_opts( # 图标内的属性设置
label_opts=opts.LabelOpts(is_show=False), # 是否显示具体的数据值
# markpoint_opts=opts.MarkPointOpts(
# data=[
# opts.MarkPointItem(type_='max', name='最大值'),
# opts.MarkPointItem(type_='min', name='最小值'),
# opts.MarkPointItem(type_='average', name='平均值'),
# ]
# ), # 显示出最大值,最小值,平均值,与X轴平行的刻度线
markline_opts=opts.MarkLineOpts(
data=[
opts.MarkLineItem(type_='LSL', name='LSL:4', x=4), # 显示与Y轴平行的刻度线1
opts.MarkLineItem(type_='USL', name='USL:20', x=20),
opts.MarkLineItem(type_='+3sigma', name='+3s', x=18),
opts.MarkLineItem(type_='-3sigma', name='-3s', x=6),
],
linestyle_opts=opts.LineStyleOpts(color="#364678", type_="dotted", width=2), # 设置线的属性
label_opts=opts.LabelOpts(position='end', formatter='{b}'), # 设置线上文字的属性,formatter设置label的内容
),
)
.set_global_opts( # 图表的坐标轴,title等属性设置
title_opts=opts.TitleOpts(
title="Bar-动画配置基本示例",
# subtitle="我是副标题"
),
yaxis_opts=opts.AxisOpts(
# axislabel_opts=opts.LabelOpts(formatter='{value}/个'), # 编辑Y轴的刻度线内容
name='Qty', # Y轴名字
splitline_opts=opts.SplitLineOpts(is_show=True), # 显示分割线
), # 设置Y轴属性
xaxis_opts=opts.AxisOpts(
name='Value', # X轴名字
# splitline_opts=opts.SplitLineOpts(is_show=True), # 显示分割线
), # 设置X轴属性
# brush_opts=opts.BrushOpts(), # 圈选工具
toolbox_opts=opts.ToolboxOpts(), # 显示toolbox,toolbox中有下载图片的功能
tooltip_opts=opts.TooltipOpts(formatter='Qty:{c}') # 自定义鼠标悬停内容
)
)
line = (
line.Line()
.add_xaxis(X)
.add_yaxis(
"A",
data1,
label_opts=opts.LabelOpts(is_show=False),
is_smooth=True,
linestyle_opts=opts.LineStyleOpts(width=3),
)
)
bar.overlap(line) # 将两个图表组合在一起
bar.render("MyBar.html") # 生产文件