简体   繁体   English

为什么对数刻度 matplotlib 条形 plot 中的误差线是不平衡的?

[英]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.我正在尝试 plot 一些条形图,其中每个 y 值都是某些系列的平均值。 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这是 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.我已经检查过,+ 方向误差线是正确的,只是不确定它们为什么/如何偶尔下降到 x 轴。 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. 'key_list' 只是将成为 x-tick 标签的字符串列表。 'width' is the bar offset to fit in pairs. 'width' 是成对的条形偏移量。 'cm' and 'kk' are just dictionaries of lists. 'cm' 和 'kk' 只是列表的字典。 This honestly seems like a rendering issue, but am mostly curious if any of you have encountered this.老实说,这似乎是一个渲染问题,但如果你们中的任何人遇到过这个问题,我会非常好奇。

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 : 如注释中所述,这是因为您的std大于y (例如std[1] > y[1] ,因此对log刻度变为香蕉。您可以通过对较低的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: 输出:

在此处输入图片说明

You should look at the relative error rather than trying to plot the standard deviation, or any other measure of variability.您应该查看相对误差,而不是尝试 plot 标准偏差或任何其他可变性度量。

To illustrate this with an example:用一个例子来说明这一点:

In your linear space, you will have x +/- delta_x to display.在您的线性空间中,您将显示 x +/- delta_x。

Projected into your logarithmic space, this becomes: log(x) +/- log(delta_x).投影到你的对数空间中,它变成:log(x) +/- log(delta_x)。 But remember that log(x) - log(y) = x/y.但请记住 log(x) - log(y) = x/y。 Hence, your non-symmetric error bar, for example.因此,例如,您的非对称误差线。 If you learn more about relative error, you will find an appropriate symmetric error bar.如果您了解有关相对误差的更多信息,您会发现一个合适的对称误差条。

Enjoy your learning:)享受你的学习:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM