import matplotlib.pyplot as plt
%matplotlib inline
plt.rcParams['font.sans-serif']=['SimHei']
plt.rcParams['axes.unicode_minus']=False
x=[1,2,3,4]
classes=['一班','二班','三班','四班']
ch=[70,80,85,77]
english=[80,85,88,82]
math=[90,96,88,87]
plt.bar(x,chinese,label='Chinese',color='orange')#设置图表颜色
plt.bar(x,english,bottom=chinese,color='#43cd80',label='English')
cheng=[chinese[i]+english[i] for i in range(len(x))]
plt.bar(x,english,bottom=cheng,color='#804dff',label='Math')
plt.xlabel('班级') #横坐标
plt.ylabel('成绩') #纵坐标
plt.ylim([0,300])
plt.xlim([0,6])
plt.title("学生成绩条形图") #标题
plt.xticks(x,classes) #xticks() 对应坐标名称
#plt.text(1,81,70) #添加文字text(横坐标,纵坐标,文字)
for i in range(0,len(x)):
plt.text(i+1,ch[i]-10,ch[i],color='white')
plt.text(i+1,ch[i]+english[i]-10,english[i],color='white')
plt.text(i+1,ch[i]+english[i]+math[i]-20,math[i],color='white')
plt.text(i+1,ch[i]+english[i]+math[i]+5,ch[i]+english[i]+math[i],color='black')
plt.legend(loc='upper right')#图例
plt.grid(axis='y',color='gray',linestyle='--',linewidth=1)
plt.show()