python
import numpy as np
import matplotlib.pyplot as plt
import matplotlib
from matplotlib.ticker import MultipleLocator, AutoMinorLocator
# 设置全局字体
matplotlib.rcParams['font.family'] = 'SimHei' # 使用黑体
# 输入数据
left_eye = [3.4, 2.5, 1.2, 0.3, -4.4, -0.4, 2.1, 3.0, 0.0, -0.3]
right_eye = [-0.1, 2.4, 1.6, -1.3, 0.0, 3.2, -2.8, -4.3, 5.1, 1.9]
both_eyes = [-0.5, 1.8, -1.5, -0.2, 0.2, -2.3, 1.7, -0.7, -2.7, -0.8]
# 计算平均值和标准差
groups = [left_eye, right_eye, both_eyes]
mean_values = [np.mean(group) for group in groups]
std_devs = [np.std(group) for group in groups]
# 设置图表标签
labels = ['左眼', '右眼', '双眼']
x = np.arange(len(labels)) # 条形的x轴位置
width = 0.35 # 条形宽度
# 绘制柱状图
fig, ax = plt.subplots()
bars = ax.bar(x, mean_values, width, yerr=std_devs, label='平均偏离值', color=['#1f77b4', '#ff7f0e', '#2ca02c'])
# 标注图表细节
ax.set_ylabel('偏离距离', fontproperties='SimHei')
ax.set_title('不同视觉条件下的深度知觉偏离测试', fontproperties='SimHei')
ax.set_xticks(x)
ax.set_xticklabels(labels, fontproperties='SimHei')
ax.legend(prop={'family': 'SimHei'})
# 设定坐标轴的主要和次要刻度
ax.yaxis.set_major_locator(MultipleLocator(0.2)) # 主坐标轴刻度间隔为0.2
ax.yaxis.set_minor_locator(AutoMinorLocator(2)) # 次坐标轴刻度设置为每个主刻度之间有2个次刻度
ax.xaxis.set_major_locator(MultipleLocator(1)) # X轴的主刻度间隔
# 添加网格线(虚线)
ax.yaxis.grid(True, linestyle='--', which='major', color='grey', alpha=0.7) # 主刻度网格线
ax.xaxis.grid(True, linestyle='--', which='major', color='grey', alpha=0.7) # 主刻度网格线
# 绘制红色水平线表示0刻度
ax.axhline(0, color='red', linewidth=2, linestyle='-') # 红色的0刻度线
# 设置刻度字体大小
plt.xticks(fontsize=12)
plt.yticks(fontsize=12)
# 绘制误差条
ax.errorbar(x, mean_values, yerr=std_devs, fmt='none', ecolor='black', capsize=5)
plt.tight_layout()
plt.show()