同专栏绘制变化图文章pyecharts可视化——变化图_北归呀的博客-CSDN博客,本文绘制柱状图:
先配置基础包:同上文,本处省略
def echarts_bar(x,y,title = '主标题',subtitle = '副标题',label = '图例'):
"""
x: 函数传入x轴标签数据
y:函数传入y轴数据
title:主标题
subtitle:副标题
label:图例
"""
bar = Bar(
init_opts=opts.InitOpts(
bg_color='#080b30', # 设置背景颜色
theme='dark' # 设置主题
# width='980px', # 设置图的宽度
# height='800px' # 设置图的高度
)
)
bar.add_xaxis(x)
bar.add_yaxis(label,y,
label_opts=opts.LabelOpts(is_show=True) # 是否显示数据
,category_gap="50%" # 柱子宽度设置
)
bar.set_series_opts( # 自定义图表样式
label_opts=opts.LabelOpts(is_show=False), # 是否显示数据标签
markpoint_opts=opts.MarkPointOpts(
data=[
opts.MarkPointItem(type_="min", name="最小值"), # 显示最小值标签
opts.MarkPointItem(type_="max", name="最大值"), # 显示最大值标签
opts.MarkPointItem(type_="average", name="平均值") # 显示均值标签
]
),
itemstyle_opts={
"normal": {
"color": JsCode(
"""new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
offset: 0,color: 'rgba(0, 244, 255, 1)'}
,{offset: 1,color: 'rgba(0, 77, 167, 1)'}], false)
"""
), # 调整柱子颜色渐变
"barBorderRadius": [100, 100, 100, 100], # 调整柱子圆角弧度
"shadowColor": "rgb(0, 160, 221)", # 调整阴影颜色
}
}
)
bar.set_global_opts(
# 标题设置
title_opts=opts.TitleOpts(
title=title, # 主标题
subtitle=subtitle, # 副标题
pos_left='center', # 标题展示位置
title_textstyle_opts=dict(color='#fff') # 设置标题字体颜色
),
# 图例设置
legend_opts=opts.LegendOpts(
is_show=True, # 是否显示图例
pos_left='right', # 图例显示位置
pos_top='3%', #图例距离顶部的距离
orient='horizontal' # 图例水平布局
),
)
return bar.render_notebook()
#先计算每周的总实际支付金额,保留两位小数
week_change = df2[['周','买家实际支付金额']].groupby('周').sum().round(2).reset_index()
#tolist()将series转化为list,否则画不出图。
echarts_bar(week_change['周'].tolist(),week_change['买家实际支付金额'].tolist(),title = '订单成交金额平均每周对比',
subtitle = '每周对比图',label = '成交金额')
参考链接:工作台 - Heywhale.com