You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用plotly figure_factory仅绘制散点图矩阵的下半部分

问题解答

一、figure_factory 实现仅展示散点图矩阵下半部分

plotly.figure_factory 的 create_scatterplotmatrix 接口没有内置类似 showupperhalf 的参数,但可以通过手动修改生成的 Figure 对象的 trace 可见性实现该效果,示例代码如下:

import plotly.figure_factory as ff
import numpy as np
import pandas as pd

# 示例数据生成
df = pd.DataFrame(np.random.randn(20, 4),
                columns=['Column A', 'Column B', 'Column C', 'Column D'])
df['Fruit'] = pd.Series(['apple', 'apple', 'grape', 'apple', 'apple',
                                'grape', 'pear', 'pear', 'apple', 'pear',
                                'apple', 'apple', 'grape', 'apple', 'apple',
                                'grape', 'pear', 'pear', 'apple', 'pear'])

# 生成完整散点矩阵
fig = ff.create_scatterplotmatrix(df, diag='histogram',index='Fruit',
                                  height=800, width=800)

# 隐藏上半部分图表
n_dim = 4 # 数值列总数,此处为Column A到D共4列
for trace in fig.data:
    # 解析trace对应的矩阵坐标:xaxis索引、yaxis索引
    x_idx = int(trace.xaxis[1:]) - 1 if trace.xaxis != 'x' else 0
    y_idx = int(trace.yaxis[1:]) - 1 if trace.yaxis != 'y' else 0
    # 上半部分为x索引大于y索引的位置,隐藏对应trace
    if x_idx > y_idx:
        trace.visible = False

fig.show()

二、go.Splom 替换散点为直方图/箱线图的方案

go.Splom 是专门为散点矩阵设计的原生接口,仅支持散点类型,无法直接替换为直方图、箱线图等其他图表类型。如果需要实现其他类型的矩阵图,可以选用以下两种方案:

  • 继续使用 figure_factory 的 create_scatterplotmatrix:它本身支持通过 diag 参数设置对角线的图表类型,可选值包括 histogram(直方图)、box(箱线图)、kde 等,如果需要修改非对角线的图表类型,可以手动替换对应位置 trace 的类型和数据配置。
  • 手动构建子图网格:如果需要完全自定义每个位置的图表类型,可以通过 plotly.subplots.make_subplots 创建对应尺寸的网格,逐个位置绘制你需要的直方图、箱线图等,适配性最高。

内容的提问来源于stack exchange,提问作者Minecraft_Json

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.02 08:15:06