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('各个品类的销售额对比图',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['所在区域'] = total_data['所在区域'].apply(lambda x:str(x).replace('男区','南区').replace('西 区','西区')) groups_area= total_data.groupby(['所在区域','商品小类']) 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')