Matplotlib 误差棒图

Matplotlib 误差棒图函数

很多科学实验都存在测量误差或者实验误差,这是一个无法控制的客观因素。如果想要对这一类数据进行可视化,比较好的一种方式是给实验结果添加一个误差。

在 Matplotlib 中,我们可以使用 errorbar() 函数来绘制一个误差棒图。

语法:

plt.errorbar(x, y, xerr, yerr)

说明:

参数 x 存放的是所有点的 x 轴坐标,参数 y 存放的是所有点的 y 轴坐标,它们可以是列表数组、Series 等。

xerr 和 yerr 这两个参数用于定义误差的范围,xerr 用于定义 x 轴方向的误差范围,yerr 用于定义 y 轴方向的误差范围。

示例 1:Matplotlib 绘制误差棒图

import numpy as np
import matplotlib.pyplot as plt

# 数据
x = [0.1, 0.2, 0.3, 0.4, 0.5]
y = [1.10, 1.17, 1.24, 1.31, 1.38]
# 误差
errors = 0.02 + 0.01 * np.array(x)
lower_errors = errors
upper_errors = 0.3 * errors

# 绘图
plt.errorbar(x, y, yerr=[lower_errors, upper_errors])

# 显示
plt.show()

运行之后,效果如下图所示。

Matplotlib误差棒图带y轴方向的误差

分析:

参数 yerr 用于定义 y 轴方向的误差范围,如果想要定义 x 轴方向的误差范围,我们可以使用 xerr 这个参数来实现。比如当把这个例子的 yerr 改成 xerr 之后,此时效果如下图所示。

Matplotlib误差棒图带x轴方向的误差

需要注意的是,不同实验数据的误差计算方式是不一样的,小伙伴们应该根据实际情况来计算。

Matplotlib 误差棒图样式

为了让误差棒图更加的美观,errorbar() 函数还提供了很多用于定义样式的参数,常用的如下表所示。

errorbar() 函数的样式参数
参数 说明
color 整体颜色
linestyle 线条外观
linewidth 线条宽度
marker 节点外观
markersize 节点大小
markerfacecolor 节点颜色
markeredgecolor 节点边框颜色
ecolor 棒条颜色
elinewidth 棒条粗细
capsize 横杠大小

示例 2:线条样式

import numpy as np
import matplotlib.pyplot as plt

# 数据
x = [0.1, 0.2, 0.3, 0.4, 0.5]
y = [1.10, 1.17, 1.24, 1.31, 1.38]
# 误差
errors = 0.02 + 0.01 * np.array(x)
lower_errors = errors
upper_errors = 0.3 * errors

# 绘图
plt.errorbar(
    x, y, 
    yerr=[lower_errors, upper_errors],
    color='red',
    linestyle='dashed',
    linewidth=1
)

# 显示
plt.show()

运行之后,效果如下图所示。

Matplotlib误差棒图线条样式1

分析:

color='red' 表示把线条和棒条的颜色都定义为红色,如果想要单独定义棒条颜色,我们可以使用 ecolor 这个参数来实现。当把代码修改如下时,此时效果如下图所示。

plt.errorbar(
    x, y, 
    yerr=[lower_errors, upper_errors],
    color='red',
    linestyle='dashed',
    linewidth=1,
    ecolor='blue'
)

Matplotlib误差棒图的棒条颜色

示例 3:节点样式

import numpy as np
import matplotlib.pyplot as plt

# 数据
x = [0.1, 0.2, 0.3, 0.4, 0.5]
y = [1.10, 1.17, 1.24, 1.31, 1.38]
# 误差
errors = 0.02 + 0.01 * np.array(x)
lower_errors = errors
upper_errors = 0.3 * errors

# 绘图
plt.errorbar(
    x, y, 
    yerr=[lower_errors, upper_errors],
    marker='o',
    markersize=10,
    markerfacecolor='yellow',
    markeredgecolor='red'
)

# 显示
plt.show()

运行之后,效果如下图所示。

Matplotlib误差棒图的节点样式

分析:

我们必须要先使用 marker 来定义节点的外观,然后 markersize、markerfacecolor、markeredgecolor 这 3 个参数才会生效。小伙伴们可以尝试把 marker='o' 删除,然后看看效果又是怎样的。

