import pyecharts.charts as pyc
import pyecharts.options as opts
import pyecharts.globals as glbs
from pyecharts.commons.utils import JsCode
from sklearn.preprocessing import MinMaxScaler
deals = pd.read_csv('deals.csv')
shops = pd.read_csv('shops.csv')
keywords = pd.read_csv('keywords.csv')
def render(chart, temp=True, show=False, changeHost=False):
'''custom render options'''
filename = chart.options['title'].opts[0]['text'] + '.html'
filename = 'unnamed.html'
js_0= 'https://cdn.jsdelivr.net/npm/echarts@latest/dist/'
js_1 = 'https://assets.pyecharts.org/assets/'
with open(filename, 'r') as f:
html = f.read().replace(js_0, js_1)
with open(filename, 'w') as f:
os.system(f'start {filename}')
counts = shops.title.value_counts()
titles = list(counts.loc[counts > 10].index)
selected = shops.loc[shops.title.map(lambda x: x in titles)]
counts = selected.title.value_counts()
init_opts=opts.InitOpts(theme=glbs.ThemeType.DARK)
'店铺数量', [int(n) for n in counts]
title_opts=opts.TitleOpts(title="各品牌店铺数量"),
xaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(rotate=22)),
datazoom_opts=opts.DataZoomOpts(range_start=0, range_end=10)
labels = list(counts.head(9).index) + ['其他']
values = [int(n) for n in counts.head(9)] + [sum(counts) - sum(counts.head(9))]
data_pair=[tpl for tpl in zip(labels, values)],
tooltip_opts=opts.TooltipOpts(is_show=True, trigger='item'),
itemstyle_opts=opts.ItemStyleOpts(border_color="#1a1c1d",opacity=0.8)
counts = shops.city.value_counts()
init_opts=opts.InitOpts(theme=glbs.ThemeType.DARK)
'店铺数量', [int(n) for n in counts]
title_opts=opts.TitleOpts(title="主要城市店铺数量"),
datazoom_opts=opts.DataZoomOpts(range_start=0, range_end=30, orient="vertical")
labels = list(counts.head(9).index) + ['其他']
values = [int(n) for n in counts.head(9)] + [sum(counts) - sum(counts.head(9))]
data_pair=[tpl for tpl in zip(labels, values)],
tooltip_opts=opts.TooltipOpts(is_show=True, trigger='item'),
itemstyle_opts=opts.ItemStyleOpts(border_color="#1a1c1d",opacity=0.8)
counts = shops.city.value_counts()
hotRate = pd.DataFrame({'city': list(counts.index), 'shopNum': list(counts)})
prices = shops.groupby('city').avgprice.mean()
hotRate = hotRate.merge(prices, on='city')
richness = shops.groupby(['city', 'title']).id.count().reset_index().city.value_counts()
richness = richness.reset_index().rename({'index': 'city', 'city': 'richness'}, axis=1)
hotRate = hotRate.merge(richness, on='city')
[hotRate.drop(['shopNum', 'avgprice', 'richness'], axis=1),
MinMaxScaler().fit_transform(hotRate.drop('city', axis=1)) * weight,
columns=['shopNum', 'avgprice', 'richness']
hotRate['rate'] = hotRate.shopNum + hotRate.avgprice + hotRate.richness
provinces = ['上海','广东','浙江','四川','河南','江苏','北京','广东','湖北','广西','山东','安徽','陕西','福建','甘肃','湖南','江西','辽宁','云南','天津','重庆','山西','内蒙古','贵州','吉林','黑龙江','宁夏','青海','海南','河北','新疆','西藏']
hotRate['province'] = provinces
capital = hotRate.drop(7)
init_opts=opts.InitOpts(theme=glbs.ThemeType.DARK, width='100%', height='360px', bg_color='#1a1c1d')
is_map_symbol_show=False,
data_pair=[tpl for tpl in zip(capital.province, [round(i, 2) for i in capital.rate])],
emphasis_itemstyle_opts=opts.ItemStyleOpts(area_color='#4992FF')
series_name='店铺数量指数(50%)',
is_map_symbol_show=False,
data_pair=[tpl for tpl in zip(capital.province, [round(i/weight[0], 2) for i in capital.shopNum])],
emphasis_itemstyle_opts=opts.ItemStyleOpts(area_color='#4992FF')
series_name='产品价格指数(30%)',
is_map_symbol_show=False,
data_pair=[tpl for tpl in zip(capital.province, [round(i/weight[1], 2) for i in capital.avgprice])],
emphasis_itemstyle_opts=opts.ItemStyleOpts(area_color='#4992FF')
series_name='品牌丰度指数(20%)',
is_map_symbol_show=False,
data_pair=[tpl for tpl in zip(capital.province, [round(i/weight[2], 2) for i in capital.richness])],
emphasis_itemstyle_opts=opts.ItemStyleOpts(area_color='#4992FF')
label_opts=opts.LabelOpts(color='#889099')
legend_opts=opts.LegendOpts(
pos_left='2.5%', pos_bottom='3%',
item_gap=6, item_width=18, item_height=10
visualmap_opts=opts.VisualMapOpts(
min_=0, max_=1, precision=2,
range_color=["#78A8F4", "#3F44A8", "#7D0083"],
range_text=["High", "Low"],
orient='horizontal', pos_left='left', pos_top='2%',
item_width=12, item_height=100
render(map, show=True, changeHost=True)
counts = shops.title.value_counts()
titles = list(counts.loc[counts > 10].index)
def get_price_seq(title, df=shops):
brand_df = df.loc[df.title==title]
filter = brand_df.avgprice.map(lambda x: x > 0)
seq = [float(p) for p in brand_df.loc[filter].avgprice]
data.append((t, seq, np.median(seq), np.mean(seq)))
data, columns=['title', 'price_seq', 'median', 'mean']
by='median', ascending=False
box = pyc.Boxplot(init_opts=opts.InitOpts(theme=glbs.ThemeType.DARK, width='100%', height='360px', bg_color='#1a1c1d')
).add_xaxis(xaxis_data=list(data.title)
y_axis=pyc.Boxplot.prepare_data(list(data.price_seq)),
itemstyle_opts=opts.ItemStyleOpts(color='#1a1c1d')
xaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(rotate=22)),
datazoom_opts=opts.DataZoomOpts(
range_start=0, range_end=30, orient="horizontal")
line = pyc.Line(init_opts=opts.InitOpts(theme=glbs.ThemeType.DARK, width='100%', height='360px', bg_color='#1a1c1d')
).add_xaxis(list(data.title)
y_axis=[int(m) for m in data['mean']],
label_opts=opts.LabelOpts(is_show=False)
keywords['words'] = keywords.words.map(lambda x: '百香果' if x=='百香' else x)
drop_list = ['中杯', '叠加', '使用', '饮品', '兄弟', '招牌', '份莓莓', '书亦烧', '系列', '建议',
'单人', '套餐', '满杯', '双人', '大叔', '小姐姐', '网红', '快乐', '经典', '不知',
'原味', '特饮', '人气', '热饮', '特色', '必点', '双重', '热销']
counts = keywords.loc[keywords.words.map(lambda x: x not in drop_list)].words.value_counts()
words = list(zip(list(counts.index), [int(n) for n in counts]))
init_opts=opts.InitOpts(theme=glbs.ThemeType.DARK, width='100%', height='360px', bg_color='#1a1c1d')
'', words, word_size_range=(8, 88), shape=glbs.SymbolType.DIAMOND
render(wc, show=True, changeHost=True)
counts = shops.title.value_counts()
titles = list(counts.loc[counts > 10].index)
selected = shops.loc[shops.title.map(lambda x: x in titles)]
sale_score = selected.dropna()[['title', 'avgscore', 'avgsales']]
sale_score = sale_score.loc[sale_score.avgsales.map(lambda x: x > 0)]
sale_score = sale_score.loc[sale_score.avgscore.map(lambda x: x > 0)]
sale_score = sale_score.groupby(by='title').mean().sort_values(by='avgscore', ascending=False).reset_index()
new echarts.graphic.RadialGradient(
[{offset: 0, color: 'rgba(111, 152, 232, 1)'},
{offset: 1, color: 'rgba(36, 54, 76, 1)'}
symbolSizeJS = 'function (data) {return 2**data[2]*2;}'
var line1 = '☕ ' + param.data[0] + '<br/>';
var line2 = '门店评分均值:' + param.data[1] + '<br/>';
var line3 = '产品销量均值:' + Math.ceil(10**param.data[2]);
return line1 + line2 + line3;
init_opts=opts.InitOpts(theme=glbs.ThemeType.DARK, width='100%', height='360px', bg_color='#1a1c1d')
[[round(sale_score.avgscore[i], 2), np.log10(sale_score.avgsales[i])] for i in sale_score.index],
label_opts=opts.LabelOpts(is_show=False),
itemstyle_opts=opts.ItemStyleOpts(color=JsCode(itemColorJS)),
symbol_size=JsCode(symbolSizeJS)
yaxis_opts=opts.AxisOpts(
name='评分均值', offset=5, type_="value", is_scale=True, min_interval=0.1,
splitline_opts=opts.SplitLineOpts(is_show=True, linestyle_opts=opts.LineStyleOpts(type_='dashed'))
legend_opts=opts.LegendOpts(is_show=False),
datazoom_opts=opts.DataZoomOpts(range_start=0, range_end=10, orient='horizontal'),
xaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(rotate=25)),
tooltip_opts=opts.TooltipOpts(formatter=JsCode(tooltipJS))
render(bubble, show=True)