Matplotlib 非对称 yerr 方式偏离
Matplotlib asymmetric yerr way off
据我所知,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()
结果:
正如@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.
您可以通过取 y
和 err
之间的绝对差来获得您想要的效果:
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()
误差条是相对于数据的,此外,+/- 值都是正值(因此是绝对误差):
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()
据我所知,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()
结果:
正如@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.
您可以通过取 y
和 err
之间的绝对差来获得您想要的效果:
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()
误差条是相对于数据的,此外,+/- 值都是正值(因此是绝对误差):
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()