条形图,当差值大于0时设置一个颜色,反之另一种颜色,并添加表格
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.ticker as mtick
import matplotlib.ticker as ticker#设置输出结果对齐方式
pd.set_option('display.unicode.ambiguous_as_wide',True)
pd.set_option('display.unicode.east_asian_width',True)# 解决plt中文显示问题
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = Falseimport warnings
warnings.filterwarnings("ignore")
数据
data_merge=pd.DataFrame({'A':[87,79,80,92],'B':[93,89,67,77],'C':['张三','李四','王五','赵六']})
print(data_merge)
A B C
0 87 93 张三
1 79 89 李四
2 80 67 王五
3 92 77 赵六
代码
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([]) # 去y坐标刻度
plt.xticks([]) # 去x坐标刻度
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) #x轴标签大小#判断大于0的为#e28b90,负的为#98b9e5
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)