numpy 如何在误差条形图中画一条连接条形中心的线?

sxpgvts3  于 2023-08-05  发布在  其他
关注(0)|答案(1)|浏览(65)

我试图插入一条线连接误差条形图中条形图上部的中心,如图中红色所示:


的数据
这是我现在的代码:

# Enter raw data
aluminum = np.array([6.4e-5 , 3.01e-5 , 2.36e-5, 3.0e-5, 7.0e-5, 4.5e-5, 3.8e-5, 4.2e-5, 2.62e-5, 3.6e-5])
copper = np.array([4.5e-5 , 1.97e-5 , 1.6e-5, 1.97e-5, 4.0e-5, 2.4e-5, 1.9e-5, 2.41e-5, 1.85e-5, 3.3e-5])
steel = np.array([3.3e-5 , 1.2e-5 , 0.9e-5, 1.2e-5, 1.3e-5, 1.6e-5, 1.4e-5, 1.58e-5, 1.32e-5 , 2.1e-5])

# Calculate the average
aluminum_mean = np.mean(aluminum)
copper_mean = np.mean(copper)
steel_mean = np.mean(steel)

# Calculate the standard deviation
aluminum_std = np.std(aluminum)
copper_std = np.std(copper)
steel_std = np.std(steel)

# Create lists for the plot
materials = ['Aluminum', 'Copper', 'Steel'] 
x_pos = np.arange(len(materials))
CTEs = [aluminum_mean, copper_mean, steel_mean]
error = [aluminum_std, copper_std, steel_std]

# Build the plot
fig, ax = plt.subplots()
ax.bar(x_pos, CTEs, yerr = error, align = 'center', alpha = 0.5, ecolor = 'black', capsize=10)
ax.set_ylabel('Coefficient of Thermal Expansion ($\degree C^{-1}$)') 
ax.set_xticks(x_pos)
ax.set_xticklabels(materials)
ax.set_title('Coefficient of Thermal Expansion (CTE) of Three Metals')  
ax.yaxis.grid(True)
plt.show()

字符串

zzwlnbp8

zzwlnbp81#

您可以在ax.bar(x_pos,...)之后添加一行Axes.plot,其余代码保持不变:

ax.bar(x_pos, CTEs, yerr = error, align = 'center', alpha = 0.5, ecolor = 'black', capsize=10)
ax.plot(x_pos, CTEs, color='red')

字符串
测试结果:


的数据

相关问题