1
2
3
4
5
import seaborn as sns
import matplotlib.pyplot as plt

import numpy as np
import pandas as pd

1. sns.regplot

  • 适合展示简单的两组关系
1
2
3
4
5
6
mpg = sns.load_dataset('mpg')

# 1) 一般用法
sns.regplot(data=mpg, x="weight", y="acceleration")
# sns.regplot(data=mpg, x="weight", y="acceleration", ci=None) # 不显示置信区间
# sns.regplot(data=mpg, x="weight", y="acceleration", ci=99)   # 自定义置信区间
image-20250804214851272
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
# 2) 修改scatter点与line线的显示细节

sns.regplot(
    data=mpg, x="weight", y="acceleration",
    scatter_kws={
        'color': 'blue',   # 散点颜色
        's': 50,           # 点的大小(面积)
        'alpha': 0.6,      # 透明度
        'marker': 'o',     # 点的样式,比如 'o', 'x', '^', etc.
        'linewidths': 0      # 点的轮廓线
    }
)

sns.regplot(
    data=mpg, x="weight", y="acceleration",
    line_kws={
        'color': 'red',     # 回归线颜色
        'linewidth': 2,     # 线宽
        'linestyle': '--',  # 线型:'-'、'--'、':'、'-.' 等
        'alpha': 0.9        # 回归线透明度
    }
)
image-20250804215207233
1
2
3
4
5
6
7
# 3) 对具有"离散"性质的变量(数据类型仍是float/int), 为避免重叠可以设置抖动
sns.regplot(data=mpg, x="cylinders", y="weight", x_jitter=.15) 

# 4) 线的拟合方式
sns.regplot(data=mpg, x="weight", y="mpg", order=2)  # 拟合高阶函数
# sns.regplot(data=mpg, x="horsepower", y="weight", robust=True)  # 不受离群点影响
# sns.regplot(data=mpg, x="horsepower", y="mpg", lowess=True)     # 拟合平滑曲线
image-20250804215520321

2. sns.lmplot

  • 适用于分组线性回归, 即分面绘图
1
2
3
tips = sns.load_dataset("tips")

sns.lmplot(x="total_bill", y="tip", hue="sex", data=tips)
image-20250804215915652
1
2
sns.lmplot(x="total_bill", y="tip", col="day", data=tips,
           facet_kws=dict(sharex=False, sharey=False),)

image-20250804220011872

3. 注释相关性

  • 直接使用sns.regplot,再标注
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
from scipy.stats import pearsonr, spearmanr

mpg = sns.load_dataset("mpg").dropna(subset=["weight", "acceleration"])
x = mpg["weight"]
y = mpg["acceleration"]
r, p = pearsonr(x, y)
r, p

sns.regplot(data=mpg, x="weight", y="acceleration")

plt.text(x.max()*0.8, y.max()*0.9, f"Pearson r = {r:.2f}\np = {p:.2e}", fontsize=12)
plt.show()
  • 先使用sns.scatterplot,再手动添加拟合线,再标注
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
# 数据准备
mpg = sns.load_dataset("mpg").dropna(subset=["weight", "acceleration"])
x = mpg["weight"]
y = mpg["acceleration"]

# 绘制散点
sns.scatterplot(x=x, y=y, color="gray", edgecolor="black", s=60)

# 添加回归线(用 numpy.polyfit)
slope, intercept = np.polyfit(x, y, deg=1)
x_vals = np.linspace(x.min(), x.max(), 100)
y_vals = slope * x_vals + intercept
plt.plot(x_vals, y_vals, color="red", linestyle="--", label="Linear fit")

# 添加相关性注释
r, p = pearsonr(x, y)
plt.text(x.max()*0.8, y.max()*0.9, f"Pearson r = {r:.2f}\np = {p:.2e}", fontsize=12)

plt.legend()
plt.show()

image-20250804220238823