Matplotlib非对称耶尔路

2024-09-27 00:15:49 发布

您现在位置:Python中文网/ 问答频道 /正文

据我所知,matplotlib只是在为我的误差条绘制错误的值。我已经尽我所能简化了我的代码,到了硬编码的值和事情仍然是错误的。。。在下面的示例中,我通过scatter绘制了完全相同的值,它们出现在我希望它们出现的地方-但是误差线离我们很远。我误解什么了吗?在

最小示例:

from matplotlib.pyplot import *

x = [1, 2, 3, 4, 5]
y = [5, 11, 22, 44, 88]
err = [[4.3, 10.1, 19.8, 40, 81.6],
       [5.9, 13.6, 24.6, 48.5, 100.2]]

figure();
errorbar(x, y, yerr=err, label="data")
scatter(x, err[0], c='r', label="lower limit")
scatter(x, err[1], c='g', label="upper limit")

legend()
show()

结果:Result of the code sample above


Tags: 代码from示例编码matplotlib地方错误绘制
2条回答

正如@Bart在评论中指出的,matplotlib将yerr解释为相对于直线y坐标的一组+/-偏移量。来自the documentation

xerr/yerr: [ scalar | N, Nx1, or 2xN array-like ]

If a scalar number, len(N) array-like object, or an Nx1 array-like object, errorbars are drawn at +/- value relative to the data.

If a sequence of shape 2xN, errorbars are drawn at -row1 and +row2 relative to the data.

您可以通过取yerr之间的绝对差异来获得所需的效果:

err = np.array(err)
y = np.array(y)
offsets = np.abs(err - y[None, :])

figure();
errorbar(x, y, yerr=offsets, label="data")
scatter(x, err[0], c='r', label="lower limit")
scatter(x, err[1], c='g', label="upper limit")
legend()
show()

enter image description here

误差线是相对于数据的,而且,两个+/-值都是正值(因此绝对误差):

from matplotlib.pyplot import *
import numpy as np

x = np.array([1, 2, 3, 4, 5])
y = np.array([5, 11, 22, 44, 88])
err = np.array([[4.3, 10.1, 19.8, 40,   81.6 ],
                [5.9, 13.6, 24.6, 48.5, 100.2]])

err2 = np.zeros_like(err)
err2[0,:] = y - err[0,:]
err2[1,:] = err[1,:] - y

figure();
errorbar(x, y, yerr=err2, label="data")
scatter(x, err[0], c='r', label="lower limit")
scatter(x, err[1], c='g', label="upper limit")

legend()
show()

enter image description here

相关问题 更多 >

    热门问题