在Python Matplotlib中绘制概要直方图
在概要直方图中,每个箱包含其条目的平均值。为了在Python中绘制概要直方图,我们可以使用 Seaborn 中的 regplot 方法。
步骤
- 设置图像大小并调整子图之间和周围的填充。
-
使用numpy创建 x 和 y 数据点。
-
使用 seaborn.regplot 绘制数据和线性回归模型拟合。使用参数 x_bins 将x变量分成离散的箱。使用 fit_reg=True 来绘制 x 和 y 变量之间的回归模型。
-
使用 Show() 方法显示图形。
示例
import numpy as np
import seaborn as sns
from matplotlib import pyplot as plt
plt.rcParams["figure.figsize"] = [7.00, 3.50]
plt.rcParams["figure.autolayout"] = True
x = np.random.uniform(-5, 5, 1000)
y = np.random.normal(x**2, np.abs(x) + 1)
sns.regplot(x=x, y=y, x_bins=20, marker='o', fit_reg=True)
plt.show()
输出
它将产生以下输出−