最近想画误差图(也有人叫误差条、误差棒……等等),就像下图这种,点代表平均值,上下长条线的范围代表标准差。
查到python的matplotlib.pyplot.errorbar函数可以画这种图,但是该函数里有各种参数,简单上网查了一下,发现别人用该函数时只是简单的用了几个参数,而至于各参数怎么用也不说清楚,本以为强大的CSDN会有该函数的详细讲解,但搜了一下没搜到……就去乌班图系统里查了一下。
打开终端,输入“python”进入python
可以用“help”获取帮助信息。先导入matplotlib.pyplot,输入“import matplotlib.pyplot”,之后再输入“help(matplotlib.pyplot)”,弹出matplotlib.pyplot的各种帮助信息,里面有该包所包含的各种函数的信息。找到errorbar()函数的讲解部分。
我懒得翻译,整体复制粘贴上来,大家自己找着看吧。
先上函数完整版:
errorbar(x, y, yerr=None, xerr=None, fmt=u'', ecolor=None, elinewidth=None, capsize=None, barsabove=False, lolims=False, uplims=False, xlolims=Fals
e, xuplims=False, errorevery=1, capthick=None, hold=None, data=None, **kwargs)
x, y, xerr与yerr可以都是标量,这样会在(x,y)位置画出一个单个的点与误差条
参数
------------------------------------
x,y : 标量或数组矩阵形式
xerr/yerr : 标量或数组矩阵形式,若为数组或矩阵形式时,可以是1×N维(1行N列)矩阵或2×N维(2行N列)矩阵。如果是一个标量或1×N维矩阵,则误差条的每个点的上下误差会取矩阵里对应位置的正负值。可选。默认是0。
若果是2×N维矩阵,则矩阵第一行的值为误差的负值,第二行的值为误差的正值。
fmt :画图格式,决定了图的格式符号,可选,默认是:None 。
如果fmt是‘none’(不区分大小写),那么整个图将仅有误差条被画出。
该参数的可选属性与函数plot()的相同·。
ecolor : 误差条的颜色,可选,默认值是:None
elinewidth : 标量,可选,默认值:None
误差条的线的宽度。
capsize : 标量,可选,默认值:None
每点的误差条的上下横线的长度
capthick : 标量,可选,默认值:None
每点的误差条的上下横线的厚度
barsabove : bool(布尔)类型,可选, 默认值: False
如果是“True”,将把误差条画在代表每个点的符号上面,默认是在下面
lolims / uplims / xlolims / xuplims : bool(布尔)类型,可选, 默认值: None
这些参数被用来表明图像仅提供上限或者下限
errorevery : 正整数,可选,默认值:1
如果errorevery=5,则误差条每5个点画一次
其他参数
---------------------------------------------------
kwargs : 其他用来描述点符号的参数
举例:下面代码画出带厚的绿色边缘的大的红色正方形符号:
x,y,yerr = rand(3,10)
errorbar(x, y, yerr, marker='s', mfc='red',mec='green', ms=20, mew=4)
mfc, mec, ms与mew都是属性名称的缩写,分别代表markerfacecolor(符号颜色), markeredgecolor(符号边缘颜色), markersize(符号大小)与markeredgewidth(符号边缘厚度).
包含的有效的参数有(该部分参数太多,并且介绍很少,就不翻译了,直接放原文):
agg_filter: unknown
alpha: float (0.0 transparent through 1.0 opaque)
animated: [True | False]
antialiased or aa: [True | False]
axes: an :class:`~matplotlib.axes.Axes` instance
clip_box: a :class:`matplotlib.transforms.Bbox` instance
clip_on: [True | False]
clip_path: [ (:class:`~matplotlib.path.Path`, :class:`~matplotlib.transforms.Transform`) | :class:`~matplotlib.patches.Patch` | None ]
color or c: any matplotlib color
contains: a callable function
dash_capstyle: ['butt' | 'round' | 'projecting']
dash_joinstyle: ['miter' | 'round' | 'bevel']
dashes: sequence of on/off ink in points
drawstyle: ['default' | 'steps' | 'steps-pre' | 'steps-mid' | 'steps-post']
figure: a :class:`matplotlib.figure.Figure` instance
fillstyle: ['full' | 'left' | 'right' | 'bottom' | 'top' | 'none']
gid: an id string
label: string or anything printable with '%s' conversion.
linestyle or ls: ['solid' | 'dashed', 'dashdot', 'dotted' | (offset, on-off-dash-seq) | ``'-'`` | ``'--'`` | ``'-.'`` | ``':'`` | ``'None'`` | ``' '`` | ``''``]
linewidth or lw: float value in points
marker: :mod:`A valid marker style
markeredgecolor or mec: any matplotlib color
markeredgewidth or mew: float value in points
markerfacecolor or mfc: any matplotlib color
markerfacecoloralt or mfcalt: any matplotlib color
markersize or ms: float
markevery: [None | int | length-2 tuple of int | slice | list/array of int | float | length-2 tuple of float]
path_effects: unknown
picker: float distance in points or callable pick function ``fn(artist, event)``
pickradius: float distance in points
rasterized: [True | False | None]
sketch_params: unknown
snap: unknown
solid_capstyle: ['butt' | 'round' | 'projecting']
solid_joinstyle: ['miter' | 'round' | 'bevel']
transform: a :class:`matplotlib.transforms.Transform` instance
url: a url string
visible: [True | False]
xdata: 1D array
ydata: 1D array
zorder: any number
“参数”部分的原文
-----------------------------------------------
errorbar(x, y, yerr=None, xerr=None, fmt=u'', ecolor=None, elinewidth=None, capsize=None, barsabove=False, lolims=False, uplims=False, xlolims=Fals
e, xuplims=False, errorevery=1, capthick=None, hold=None, data=None, **kwargs)
Plot an errorbar graph.
Plot x versus y with error deltas in yerr and xerr.
Vertical errorbars are plotted if yerr is not None.
Horizontal errorbars are plotted if xerr is not None.
x, y, xerr, and yerr can all be scalars, which plots a
single error bar at x, y.
Parameters
----------
x : scalar or array-like
y : scalar or array-like
xerr/yerr : scalar or array-like, shape(N,) or shape(2,N), optional
If a scalar number, len(N) array-like object, or a N-element
array-like object, errorbars are drawn at +/-value relative
to the data. Default is None.
If a sequence of shape 2xN, errorbars are drawn at -row1
and +row2 relative to the data.
fmt : plot format string, optional, default: None
The plot format symbol. If fmt is 'none' (case-insensitive),
only the errorbars are plotted. This is used for adding
errorbars to a bar plot, for example. Default is '',
an empty plot format string; properties are
then identical to the defaults for :meth:`plot`.
ecolor : mpl color, optional, default: None
A matplotlib color arg which gives the color the errorbar lines;
if None, use the color of the line connecting the markers.
elinewidth : scalar, optional, default: None
The linewidth of the errorbar lines. If None, use the linewidth.
capsize : scalar, optional, default: None
The length of the error bar caps in points; if None, it will
take the value from ``errorbar.capsize``
:data:`rcParam
capthick : scalar, optional, default: None
An alias kwarg to markeredgewidth (a.k.a. - mew). This
setting is a more sensible name for the property that
controls the thickness of the error bar cap in points. For
backwards compatibility, if mew or markeredgewidth are given,
then they will over-ride capthick. This may change in future
releases.
barsabove : bool, optional, default: False
if True , will plot the errorbars above the plot
symbols. Default is below.
lolims / uplims / xlolims / xuplims : bool, optional, default:None
These arguments can be used to indicate that a value gives
only upper/lower limits. In that case a caret symbol is
used to indicate this. lims-arguments may be of the same
type as *xerr* and *yerr*. To use limits with inverted
axes, :meth:`set_xlim` or :meth:`set_ylim` must be called
before :meth:`errorbar`.
errorevery : positive integer, optional, default:1
subsamples the errorbars. e.g., if errorevery=5, errorbars for
every 5-th datapoint will be plotted. The data plot itself still
shows all data points.
Returns
-------
plotline : :class:`~matplotlib.lines.Line2D` instance
x, y plot markers and/or line
caplines : list of :class:`~matplotlib.lines.Line2D` instances
error bar cap
barlinecols : list of :class:`~matplotlib.collections.LineCollection`
horizontal and vertical error ranges.