使用matplotlib绘制双柱状图
实现代码:
import matplotlib.pyplot as plt
# 定义函数来显示柱状上的数值
def autolabel(rects):
for rect in rects:
height = rect.get_height()
plt.text(rect.get_x() + rect.get_width() / 2. - 0.2, 1.03 * height, '%s' % float(height))
if __name__ == '__main__':
l1 = [75, 57, 59, 60, 60]
l2 = [68.39, 58, 54, 58, 55]
# l1 = [68.50, 53.13, 53.04, 52.64, 56.32]
# l2 = [66.31, 53.57, 50.54, 55.54, 47.72]
name = ['CNN-LSTM', 'CNN3Conv', 'CNN5Conv', 'LeNet', 'EEGNet']
total_width, n = 0.8, 2
width = total_width / n
x = [0, 1, 2, 3, 4]
plt.rc('font', family='SimHei', size=12) # 设置中文显示,否则出现乱码!
a = plt.bar(x, l1, width=width, label='Valence', fc='y')
for i in range(len(x)):
x[i] = x[i] + width
b = plt.bar(x, l2, width=width, label='Arousal', tick_label=name, fc='r')
autolabel(a)
autolabel(b)
plt.xlabel('Models')
plt.ylabel('Accuracy')
plt.title('Valence Accuracy and Arousal Accuracy')
plt.legend()
plt.show()
plt.savefig('accuracy.png')