当前位置:   article > 正文

Pyecharts html交互可视化_我用pyecharts做了很多可以交互的图,html格式的

我用pyecharts做了很多可以交互的图,html格式的

1. Package

  1. from pyecharts import options as opts
  2. from pyecharts.charts import Bar, Line,Page
  3. from pyecharts.faker import Faker

2. Bar function

  1. def bar_store() -> Bar:
  2. bar = (
  3. Bar()
  4. .add_xaxis(year_time)
  5. .add_yaxis(sample_col[0], sample_value[0],stack="stack1",category_gap="50%")
  6. .add_yaxis(sample_col[1], sample_value[1],stack="stack1",category_gap="50%")
  7. .add_yaxis(sample_col[2], sample_value[2],stack="stack1",category_gap="50%")
  8. .add_yaxis(sample_col[3], sample_value[3],stack="stack1",category_gap="50%")
  9. .add_yaxis(sample_col[4], sample_value[4],stack="stack1",category_gap="50%")
  10. .add_yaxis(sample_col[5], sample_value[5],stack="stack1",category_gap="50%")
  11. .add_yaxis(sample_col[6], sample_value[6],stack="stack1",category_gap="50%")
  12. .add_yaxis(sample_col[7], sample_value[7],stack="stack1",category_gap="50%")
  13. .add_yaxis(sample_col[8], sample_value[8],stack="stack1",category_gap="50%")
  14. .add_yaxis(sample_col[9], sample_value[9],stack="stack1",category_gap="50%")
  15. .add_yaxis(sample_col[10], sample_value[10],stack="stack1",category_gap="50%")
  16. .add_yaxis(sample_col[11], sample_value[11],stack="stack1",category_gap="50%")
  17. .add_yaxis(sample_col[12], sample_value[12],stack="stack1",category_gap="50%")
  18. .add_yaxis(sample_col[13], sample_value[13],stack="stack1",category_gap="50%")
  19. .add_yaxis(sample_col[14], sample_value[14],stack="stack1",category_gap="50%")
  20. .add_yaxis(sample_col[15], sample_value[15],stack="stack1",category_gap="50%")
  21. .add_yaxis(sample_col[16], sample_value[16],stack="stack1",category_gap="50%")
  22. .add_yaxis(sample_col[17], sample_value[17],stack="stack1",category_gap="50%")
  23. .add_yaxis(sample_col[18], sample_value[18],stack="stack1",category_gap="50%")
  24. .add_yaxis(sample_col[19], sample_value[19],stack="stack1",category_gap="50%")
  25. .add_yaxis(sample_col[20], sample_value[20],stack="stack1",category_gap="50%")
  26. .add_yaxis(sample_col[21], sample_value[21],stack="stack1",category_gap="50%")
  27. .add_yaxis(sample_col[22], sample_value[22],stack="stack1",category_gap="50%")
  28. .add_yaxis(sample_col[23], sample_value[23],stack="stack1",category_gap="50%")
  29. .add_yaxis(sample_col[24], sample_value[24],stack="stack1",category_gap="50%")
  30. .add_yaxis(sample_col[25], sample_value[25],stack="stack1",category_gap="50%")
  31. .add_yaxis(sample_col[26], sample_value[26],stack="stack1",category_gap="50%")
  32. .add_yaxis(sample_col[27], sample_value[27],stack="stack1",category_gap="50%")
  33. .add_yaxis(sample_col[28], sample_value[28],stack="stack1",category_gap="50%")
  34. .add_yaxis(sample_col[29], sample_value[29],stack="stack1",category_gap="50%")
  35. .add_yaxis(sample_col[30], sample_value[30],stack="stack1",category_gap="50%")
  36. .add_yaxis(sample_col[31], sample_value[31],stack="stack1",category_gap="50%")
  37. .add_yaxis(sample_col[32], sample_value[32],stack="stack1",category_gap="50%")
  38. .add_yaxis(sample_col[33], sample_value[33],stack="stack1",category_gap="50%")
  39. .add_yaxis(sample_col[34], sample_value[34],stack="stack1",category_gap="50%")
  40. .add_yaxis(sample_col[35], sample_value[35],stack="stack1",category_gap="50%")
  41. .add_yaxis(sample_col[36], sample_value[36],stack="stack1",category_gap="50%")
  42. .extend_axis(
  43. yaxis=opts.AxisOpts(
  44. axislabel_opts=opts.LabelOpts(formatter="{value}"), interval=50000
  45. )
  46. )
  47. .set_series_opts(label_opts=opts.LabelOpts(is_show=False))
  48. .set_global_opts(
  49. title_opts=opts.TitleOpts(title="{0}柱状图".format(title)),
  50. yaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(formatter="{value}")),
  51. legend_opts=opts.LegendOpts(type_="scroll", pos_left="90%", orient="vertical"),
  52. )
  53. )
  54. # 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))
  55. # bar.overlap(line)
  56. return bar

3. 应用

  1. # store 切片
  2. chnl_code = int(chnl_code_ls[i])
  3. title = chnl_dict[chnl_code_ls[i]]
  4. print(chnl_code,title)
  5. #sample 获取单独一家店数据,这家店每个月每个类型的销售额
  6. df_sample = df_cates_d[df_cates_d['chnl_code'] == chnl_code]
  7. df_sample = df_sample[['month', 'material_class_desc_large', 'month_cate_sales']]
  8. #pivot dataframe转向,类别为columns,month为index
  9. df_sample = df_sample.pivot(index = 'month',columns = 'material_class_desc_large',values = 'month_cate_sales').reset_index()
  10. df_sample = df_sample.sort_values(by='month')
  11. # fillna
  12. df_sample = df_sample.fillna(0)
  13. # fillcate (以防每个店类型不同)
  14. sample_col = df_sample.columns.tolist()
  15. na_cate = list(set(cates_ls).difference(set(sample_col)))
  16. for cate_name in na_cate:
  17. df_sample[cate_name] = 0
  18. print(len(df_sample.columns)== 38)
  19. # 开店月
  20. year_time = df_sample['month'].to_list()
  21. sample_value = [] # 对应值
  22. sample_col = [] #列名称
  23. for cols in cates_ls:
  24. try:
  25. temp = df_sample[cols].to_list()
  26. sample_col.append(cols)
  27. sample_value.append(temp)
  28. except:
  29. continue
  30. if len(sample_col) == 37:
  31. # bar chart
  32. bar = bar_store()
  33. bar.render('./store_cate/{0}分类型销量堆叠柱状图.html'.format(title))

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/165089
推荐阅读
相关标签
  

闽ICP备14008679号