首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何通过指定错误条的下限值和上限值将错误条添加到matplotlib中的条形图

如何通过指定错误条的下限值和上限值将错误条添加到matplotlib中的条形图
EN

Stack Overflow用户
提问于 2017-08-17 00:30:22
回答 1查看 3.2K关注 0票数 0

我正在尝试用python绘制一个带有最小值和最大值的条形图。我可以添加标准差条,但这不是我想要的。请看附件中的图片。我可以很容易地为散点图定义这一点,但不能为条形图定义。有人能帮我拿一下这个吗?

下面是我的代码:

代码语言:javascript
复制
# here is some example to for plotting

import matplotlib.pyplot as plt  

Label_L4L5_DP = ['Upright', 'Flexion', 'Extension', 'Bending', 'Rotation']

L4L5DP_Upr_Model  = 0.618
L4L5DP_Flex_Model = 1.137
L4L5DP_Exte_Model = 0.573
L4L5DP_Bend_Model = 1.155
L4L5DP_Rot_Model  = 1.181

L4L5DP_Wilke     = [0.5, 1.1, 0.6, 0.6, 0.62]    
L4L5DP_Sato      = [0.53912500000000008, 1.3240000000000001, 0.60024999999999995, 0, 0]    
L4L5DP_Takahashi = [0.645, 2.305, 0, 0, 0]

L4L5DP_Model     = [0.61899333208571361, 1.1375315303045039,0.57351447125529353,1.1552968123236578,1.1817117576612417]  

Std_L4L5_Sato = [0.16732411474440856, 0.2077287895309651, 0.17525320967103569, 0, 0]

# Setting the positions and width for the bars
pos = [0, 1, 2, 3, 4]
width = 0.2 

# Plotting the bars
fig, ax = plt.subplots(figsize=(8,6))

plt.bar(pos, L4L5DP_Model, width,
                 alpha=0.5,
                 color='r',
                 #yerr= 0,
                 #label=Label_L4L5_DP[3]
                 )

plt.bar([p + width for p in pos], L4L5DP_Wilke, width,
                 alpha=0.5,
                 color='g',
                 #yerr= 0,
                 #label=Label_L4L5_DP[0]
                 )

plt.bar([p + 2*width for p in pos], L4L5DP_Sato, width,
                 alpha=0.5,
                 color='b',
                    yerr= Std_L4L5_Sato,
                 #label=Label_L4L5_DP[1]
                 )

plt.bar([p + 3*width for p in pos], L4L5DP_Takahashi, width,
                 alpha=0.5,
                 color='k',
                 #yerr= 0,
                 #label=Label_L4L5_DP[2]
                 )

#    plt.errorbar(Sato_Mean_L4L5_Upr,Sato_Mean_L4L5_Upr, xerr = offsets_Sato_L4L5_Upr, fmt='b')
#    plt.errorbar([p + 3*width for p in pos], Sato_Mean_L4L5_F53, xerr = offsets_Sato_L4L5_F53, fmt='b')
#    plt.errorbar([p + 3*width for p in pos], Sato_Mean_L4L5_E30, xerr = offsets_Sato_L4L5_E30, fmt='b')

# Setting axis labels and ticks
ax.set_ylabel('Disc pressure [MPa]')
ax.set_title('L4L5')
ax.set_xticks([p + 1.5 * width for p in pos])
ax.set_xticklabels(Label_L4L5_DP)

# Setting the x-axis and y-axis limits
plt.xlim(min(pos)-width, max(pos)+width*4)
plt.ylim([0, max(L4L5DP_Wilke + L4L5DP_Sato + L4L5DP_Takahashi + L4L5DP_Model) * 1.5])

# Adding the legend and showing the plot
plt.legend(['Model', 'Wilke', 'Sato', 'Takahashi'], loc='upper left')
plt.grid()
plt.show()
EN

回答 1

Stack Overflow用户

发布于 2017-08-17 00:53:15

yerr选项可以采用包含两项的列表,其中包含向下错误和向上错误的值

代码语言:javascript
复制
Std_L4L5_Sato = [[0.1, 0.1, 0.1, 0, 0], [1, 1, 1, 0, 0]]

此示例绘制不对称的误差栏。

如果您想指定最小和最大值,您可以简单地根据中心值L4L5DP_Sato计算向上和向下误差

代码语言:javascript
复制
import numpy
L4L5DP_Sato = numpy.array(L4L5DP_Sato)
Std_L4L5_Sato = [L4L5DP_Sato - min_values, max_values - L4L5DP_Sato]
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45718750

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档