在 Python Matplotlib 中绘制轮廓直方图

在配置文件直方图中,每个 bin 都包含其条目的平均值。要在 Python 中绘制剖面直方图,我们可以使用Seaborn中的regplot方法。

步骤

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

  • 使用 numpy创建xy数据点。

  • 使用seaborn.regplot绘制数据和线性回归模型拟合。使用参数x_bins将 x 变量分箱到离散箱中。使用fit_reg=True绘制与xy变量相关的回归模型。

  • 要显示图形,请使用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()
输出结果

它将产生以下输出 -