可以使用Matplotlib库中的plt.subplots函数来创建子图,并在子图中绘制散点矩阵图。如果想要删除空图,可以使用plt.delaxes函数来删除指定的子图。

下面是一个示例代码:

import numpy as np
import matplotlib.pyplot as plt

# 生成随机数据
np.random.seed(123)
data = np.random.randn(100, 4)

# 创建子图
fig, axs = plt.subplots(4, 4, figsize=(10, 10))

# 绘制散点矩阵图
for i in range(4):
    for j in range(4):
        if i == j:
            axs[i, j].hist(data[:, i], bins=10)
        else:
            axs[i, j].scatter(data[:, j], data[:, i])

# 删除空图
plt.delaxes(axs[0, 2])
plt.delaxes(axs[0, 3])
plt.delaxes(axs[1, 2])
plt.delaxes(axs[1, 3])
plt.delaxes(axs[2, 0])
plt.delaxes(axs[2, 1])
plt.delaxes(axs[3, 0])
plt.delaxes(axs[3, 1])

plt.show()

在上面的代码中,我们首先使用np.random.randn函数生成了一个随机数据矩阵,然后使用plt.subplots函数创建了一个4x4的子图,并在每个子图中绘制了散点矩阵图或直方图。最后,使用plt.delaxes函数删除了指定的空图。运行上面的代码,可以得到如下的散点矩阵图:

![scatter_matrix](https://img-blog.csdn.net/20180320152311733?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZ3VhbmdfZGFuZ2Vyb2ZfY2FybG9z/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/q/80

Python中散点矩阵图有空图怎么删除

原文地址: https://www.cveoy.top/t/topic/hhrp 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录