Python中的 Matplotlib.pyplot.errorbar()
Matplotlib是Python中的一个库,它是 NumPy 库的数值数学扩展。 Pyplot是Matplotlib模块的基于状态的接口,它提供了一个类似 MATLAB 的接口。
matplotlib.pyplot.errorbar()函数:
matplotlib 库的 pyplot 模块中的errorbar()函数用于将 y 与 x 绘制为线和/或带有附加误差线的标记。
Syntax: matplotlib.pyplot.errorbar(x, y, yerr=None, xerr=None, fmt=”, ecolor=None, elinewidth=None, capsize=None, barsabove=False, lolims=False, uplims=False, xlolims=False, xuplims=False, errorevery=1, capthick=None, \*, data=None, \*\*kwargs)
Parameters: This method accept the following parameters that are described below:
- x, y: These parameter are the horizontal and vertical coordinates of the data points.
- fmt: This parameter is an optional parameter and it contains the string value.
- xerr, yerr: These parameter contains an array.And the error array should have positive values.
- ecolor: This parameter is an optional parameter. And it is the color of the errorbar lines with default value NONE.
- elinewidth: This parameter is also an optional parameter. And it is the linewidth of the errorbar lines with default value NONE.
- capsize: This parameter is also an optional parameter. And it is the length of the error bar caps in points with default value NONE.
- barsabove: This parameter is also an optional parameter. It contains boolean value True for plotting errorsbars above the plot symbols.Its default value is False.
- lolims, uplims, xlolims, xuplims: These parameter are also an optional parameter. They contain boolean values which is used to indicate that a value gives only upper/lower limits.
- errorevery: This parameter is also an optional parameter. They contain integer values which is used to draws error bars on a subset of the data.
Returns: This returns the container and it is comprises of the following:
- plotline:This returns the Line2D instance of x, y plot markers and/or line.
- caplines:This returns the tuple of Line2D instances of the error bar caps.
- barlinecols:This returns the tuple of LineCollection with the horizontal and vertical error ranges.
下面的示例说明了 matplotlib.pyplot 中的 matplotlib.pyplot.errorbar()函数:
示例 #1:
# Implementation of matplotlib function
import numpy as np
import matplotlib.pyplot as plt
# example data
xval = np.arange(0.1, 4, 0.5)
yval = np.exp(-xval)
plt.errorbar(xval, yval, xerr = 0.4, yerr = 0.5)
plt.title('matplotlib.pyplot.errorbar() function Example')
plt.show()
输出:
示例 #2:
# Implementation of matplotlib function
import numpy as np
import matplotlib.pyplot as plt
fig = plt.figure()
x = np.arange(10)
y = 3 * np.sin(x / 20 * np.pi)
yerr = np.linspace(0.05, 0.2, 10)
plt.errorbar(x, y + 7, yerr = yerr,
label ='Line1')
plt.errorbar(x, y + 5, yerr = yerr,
uplims = True,
label ='Line2')
plt.errorbar(x, y + 3, yerr = yerr,
uplims = True,
lolims = True,
label ='Line3')
upperlimits = [True, False] * 5
lowerlimits = [False, True] * 5
plt.errorbar(x, y, yerr = yerr,
uplims = upperlimits,
lolims = lowerlimits,
label ='Line4')
plt.legend(loc ='upper left')
plt.title('matplotlib.pyplot.errorbar()\
function Example')
plt.show()
输出: