1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
| import matplotlib.pyplot as plt import pandas as pd import numpy as np
plt.rcParams['font.sans-serif'] = ['SimHei'] plt.rcParams['axes.unicode_minus'] = False pd.set_option('display.unicode.ambiguous_as_wide',True) pd.set_option('display.unicode.east_asian_width',True)
data = pd.DataFrame({ "name":['百香果','猕猴桃','草莓','桃子','苹果', '梨', '葡萄', '车厘子', '榴莲','蓝莓','橙子','橘子'], "f": [1,5,6,3,11, 13, 25, 47, 27,37,68,89], }) d = data.groupby(by=['name'],as_index=False)['f'].sum() d['name2'] = d['name'].map(lambda x: '\n'.join(x))
d1 = pd.Series(np.array(d['f']),index = np.array(d['name2'])) d1.sort_values(ascending=False, inplace= True)
c = pd.Series(np.array(d['f']),index = np.array(d['name'])) c.sort_values(ascending=False, inplace= True)
ax1 = plt.subplot(1, 1, 1) ax1.bar(d1.index, d1, color='#527C5A',width=0.9), ax1.spines['bottom'].set_linewidth(1);
cindex = np.arange(len(d1)) for a,b,y in zip(cindex,d1,c): plt.tick_params(labelsize=12) plt.text(a,b,'%.0f'%(y),ha='center',va='bottom',fontsize=12);
biaoti ='统计' plt.title(biaoti,fontsize=15)
p = d1.cumsum()/d1.sum() key = p[p>0.8].index[0] key_num = d1.index.tolist().index(key)
cp = c.cumsum()/c.sum() ckey = cp[cp>0.8].index[0] ckey_num = c.index.tolist().index(ckey)
print('超过80%累计占比的节点值索引为:' ,ckey) print('超过80%累计占比的节点值索引位置为:' ,ckey_num) print('------')
ckey_product = c.loc[:ckey] print('核心产品为:') print(ckey_product)
p.plot(style = '--ko', secondary_y=True) ax1.axvline(key_num,color='r',linestyle="--",alpha=0.8) plt.text(key_num+0.2,p[key],'累计占比为:%.2f%%' % (p[key]*100), color = 'r') plt.ylabel('累\n计\n比\n例',rotation=0)
|