赞
踩
- from pyecharts import options as opts
- from pyecharts.charts import Bar, Line,Page
- from pyecharts.faker import Faker
- def bar_store() -> Bar:
-
- bar = (
- Bar()
- .add_xaxis(year_time)
- .add_yaxis(sample_col[0], sample_value[0],stack="stack1",category_gap="50%")
- .add_yaxis(sample_col[1], sample_value[1],stack="stack1",category_gap="50%")
- .add_yaxis(sample_col[2], sample_value[2],stack="stack1",category_gap="50%")
- .add_yaxis(sample_col[3], sample_value[3],stack="stack1",category_gap="50%")
- .add_yaxis(sample_col[4], sample_value[4],stack="stack1",category_gap="50%")
- .add_yaxis(sample_col[5], sample_value[5],stack="stack1",category_gap="50%")
- .add_yaxis(sample_col[6], sample_value[6],stack="stack1",category_gap="50%")
- .add_yaxis(sample_col[7], sample_value[7],stack="stack1",category_gap="50%")
- .add_yaxis(sample_col[8], sample_value[8],stack="stack1",category_gap="50%")
- .add_yaxis(sample_col[9], sample_value[9],stack="stack1",category_gap="50%")
- .add_yaxis(sample_col[10], sample_value[10],stack="stack1",category_gap="50%")
- .add_yaxis(sample_col[11], sample_value[11],stack="stack1",category_gap="50%")
- .add_yaxis(sample_col[12], sample_value[12],stack="stack1",category_gap="50%")
- .add_yaxis(sample_col[13], sample_value[13],stack="stack1",category_gap="50%")
- .add_yaxis(sample_col[14], sample_value[14],stack="stack1",category_gap="50%")
- .add_yaxis(sample_col[15], sample_value[15],stack="stack1",category_gap="50%")
- .add_yaxis(sample_col[16], sample_value[16],stack="stack1",category_gap="50%")
- .add_yaxis(sample_col[17], sample_value[17],stack="stack1",category_gap="50%")
- .add_yaxis(sample_col[18], sample_value[18],stack="stack1",category_gap="50%")
- .add_yaxis(sample_col[19], sample_value[19],stack="stack1",category_gap="50%")
- .add_yaxis(sample_col[20], sample_value[20],stack="stack1",category_gap="50%")
- .add_yaxis(sample_col[21], sample_value[21],stack="stack1",category_gap="50%")
- .add_yaxis(sample_col[22], sample_value[22],stack="stack1",category_gap="50%")
- .add_yaxis(sample_col[23], sample_value[23],stack="stack1",category_gap="50%")
- .add_yaxis(sample_col[24], sample_value[24],stack="stack1",category_gap="50%")
- .add_yaxis(sample_col[25], sample_value[25],stack="stack1",category_gap="50%")
- .add_yaxis(sample_col[26], sample_value[26],stack="stack1",category_gap="50%")
- .add_yaxis(sample_col[27], sample_value[27],stack="stack1",category_gap="50%")
- .add_yaxis(sample_col[28], sample_value[28],stack="stack1",category_gap="50%")
- .add_yaxis(sample_col[29], sample_value[29],stack="stack1",category_gap="50%")
- .add_yaxis(sample_col[30], sample_value[30],stack="stack1",category_gap="50%")
- .add_yaxis(sample_col[31], sample_value[31],stack="stack1",category_gap="50%")
- .add_yaxis(sample_col[32], sample_value[32],stack="stack1",category_gap="50%")
- .add_yaxis(sample_col[33], sample_value[33],stack="stack1",category_gap="50%")
- .add_yaxis(sample_col[34], sample_value[34],stack="stack1",category_gap="50%")
- .add_yaxis(sample_col[35], sample_value[35],stack="stack1",category_gap="50%")
- .add_yaxis(sample_col[36], sample_value[36],stack="stack1",category_gap="50%")
- .extend_axis(
- yaxis=opts.AxisOpts(
- axislabel_opts=opts.LabelOpts(formatter="{value}"), interval=50000
- )
- )
- .set_series_opts(label_opts=opts.LabelOpts(is_show=False))
- .set_global_opts(
- title_opts=opts.TitleOpts(title="{0}柱状图".format(title)),
- yaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(formatter="{value}")),
- legend_opts=opts.LegendOpts(type_="scroll", pos_left="90%", orient="vertical"),
-
- )
- )
-
- # line = Line().add_xaxis(week3_data['weeks'].to_list()).add_yaxis("消费金额",v3, yaxis_index=1).set_series_opts(label_opts=opts.LabelOpts(is_show=False))
- # bar.overlap(line)
- return bar
- # store 切片
- chnl_code = int(chnl_code_ls[i])
- title = chnl_dict[chnl_code_ls[i]]
- print(chnl_code,title)
- #sample 获取单独一家店数据,这家店每个月每个类型的销售额
- df_sample = df_cates_d[df_cates_d['chnl_code'] == chnl_code]
- df_sample = df_sample[['month', 'material_class_desc_large', 'month_cate_sales']]
- #pivot dataframe转向,类别为columns,month为index
- df_sample = df_sample.pivot(index = 'month',columns = 'material_class_desc_large',values = 'month_cate_sales').reset_index()
- df_sample = df_sample.sort_values(by='month')
- # fillna
- df_sample = df_sample.fillna(0)
- # fillcate (以防每个店类型不同)
- sample_col = df_sample.columns.tolist()
- na_cate = list(set(cates_ls).difference(set(sample_col)))
- for cate_name in na_cate:
- df_sample[cate_name] = 0
- print(len(df_sample.columns)== 38)
-
- # 开店月
- year_time = df_sample['month'].to_list()
- sample_value = [] # 对应值
- sample_col = [] #列名称
- for cols in cates_ls:
- try:
- temp = df_sample[cols].to_list()
- sample_col.append(cols)
- sample_value.append(temp)
- except:
- continue
- if len(sample_col) == 37:
- # bar chart
- bar = bar_store()
- bar.render('./store_cate/{0}分类型销量堆叠柱状图.html'.format(title))
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。