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 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
| data_merge['差值'] = data_merge['A'] - data_merge['B'] data_merge['百分比'] = (data_merge['A'] - data_merge['B'])/data_merge['B'] data_merge[['A', 'B', '差值']] = data_merge[['A', 'B', '差值']].astype('int') data_merge['百分比画'] = data_merge['百分比'].map(lambda x: '{:.0%}'.format(x)) data_merge = data_merge.sort_values(by='百分比', ascending=False) data_merge = data_merge[data_merge['B']!=0] data_merge = data_merge.reset_index(drop=True)
x_data = list(data_merge['C']) y_data = list(data_merge['百分比'])
yz = data_merge[data_merge['百分比']>0] yz["百分比画"]= '▲' + yz["百分比画"] x1 = list(yz['C']) y_data1 = list(yz['百分比']) y1 = list(yz['百分比画']) index1 = np.arange(len(y1))
yf = data_merge[data_merge['百分比']<0] yf["百分比画"]= '▼' + yf["百分比画"] x2 = list(yf['C']) y_data2 = list(yf['百分比']) y2 = list(yf['百分比画']) index2 = np.arange(len(y2))
y_ = data_merge[data_merge['百分比']==0] y_["百分比画"]= '-' + y_["百分比画"] x3 = list(y_['C']) y_data3 = list(y_['百分比']) y3 = list(y_['百分比画']) index3 = np.arange(len(y3))
plt.figure(figsize=(10,5),dpi=300) plt.ylim(data_merge['百分比'].min(),data_merge['百分比'].max()+0.05) plt.yticks([]) plt.xticks([]) plt.title('A与B',fontsize=25)
ax1 = plt.subplot(1, 1, 1) ax1.spines['right'].set_visible(False) ax1.spines['top'].set_visible(False) ax1.spines['left'].set_visible(False) ax1.spines['bottom'].set_visible(False) ax=plt.gca(); ax.spines['bottom'].set_linewidth(1); plt.tick_params(labelsize=8)
plt.bar(x_data, y_data, color=np.where(data_merge['百分比']>=0,'#e28b90','#98b9e5'),width=0.8)
for a,b,c in zip(index1,y_data1,y1): plt.text(a*1.,b+0.001,c,ha='center',va='bottom',fontsize=8,color ='#c02c38'); for a,b,c in zip(index1,y_data1,x1): plt.text(a*1.,b+0.02,c,ha='center',va='bottom',fontsize=8,color ='#c02c38');
for a,b,c in zip(index2,y_data2,x2): plt.text(a+len(index1)+len(index3),b-0.01,c,ha='center',va='bottom',fontsize=8); for a,b,c in zip(index2,y_data2,y2): plt.text(a+len(index1)+len(index3),b-0.025,c,ha='center',va='bottom',fontsize=8);
for a,b,c in zip(index3,y_data3,x3): plt.text(a+len(index1),b+0.02,c,ha='center',va='bottom',fontsize=8); for a,b,c in zip(index3,y_data3,y3): plt.text(a+len(index1),b,c,ha='center',va='bottom',fontsize=8);
columns = x_data rows = ['A', 'B', "差距","百分比"] d = np.array([data_merge['A'],data_merge['B'],data_merge['差值'],data_merge['百分比画']]) table = plt.table(cellText=d, rowLabels=rows, colLabels=columns, cellLoc='center', loc="bottom", bbox=[0.04, -0.35, 0.92, 0.24])
table.auto_set_font_size(False) table.set_fontsize(9)
|