开始代码部分
一次性导入所需要的全部第三方库
import pandas as pd import pyecharts.options as opts from pyecharts.charts import * from pyecharts.globals import ThemeType#设定主题 from pyecharts.commons.utils import JsCode
1. 读取数据,而这些数据,一般都是我们爬取到的商品数据,或者公司内的数据库里面的数据
df1 = pd.read_csv(r'京东-乐高.csv', engine='python', encoding='utf-8-sig') df2 = pd.read_csv(r'6K高达.csv', engine='python', encoding='utf-8-sig') df3 = pd.read_csv(r'6K奥特曼.csv', engine='python', encoding='utf-8-sig')
查看下数据
df1.head(1)
2. 数据处理
把表格统计到一起
df_all = pd.concat([df1,df2,df3])
df_all.info()
除去重复值
df_all.drop_duplicates(inplace=True)
删除不必要的列
df_all = df_all.drop(['商品SKU','商品链接','封面图链接','评论链接','店铺链接','页码','当前时间','页面网址'],axis=1) df_all.head(1)
筛选剔除广告
df_all = df_all[df_all['是否广告'] == '否']
重置索引
df_all = df_all.reset_index(drop=True)
df_all.info()
3. 处理完数据以后,我们就可以做可视化图表了
绘制商家上线的商品数目Top20柱状图
bar1 = ( Bar(init_opts=opts.InitOpts(theme='dark', width='1000px',height ='500px')) .add_xaxis(shopname.index.tolist()) .add_yaxis("",shopname.values.tolist()) .set_series_opts( label_opts=opts.LabelOpts( is_show=True, position='insideRight', font_style='italic' ), itemstyle_opts=opts.ItemStyleOpts( color=JsCode( """new echarts.graphic.LinearGradient(1, 0, 0, 0, [{offset: 0,color: 'rgb(255,99,71)'}, {offset: 1,color: 'rgb(32,178,170)'}])""" ) ) ) .set_global_opts( title_opts=opts.TitleOpts(title="商家上线的商品数目Top20"), xaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(rotate=45)), legend_opts=opts.LegendOpts(is_show=True)) .reversal_axis() ) bar1.render_notebook()
总体价格区间
pie1 = ( Pie(init_opts=opts.InitOpts(theme='dark',width='1000px',height='600px')) .add('', datas_pair, radius=['35%', '60%']) .set_global_opts( title_opts=opts.TitleOpts(title='不同价格区间的销售额整体表现'), legend_opts=opts.LegendOpts(orient='vertical', pos_top='15%', pos_left='2%') ) .set_series_opts(label_opts=opts.LabelOpts(formatter="{b}:{d}%")) .set_global_opts( title_opts=opts.TitleOpts( title="乐高、奥特曼、高达\n\n价格区间", pos_left='center', pos_top='center', title_textstyle_opts=opts.TextStyleOpts( color='#F0F8FF', font_size=20, font_weight='bold' ), ) ) .set_colors(['#EF9050', '#3B7BA9', '#6FB27C', '#FFAF34', '#D8BFD8', '#00BFFF', '#7FFFAA']) ) pie1.render_notebook()
单价最高的商品Top20
bar=( Bar(init_opts=opts.InitOpts(height='500px',width='1000px',theme='dark')) .add_xaxis(price_top.index.tolist()) .add_yaxis( '单价最高的商品', price_top.values.tolist(), label_opts=opts.LabelOpts(is_show=True,position='top'), itemstyle_opts=opts.ItemStyleOpts( color=JsCode("""new echarts.graphic.LinearGradient( 0, 0, 0, 1,[{offset: 0,color: 'rgb(255,99,71)'}, {offset: 1,color: 'rgb(32,178,170)'}]) """ ) ) ) .set_global_opts( title_opts=opts.TitleOpts( title='单价最高的商品详细柱状图'), xaxis_opts=opts.AxisOpts(name='玩具名称', type_='category', axislabel_opts=opts.LabelOpts(rotate=90), ), yaxis_opts=opts.AxisOpts( name='单价/元', min_=0, max_=39980.0, splitline_opts=opts.SplitLineOpts(is_show=True,linestyle_opts=opts.LineStyleOpts(type_='dash')) ), tooltip_opts=opts.TooltipOpts(trigger='axis',axis_pointer_type='cross') ) .set_series_opts( markline_opts=opts.MarkLineOpts( data=[ opts.MarkLineItem(type_='average',name='均值'), opts.MarkLineItem(type_='max',name='最大值'), opts.MarkLineItem(type_='min',name='最小值'), ] ) ) ) bar.render_notebook()
出处:https://www.cnblogs.com/qshhl/p/15125964.html