Skip to content
Advertisement

Why error bars in log-scale matplotlib bar plot are lopsided?

I’m trying to plot some bar plots, where each y-value is averaged over some series. Consequently, I’m also trying to add the error bars (standard deviations) for each bar.

The magnitudes generally seem right, even in log scale, but for several of the bars, the error bar drops down (- direction) almost indefinitely, while the + direction error is the right magnitude. I don’t think its just the log scaling, but any input is greatly appreciated. Here is a link to the plot

Here is a link to the plot

I’ve checked and the + direction error bars are correct, just not sure why/how they are are dropping down to the x-axis occasionally. Below is a simplified example.

y = [99.79999999999997, 0.11701249999999999, 0.00011250000000000004, 0.013393750000000001,0.007743750000000001,
   0.01, 0.033906250000000006, 0.0009687500000000002, 0.04187500000000001, 0.0218, 0.0018062499999999997, 0.0005187500000000001]
std =[0.013662601021279521, 0.1500170651403811, 3.4156502553198664e-05, 0.001310709095617076,0.0006239324215543433,
   0.0, 0.0021671698133741164,0.0018750000000000001, 0.005302515126491074,0.007984401459512583,0.0006297817082132506,4.0311288741492725e-05]

plt.figure()  # Powder plot
plt.bar(np.arange(len(y)), y, yerr=std)
plt.yscale('log')

‘key_list’ is just a list of strings that will become the x-tick labels. ‘width’ is the bar offset to fit in pairs. ‘cm’ and ‘kk’ are just dictionaries of lists. This honestly seems like a rendering issue, but am mostly curious if any of you have encountered this.

Advertisement

Answer

Like mentioned in the comment, it is because your std is larger than y (for example std[1] > y[1], hence the log scale goes banana. You can fix this by introduce a small tolerance to the lower std:

tor = 1e-9
lower_std = [a - tor if a<b else b for a,b in zip(y,std)]

plt.figure()
plt.bar(np.arange(len(y)), y, yerr=(lower_std,std))
plt.yscale('log')
plt.show()

Output:

enter image description here

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement