groups = data.groupby('月份') x = [each[0] for each in groups] y = [each[1].金額.sum() for each in groups] z = [each[1].金額.count() for each in groups] money_mean = data.金額.sum()/9 order_mean = data.金額.count()/9
groups_categorys= total_data.groupby('商品小類') x = [each[0] for each in groups_categorys] y = [each[1].金額.sum() for each in groups_categorys]
fig = plt.figure(figsize=(18,8),dpi=80) plt.title('各個(gè)品類的銷售額對(duì)比圖',color='#4A708B',fontsize=24) plt.ylabel('銷售額(元)',fontsize=15) colors = ['#6699cc','#4A708B','#CDCD00','#DAA520','#EE7621','#FFEC8B','#CDCD00','#4A708B','#6699cc','#DAA520','#4A708B','#FFEC8B'] for i, group_name in enumerate(groups_categorys): lin1 =plt.bar(group_name[0], group_name[1].金額.sum(),width=0.8,color=colors[i]) for rect in lin1: height = rect.get_height() plt.text(rect.get_x()+rect.get_width()/2, height+1, int(height),ha='center', fontsize=12)
plt.xticks(fontsize=15) plt.grid() plt.show()
圖表說明:面膜的銷售額第一,,其次是面霜,、爽膚水。銷售額最低的是蜜粉,,眼影,。
total_data = total_data.dropna() total_data['所在區(qū)域'] = total_data['所在區(qū)域'].apply(lambda x:str(x).replace('男區(qū)','南區(qū)').replace('西 區(qū)','西區(qū)')) groups_area= total_data.groupby(['所在區(qū)域','商品小類']) results = {} for i,j in groups_area: money = int(j.金額.sum()) if i[0] in results.keys(): results[i[0]][i[1]] = money else: results[i[0]] = {} for cate in category_names: results[i[0]][cate] = 0 results[i[0]]['口紅'] = money
results= {key_data:list(values_data.values()) for key_data,values_data in results.items()}
defsurvey1(results, category_names): labels = list(results.keys()) data = np.array(list(results.values()))
for i, (colname, color) in enumerate(zip(category_names, category_colors)): widths = data[:, i] starts = data_cum[:, i] - widths ax.barh(labels, widths, left=starts, height=0.5, label=colname, color=color) xcenters = starts + widths / 2
r, g, b, _ = color text_color = 'white'if r * g * b < 0.5else'darkgrey' for y, (x, c) in enumerate(zip(xcenters, widths)): ax.text(x, y, str(int(c)), ha='center', va='center',color=text_color) ax.legend(ncol=len(category_names), bbox_to_anchor=(0, 1), loc='lower left', fontsize='small')
import numpy as np import matplotlib.pyplot as plt category_names = list(total_data.商品小類.unique()) groups_small_category= total_data.groupby(['月份','商品小類']) results = {} for i,j in groups_small_category: money = int(j.金額.sum()) if i[0] in results.keys(): results[i[0]][i[1]] = money else: results[i[0]] = {} for cate in category_names: results[i[0]][cate] = 0 results[i[0]]['口紅'] = money
results= {key_data:list(values_data.values()) for key_data,values_data in results.items()} defsurvey(results, category_names): labels = list(results.keys()) data = np.array(list(results.values()))
for i, (colname, color) in enumerate(zip(category_names, category_colors)): widths = data[:, i] starts = data_cum[:, i] - widths ax.barh(labels, widths, left=starts, height=0.5, label=colname, color=color) xcenters = starts + widths / 2
# r, g, b, _ = color # text_color = 'white' if r * g * b < 0.5 else 'darkgrey' # for y, (x, c) in enumerate(zip(xcenters, widths)): # ax.text(x, y, str(int(c)), ha='center', va='center') ax.legend(ncol=len(category_names), bbox_to_anchor=(0, 1), loc='lower left', fontsize='small')