示例 4:棒条样式

import numpy as np
import matplotlib.pyplot as plt

# 数据
x = [0.1, 0.2, 0.3, 0.4, 0.5]
y = [1.10, 1.17, 1.24, 1.31, 1.38]
# 误差
errors = 0.02 + 0.01 * np.array(x)
lower_errors = errors
upper_errors = 0.3 * errors

# 绘图
plt.errorbar(
    x, 
    y, 
    yerr=[lower_errors, upper_errors],
    ecolor='red',
    elinewidth=3
)

# 显示
plt.show()

运行之后,效果如下图所示。

Matplotlib误差棒图的棒条样式2

分析:

ecolor='red' 表示定义棒条颜色为红色,elinewidth=3 表示定义棒条宽度为 3 像素。

示例 5:横杠样式

import numpy as np
import matplotlib.pyplot as plt

# 数据
x = [0.1, 0.2, 0.3, 0.4, 0.5]
y = [1.10, 1.17, 1.24, 1.31, 1.38]
# 误差
errors = 0.02 + 0.01 * np.array(x)
lower_errors = errors
upper_errors = 0.3 * errors

# 绘图
plt.errorbar(
    x, 
    y, 
    yerr=[lower_errors, upper_errors],
    capsize=4
)

# 显示
plt.show()

运行之后,效果如下图所示。

Matplotlib误差棒图的横杠样式

分析:

capsize=4 表示定义横杠的大小为 4 像素。所谓的横杠,指的是在棒条两端添加一个短横线。capsize 本质上是定义这个短横线的长度,比如我们将 capsize=4 修改成 capsize=10,此时效果如下图所示。

Matplotlib误差棒图capsize

示例 6:误差棒图综合实例

import numpy as np
import matplotlib.pyplot as plt

# 数据
x = [0.1, 0.2, 0.3, 0.4, 0.5]
y = [1.10, 1.17, 1.24, 1.31, 1.38]
# 误差
errors = 0.02 + 0.01 * np.array(x)
lower_errors = errors
upper_errors = 0.3 * errors

# 绘图
plt.errorbar(
    x, y, 
    yerr=[lower_errors, upper_errors],
    linestyle='dashed',
    marker='o',
    markersize=5,
    markerfacecolor='red',
    markeredgecolor='red',
    ecolor='lightseagreen', 
    elinewidth=2,
    capsize=2
)

# 显示
plt.show()

运行之后,效果如下图所示。

Matplotlib误差棒图综合案例

Matplotlib 高级绘图

之前已经学习过柱状图和误差棒图了,实际上这两种图表是可以结合一起使用的。接下来,我们来尝试绘制一个带误差棒的柱状图。

示例 7:带误差棒的柱状图

import matplotlib.pyplot as plt

# 设置
plt.rcParams['font.family'] = ['SimHei', 'PingFang SC'] 
plt.rcParams['axes.unicode_minus'] = False

# 数据
x = [1, 2, 3, 4, 5]
y = [120, 80, 90, 60, 80]
# 误差
error = [8, 5, 7, 6, 9]
# 绘图
plt.bar(
    x, y,
    yerr = error,
    error_kw = {
        'ecolor': 'orangered',
        'elinewidth': 2,
        'capsize': 4
    }
)

# 定义标题
plt.title('不同种植区的苹果收割量')
plt.xlabel('种植区')
plt.ylabel('收割量')
# 刻度标签
parks = ['园区' + str(i) for i in range(1, 6)]
plt.xticks(range(1, 6), parks)

# 显示
plt.show()

运行之后,效果如下图所示。

Matplotlib绘制带误差棒的柱状图

分析:

想要绘制一个带误差棒的柱状图,关键在于使用 bar() 函数的 yerr 和 error_kw 这 2 个参数。其中,参数 yerr 用于定义每一个柱条的误差值,它的取值是一个列表。参数 error_kw 用于定义误差棒的样式,它的取值是一个字典

给站长反馈

绿叶网正在不断完善中,小伙伴们如果发现任何问题,还望多多给站长反馈,谢谢!

邮箱:lvyenet@vip.qq.com

「绿叶网」服务号
绿叶网服务号放大
关注服务号,微信也能看教程。
绿叶网服务号