pyecharts是做数据分析的好帮手,柱状图比较简单,网站例子不够多,一般柱状图就是直接传两组数据就搞掂了,如果想要显示数据为百分比,比如下图例子。
需要做两处调整。
1、Y轴内容
2、标签内容
查了文档,都是修改opts.LabelOpts 。做了个例子,供大家参考。
# encoding: utf-8
"""
@author: seakingx
@contact: hndm@qq.com
@version: 1.0
@file: doex.py
@time: 2020/3/27 0019 09:39
说明 建立百分比的柱状图
"""
from pyecharts.charts import Bar
from pyecharts import options as opts
from pyecharts.globals import ThemeType
def create_bar(bar_dict):
# 建立百分比的柱状图
bar_item = bar_dict['item']
bar_head = bar_dict['head']
bar_data = bar_dict['data']
bar = (
Bar(init_opts=opts.InitOpts(theme=ThemeType.LIGHT))
.add_xaxis(bar_item)
.set_global_opts(title_opts=opts.TitleOpts(title="销售情况", subtitle="占比情况"))
)
for i in range(len(bar_head)):
bar.add_yaxis(bar_head[i], bar_data[i], label_opts=opts.LabelOpts(formatter="{c} %"))
bar.set_global_opts(
yaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(formatter="{value} %"), interval=10))
return bar
def get_data_dict():
# 这里获取要显示的数据 , 可以改成连接数据库
data_a = [round(n*100,2) for n in [0.2155, 0.423, 0.351, 0.4422, 0.651, 0.722]]
data_b = [round(n*100,2) for n in [0.1233, 0.231, 0.4522, 0.5612, 0.6667, 0.745]]
pdt_list = ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"]
data_dict= {'data':[data_a,data_b], 'head':['商家甲','商家乙'], 'item':pdt_list}
return data_dict
if __name__=="__main__":
data = get_data_dict()
bar = create_bar(data)
bar.render()
作者:陈年椰子