更改 Matplotlib 中的误差条粗细
Matplotlib 是一个被数据分析广泛使用的Python库。 Matplotlib.pyplot.errorbar() 是一个 pyplot 模块,由一个提供类似 MATLAB 接口的函数组成。
更改误差条粗细
在更改误差线的粗细之前,让我们看看误差线是什么以及我们如何绘制和使用它们。
误差条:误差条是我们在数据中包含的条,用于传达报告测量中的不可预测性。通俗地说,它显示数据可变性的图形表示,并在图表上用于显示误差测量。
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)
示例 1:
Python3
# importing necessary libraries
import matplotlib.pyplot as plt
# example data
x = np.arange(3, 5, 0.5)
y = np.arange(9, 11, 0.5)
# ploting with default thickness of bar
plt.title("1. Without changing thickness")
plt.errorbar(x, y, xerr=0.2, yerr=0.6, fmt='o')
plt.show()
# ploting with changed thickness of bar
plt.title("1. With changed thickness of bar")
# change elinwidth to change the thickness of bar
plt.errorbar(x, y, xerr=0.2, yerr=0.6, fmt='o', elinewidth=4)
plt.show()
Python3
# importing necessary libraries
import matplotlib.pyplot as plt
# example data
x = np.arange(1, 3, 0.5)
y = np.log(x)
# ploting with default thickness of bar
plt.title("2. Without changing thickness")
plt.errorbar(x, y, xerr=0.2, yerr=0.4, fmt='o', ecolor='black', capsize=5)
plt.show()
# ploting with changed thickness of bar
# change elinwidth to change the thickness of bar
plt.title("2. With changed thickness of bar")
plt.errorbar(x, y, xerr=0.2, yerr=0.4, fmt='o', elinewidth=4,
ecolor='black', capsize=5, capthick=3)
plt.show()
输出 :

图 1.1

图 1.2
正如我们可以看到两个图像的差异。 elinewidth 参数取整数值,然后增加误差条的粗细。
示例 2:
蟒蛇3
# importing necessary libraries
import matplotlib.pyplot as plt
# example data
x = np.arange(1, 3, 0.5)
y = np.log(x)
# ploting with default thickness of bar
plt.title("2. Without changing thickness")
plt.errorbar(x, y, xerr=0.2, yerr=0.4, fmt='o', ecolor='black', capsize=5)
plt.show()
# ploting with changed thickness of bar
# change elinwidth to change the thickness of bar
plt.title("2. With changed thickness of bar")
plt.errorbar(x, y, xerr=0.2, yerr=0.4, fmt='o', elinewidth=4,
ecolor='black', capsize=5, capthick=3)
plt.show()
输出 :

图 2.1

图 2.2
在上面的示例中,我们已将倾覆作为上限,其中包含有关不确定性有多大的完整信息。如果条形较小,则降低标准差并降低数据分布,这意味着数据集中在均值附近,如果条形较大,则标准差越大,数据分布越大。