[英]How can I add error bars with min and max value to grouped barplot?
How can I add an error bar with a specific minimum and maximum value on the orange bars only.如何仅在橙色条上添加具有特定最小值和最大值的误差条。
This is my code now这是我的代码
import matplotlib.pyplot as plt
import numpy as np
# Enter my SiC2 data
sic2_AGB = np.array([3.7e-7, 1.1e-5, 1.9e-6, 1.0e-5, 9.7e-6, 4.0e-6,
6.8e-6, 6.6e-6, 1.0e-6, 1.3e-5, 6.0e-6, 3.7e-5, 1.7e-6, 4.2e-6,
2.7e-6, 1.4e-5, 1.0e-5, 8.8e-7, 3.6e-6, 2.0e-5, 1.6e-6, 6.0e-7,
2.7e-5, 3.0e-6, 4.0e-6])
sic2_g0693 = 7.9e-11
# Enter my SiS data
sis_AGB = np.array([1.3e-6, 4.8e-6, 3.8e-6, 1.9e-6, 7.2e-6, 8.0e-7,
7.0e-6, 4.0e-6, 6.1e-7, 1.0e-5, 1.0e-5, 4.1e-6,
9.6e-7, 1.1e-5, 1.1e-6, 4.9e-6, 4.6e-6, 9.8e-7,
3.3e-6, 2.3e-6, 2.8e-6, 1.7e-6, 1.1e-5, 4.6e-6,
2.2e-6])
sis_g0693 = 3.9e-10
# Calculate the average of AGB data
sic2_AGB_mean = np.mean(sic2_AGB)
sis_AGB_mean = np.mean(sis_AGB)
labels = ['SiC$_{2}$', 'SiS']
data_g0693 = [sic2_g0693, sis_g0693]
data_AGB =[sic2_AGB_mean, sis_AGB_mean]
x = np.arange(len(labels)) # the label locations
width = 0.15 # width of the bar
fig, ax = plt.subplots()
ax.bar(x - width/2, data_g0693, width, label='G+0.693')
ax.bar(x + width/2, data_AGB, width, label='AGB')
# Labels and axes
plt.xticks(x, labels)
plt.yscale('log', nonposy='clip')
plt.ylabel('$\chi$', fontsize=15)
plt.tick_params(axis='x', labelrotation=0, labelsize= 10)
plt.tick_params(axis="y",direction="in")
ax.legend()
plt.tight_layout()
plt.savefig('toto.jpg',bbox_inches='tight', dpi=150)
plt.show()
I want error bars with limits of [3.7 e-7, 3.7 e-5] on the first orange bar, and a [8.0 e-7, 1.1 e-5] on the second orange bar.我想要在第一个橙色条上限制为 [3.7 e-7, 3.7 e-5] 的误差条,在第二个橙色条上限制为 [8.0 e-7, 1.1 e-5]。 I attach a photo of what I want it to look like (see below).
我附上了一张我想要它看起来像的照片(见下文)。
I tried:我试过:
y_errormin = [3.7e-7, 8.0e-7]
y_errormax = [3.7e-5, 1.1e-5]
y_error = [y_errormin, y_errormax]
and modified:并修改:
ax.bar(x + width/2, data_AGB, width, label='AGB', yerr=y_error)
But the limits look wrong.但是限制看起来不对。 Can you please help me with it?
你能帮我吗?
[.[Here's a photo of what I would like it to look like (-ish).] [.[这是我希望它看起来像 (-ish) 的照片。]
Well, yerr
is the distance you should add or subtract from the bar's value, so you should have y_errormax = y_max - y_mean
and y_errormin = y_mean - y_min
好吧,
yerr
是您应该从条形值中添加或减去的距离,因此您应该有y_errormax = y_max - y_mean
和y_errormin = y_mean - y_min
y_errormin = data_AGB - np.array([3.7e-7, 8.0e-7])
y_errormax = np.array([3.7e-5, 1.1e-5]) - data_AGB
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.