import jieba
from stylecloud import gen_stylecloud
import pandas as pd
df = pd.read_csv('F:\\python_work\\underwear_info.csv',encoding='gbk')
df.head(5)
count = df.shape[0]
comments = ""
for i in range(count):
comments = comments+"\n"+df["评论"][i]
list = jieba.cut(comments)#分词
result = " ".join(list)
gen_stylecloud(text=result, #生成图云
size=1000,
icon_name='fas fa-heartbeat',
collocations=False,
font_path='F:\\word_font\\simhei.ttf',
output_name='wordcloud.png',
custom_stopwords=['的','了','也','我','很','穿']
) # 必须加中文字体,否则格式错误
import collections
from pyecharts.charts import *
from pyecharts import options as opts
from pyecharts.commons.utils import JsCode
underwear_color = []
for i in range(count):
underwear_color.append(df["颜色"][i])
c = collections.Counter(underwear_color)
counter = c.most_common(50)
print(c)
bar = (Bar(init_opts=opts.InitOpts(theme='purple-passion', width='400px', height='300px'))
.add_xaxis([x for x, y in counter[::-1]])
.add_yaxis('出现次数', [y for x, y in counter[::-1]], category_gap='30%')
.set_global_opts(title_opts=opts.TitleOpts(title="各个颜色购买量",
pos_left="center",
title_textstyle_opts=opts.TextStyleOpts(font_size=20)),
datazoom_opts=opts.DataZoomOpts(range_start=70, range_end=100, orient='vertical'),
visualmap_opts=opts.VisualMapOpts(is_show=False, max_=250, min_=50, dimension=0,
range_color=['#f5d69f', '#f5898b', '#ef5055']),
legend_opts=opts.LegendOpts(is_show=False),
xaxis_opts=opts.AxisOpts(is_show=False,),
yaxis_opts=opts.AxisOpts(axistick_opts=opts.AxisTickOpts(is_show=False),
axisline_opts=opts.AxisLineOpts(is_show=False)))
.set_series_opts(label_opts=opts.LabelOpts(is_show=True,
position='right',
font_style='italic'),
itemstyle_opts={"normal": {
"barBorderRadius": [30, 30, 30, 30],
'shadowBlur': 10,
'shadowColor': 'rgba(120, 36, 50, 0.5)',
'shadowOffsetY': 5,
}
}
).reversal_axis())
bar.render_notebook()
Counter({' 肤色': 138, ' 黑色': 107, ' 淡紫': 90, ' 粉色': 62})
t_data = df.groupby(['下胸围', '罩杯'])['日期'].count().reset_index()
t_data.columns = ['circumference', 'cup', 'num']
#t_data.num = round(t_data.num.div(t_data.num.sum(axis=0), axis=0) * 100, 1)
print(t_data)
data_pair = [
{"name": 'A',
"label":{"show": True},
"children": []},
{"name": 'B',
"label":{"show": True},
"children": []},
{"name": 'C',
"label":{"show": True},
'shadowBlur': 10,
'shadowColor': 'rgba(120, 36, 50, 0.5)',
'shadowOffsetY': 5,
"children": []},
{"name": 'D',
"label":{"show": False},
"children": []},
{"name": 'E',
"label":{"show": False},
"children": []}
]
for idx, row in t_data.iterrows():
t_dict = {"name": row.cup,
"label":{"show": True},
"children": []}
if row.num > 0:
child_data = {"name": '{}-{}'.format(row.circumference, row.cup), "value":row.num, "label":{"show": True}}
else:
child_data = {"name": '{}-{}'.format(row.circumference, row.cup), "value":row.num, "label":{"show": False}}
if row.cup == "A":
data_pair[0]['children'].append(child_data)
elif row.cup == "B":
data_pair[1]['children'].append(child_data)
elif row.cup == "C":
data_pair[2]['children'].append(child_data)
elif row.cup == "D":
data_pair[3]['children'].append(child_data)
elif row.cup == "E":
data_pair[4]['children'].append(child_data)
circumference cup num 0 70 A 33 1 70 B 40 2 70 C 4 3 75 A 84 4 75 B 115 5 75 C 18 6 80 A 29 7 80 B 36 8 80 C 10 9 85 A 5 10 85 B 18 11 85 C 5
c = (Sunburst(
init_opts=opts.InitOpts(
theme='purple-passion',
width="600px",
height="600px"))
.add(
"",
data_pair=data_pair,
highlight_policy="ancestor",
radius=[0, "100%"],
sort_='null',
levels=[
{},
{
"r0": "20%",
"r": "48%",
"itemStyle": {"borderColor": 'rgb(220,220,220)', "borderWidth": 2}
},
{"r0": "50%", "r": "80%", "label": {"align": "right"},
"itemStyle": {"borderColor": 'rgb(220,220,220)', "borderWidth": 1}}
],
)
.set_global_opts(
visualmap_opts=opts.VisualMapOpts(is_show=False, max_=200, min_=50,
range_color=['#f5d69f', '#f5898b', '#ef5055']),
title_opts=opts.TitleOpts(title="文 胸\n\n尺 码 分 布",
pos_left="center",
pos_top="center",
title_textstyle_opts=opts.TextStyleOpts(font_style='oblique', font_size=30),))
.set_series_opts(label_opts=opts.LabelOpts(font_size=18, formatter="{b}: {c}"))
)
c.render_notebook()
grid = Grid(init_opts=opts.InitOpts(theme='purple-passion', width='800px', height='800px'))
for idx, c in enumerate(['70', '75', '80', '85']):
if idx % 2 == 0:
x = 30
y = int(idx/2) * 30 + 20
else:
x = 70
y = int(idx/2) * 30 + 20
pos_x = str(x)+'%'
pos_y = str(y)+'%'
pie = Pie(init_opts=opts.InitOpts())
pie.add(
c,
[[row.cup, row.num]for i, row in t_data[t_data.circumference==int(c)].iterrows()],
center=[pos_x, pos_y],
radius=[70, 100],
label_opts=opts.LabelOpts(formatter='{b}:{d}%'),
)
pie.set_global_opts(
title_opts=opts.TitleOpts(title="下胸围={}".format(c),
pos_top=str(y-1)+'%', pos_left=str(x-4)+'%',
title_textstyle_opts=opts.TextStyleOpts(font_size=15)),
legend_opts=opts.LegendOpts(is_show=True))
grid.add(pie,grid_opts=opts.GridOpts(pos_left='20%'))
grid.render_notebook()