Home > Net >  How can I add error bars with min and max value to grouped barplot?
How can I add error bars with min and max value to grouped barplot?

Time:12-10

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. 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).]enter image description here

CodePudding user response:

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

y_errormin = data_AGB - np.array([3.7e-7, 8.0e-7])
y_errormax = np.array([3.7e-5, 1.1e-5]) - data_AGB
  • Related