如何在 Python Matploblib 中显示箱线图中的平均值?

matplotlibpythondata visualization更新于 2025/6/6 16:12:17

要在箱线图中显示平均值,我们可以在 boxplot() 方法的参数中使用 showmeans=True。

步骤

  • 设置图形大小并调整子图之间和周围的填充。

  • 创建一个随机数据集。

  • 使用 figure() 方法创建新图形或激活现有图形。

  • 将轴添加到当前图形作为子图排列。

  • 使用 boxplot() 方法制作箱线图。

  • 要显示图形,请使用 show()方法。

示例

import matplotlib.pyplot as plt
import numpy as np

plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
data = np.random.rand(100, 5)
fig = plt.figure()
ax = fig.add_subplot(111)
bp = ax.boxplot(data, 'd', showmeans=True)

plt.show()

输出


相关文章


有用资源