26 成对网格 PairGrid

26 成对网格 PairGrid

这类图是什么

成对矩阵的自定义版,上三角/下三角/对角线可挂不同函数。

看什么:同一矩阵里用不同图形表达不同信息,如上方散点下方密度。

5 个案例

26-1 自定义成对矩阵:上三角散点、下三角密度、对角线直方

自定义成对矩阵:上三角散点、下三角密度、对角线直方

26-2 按区域分色的成对矩阵:同一网格内比较两类

按区域分色的成对矩阵:同一网格内比较两类

26-3 下三角回归、上三角散点的成对网格

下三角回归、上三角散点的成对网格

26-4 全用六边形分箱的成对矩阵:大样本场景

全用六边形分箱的成对矩阵:大样本场景

26-5 按季节分色的密度成对矩阵

按季节分色的密度成对矩阵

代码

5 个函数取自 code/第五批_组合网格261107.py。关键约束:不调用 sns.set_theme(),否则 font.family 被改成 Arial,中文会变方框;版式由 code/公共配置261107.py 里的 setup_cn_style() 与 save() / save_grid() 统一接管。

# 摘自 code/第五批_组合网格261107.py

def pg_01():
    """自定义成对矩阵:上三角散点、下三角密度、对角线直方。"""
    rng = np.random.default_rng(2601)
    b = rng.uniform(0.2, 1.1, 300)
    df = pd.DataFrame({"负载率": b,
                       "效率": 98.4 - 1.5 * (1 - b) ** 1.4 + rng.normal(0, 0.08, 300),
                       "温升": 28 + 26 * b ** 2 + rng.normal(0, 2, 300)})
    g = sns.PairGrid(df, height=1.75)
    g.map_upper(sns.scatterplot, s=10, color=C1, edgecolor="white", linewidth=0.2)
    g.map_lower(sns.kdeplot, fill=True, cmap="Blues", levels=6)
    g.map_diag(sns.histplot, color=C5, edgecolor="0.4", linewidth=0.3, bins=15)
    g.figure.set_size_inches(4.4, 4.2)
    save_grid(g, "26_成对网格", "26_成对网格_上下三角分工", multi=True,
              w_mm=112, h_mm=106,
              adjust=dict(left=0.135, bottom=0.11, right=0.95, top=0.94))

def pg_02():
    """按区域分色的成对矩阵:同一网格内比较两类。"""
    rng = np.random.default_rng(2602)
    rows = []
    for area, k in [("城区", 1.0), ("农村", 0.86)]:
        p = rng.uniform(200, 850, 150) * k
        rows.append(pd.DataFrame({"有功": p, "电流": p / 1.72 + rng.normal(0, 26, 150),
                                  "电压": 10.12 - 0.0009 * p + rng.normal(0, 0.07, 150),
                                  "区域": area}))
    df = pd.concat(rows, ignore_index=True)
    g = sns.PairGrid(df, hue="区域", palette=[C5, C3], height=1.75)
    g.map_offdiag(sns.scatterplot, s=9, edgecolor="white", linewidth=0.2)
    g.map_diag(sns.kdeplot, fill=True, alpha=0.45)
    g.add_legend(title="区域", framealpha=0.6)
    g.figure.set_size_inches(4.4, 4.2)
    save_grid(g, "26_成对网格", "26_成对网格_按区域分色", multi=True,
              w_mm=112, h_mm=106,
              adjust=dict(left=0.135, bottom=0.11, right=0.95, top=0.94))

def pg_03():
    """下三角回归、上三角散点的成对网格。"""
    rng = np.random.default_rng(2603)
    v = rng.weibull(2.1, 260) * 9.5
    df = pd.DataFrame({"风速": v,
                       "出力": np.clip(0.42 * v ** 3, 0, 1650) + rng.normal(0, 85, 260),
                       "桨距角": np.clip((v - 11) * 1.6, 0, None) + rng.normal(0, 1, 260)})
    g = sns.PairGrid(df, height=1.75)
    g.map_upper(sns.scatterplot, s=9, color=C3, edgecolor="white", linewidth=0.2)
    g.map_lower(sns.regplot, color=C1,
                scatter_kws={"s": 8, "edgecolor": "white", "linewidths": 0.2},
                line_kws={"lw": 0.9})
    g.map_diag(sns.histplot, color=C5, edgecolor="0.4", linewidth=0.3, bins=14)
    g.figure.set_size_inches(4.4, 4.2)
    save_grid(g, "26_成对网格", "26_成对网格_回归与散点分工", multi=True,
              w_mm=112, h_mm=106,
              adjust=dict(left=0.135, bottom=0.11, right=0.95, top=0.94))

def pg_04():
    """全用六边形分箱的成对矩阵:大样本场景。"""
    rng = np.random.default_rng(2604)
    i = rng.gamma(9, 32, 4000)
    df = pd.DataFrame({"电流": i,
                       "电压": 10.18 - 0.00095 * i + rng.normal(0, 0.05, 4000),
                       "温度": 26 + 0.03 * i + rng.normal(0, 3, 4000)})
    g = sns.PairGrid(df, height=1.75)
    g.map_offdiag(sns.histplot, bins=18, cmap="Blues", cbar=False)
    g.map_diag(sns.histplot, color=C5, edgecolor="0.4", linewidth=0.3, bins=18)
    g.figure.set_size_inches(4.4, 4.2)
    save_grid(g, "26_成对网格", "26_成对网格_大样本分箱", multi=True,
              w_mm=112, h_mm=106,
              adjust=dict(left=0.135, bottom=0.11, right=0.95, top=0.94))

def pg_05():
    """按季节分色的密度成对矩阵。"""
    rng = np.random.default_rng(2605)
    rows = []
    for s, mu in [("夏季", 620), ("冬季", 520)]:
        p = rng.normal(mu, 85, 200)
        rows.append(pd.DataFrame({"负荷": p, "气温": 22 + 0.02 * p + rng.normal(0, 4, 200),
                                  "谐波": 4 + 0.005 * p + rng.normal(0, 1.0, 200),
                                  "季节": s}))
    df = pd.concat(rows, ignore_index=True)
    g = sns.PairGrid(df, hue="季节", palette=[C3, C1], height=1.75)
    g.map_offdiag(sns.scatterplot, s=8, edgecolor="white", linewidth=0.2)
    g.map_diag(sns.kdeplot, fill=True, alpha=0.45)
    g.add_legend(title="季节", framealpha=0.6)
    g.figure.set_size_inches(4.4, 4.2)
    save_grid(g, "26_成对网格", "26_成对网格_按季节密度", multi=True,
              w_mm=112, h_mm=106,
              adjust=dict(left=0.135, bottom=0.11, right=0.95, top=0.94))

说明

  • axes 级图 1181×944 px;figure 级多轴图按内容取 1100~1470 px 宽。
  • 图内不放标题,图题由本文件的标题承担。
  • 数据为 10 kV 配网、380 V 低压、50 Hz 量级的仿真算例,随机种子固定。
posted @ 2026-11-08 08:33  小青蛙电气  阅读(2)  评论(0)    收藏  举报