Python导入matplotlib库

问题遇到的现象和发生背景
Python导入matplotlib库使用pyplot做柱状图时,提示TypeError
问题相关代码,请勿粘贴截图


import pandas as pd
import matplotlib.pyplot as plt

plt.rcParams['font.sans-serif']=['SimHei']
df = pd.read_excel('F:/heart/ex.xls', sheet_name=0, header=0)

x = df['专业名称']
y1 = df['最高分']
y2 = df['最低分']
y3 = df['平均分']
width = 0.25
#y轴标签
plt.ylabel('分数(分)')
#图标标题
plt.title('上海大学各专业高考分数', fontsize='18')
plt.bar(x, y1, width=width, color='r')
plt.bar(x+width, y2, width=width, color='b')
plt.bar(x+2*width, y3, width=width, color='g')
#设置每个柱子的文本标签
for a, b in zip(x, y1):
    plt.text(a, b+3, '%.1f'%b,  ha='center', va='bottom', fontsize=8)
for a, b in zip(x, y2):
    plt.text(a, b+3, '%.1f'%b,  ha='center', va='bottom', fontsize=8)
for a, b in zip(x, y3):
    plt.text(a, b+3, '%.1f'%b,  ha='center', va='bottom', fontsize=8)

plt.legend(['最高分', '最低分', '平均分'])
plt.show()


运行结果及报错内容
TypeError: can only concatenate str (not "float") to str

我的解答思路和尝试过的方法
将plt.bar()中x加的width去掉,但出现了柱子叠加。

我想要达到的结果
怎么才能把柱子分开

试试这样:

 
import pandas as pd
import matplotlib.pyplot as plt
 
plt.rcParams['font.sans-serif']=['SimHei']
df = pd.read_excel('F:/heart/ex.xls', sheet_name=0, header=0)
 
x = df['专业名称']
y1 = df['最高分']
y2 = df['最低分']
y3 = df['平均分']
x_w = np.arange(3)
width = 0.25
#y轴标签
plt.ylabel('分数(分)')
#图标标题
plt.title('上海大学各专业高考分数', fontsize='18')
plt.bar(x_w, y1, width=width, color='r')
plt.bar(x_w+width, y2, width=width, color='b')
plt.bar(x_w+2*width, y3, width=width, color='g')
#设置每个柱子的文本标签
for a, b in zip(x, y1):
    plt.text(a, b+3, '%.1f'%b,  ha='center', va='bottom', fontsize=8)
for a, b in zip(x, y2):
    plt.text(a, b+3, '%.1f'%b,  ha='center', va='bottom', fontsize=8)
for a, b in zip(x, y3):
    plt.text(a, b+3, '%.1f'%b,  ha='center', va='bottom', fontsize=8)
 
plt.legend(['最高分', '最低分', '平均分'])
plt.show()