美文网首页
Python数据可视化-误差棒图errorbar

Python数据可视化-误差棒图errorbar

作者: 5cb608806559 | 来源:发表于2020-10-15 00:49 被阅读0次

实验中往往由于各种原因会存在一定的误差,针对这一波动范围我们称之为置信区间。在可视化数据时,Matplotlib中的误差棒图(errorbar,官方项目地址)可以很好的表现这种有一定置信区间的带误差数据。

matplotlib.pyplot.errorbar(x, y, yerr=None, xerr=None, fmt='', ecolor=None, elinewidth=None, capsize=None, capthick=None)

主要参数说明:
x,y: 数据点的位置坐标
xerr,yerr: 数据的误差范围
fmt: 数据点的标记样式以及相互之间连接线样式
ecolor: 误差棒的线条颜色
elinewidth: 误差棒的线条粗细
capsize: 误差棒边界横杠的大小
capthick: 误差棒边界横杠的厚度
ms: 数据点的大小
mfc: 数据点的颜色
mec: 数据点边缘的颜色

示例:

  • Y轴误差设置
import numpy as np
import matplotlib.pyplot as plt


fig = plt.figure()
x = np.arange(10)
y = 2.5 * np.sin(x / 20 * np.pi)
yerr = np.linspace(0.05, 0.2, 10)

plt.errorbar(x, y + 3, yerr=yerr, label='both limits (default)')

plt.errorbar(x, y + 2, yerr=yerr, uplims=True, label='uplims=True')

plt.errorbar(x, y + 1, yerr=yerr, uplims=True, lolims=True,
             label='uplims=True, lolims=True')

upperlimits = [True, False] * 5
lowerlimits = [False, True] * 5
plt.errorbar(x, y, yerr=yerr, uplims=upperlimits, lolims=lowerlimits,
             label='subsets of uplims and lolims')

plt.legend(loc='lower right')

运行结果:


image.png
  • X轴误差设置
fig = plt.figure()
x = np.arange(10) / 10
y = (x + 0.1)**2

plt.errorbar(x, y, xerr=0.1, xlolims=True, label='xlolims=True')
y = (x + 0.1)**3

plt.errorbar(x + 0.6, y, xerr=0.1, xuplims=upperlimits, xlolims=lowerlimits,
             label='subsets of xuplims and xlolims')

y = (x + 0.1)**4
plt.errorbar(x + 1.2, y, xerr=0.1, xuplims=True, label='xuplims=True')

plt.legend()
plt.show()

运行结果:


image.png

相关文章

网友评论

      本文标题:Python数据可视化-误差棒图errorbar

      本文链接:https://www.haomeiwen.com/subject/mtzgpktx.html