R有一个有用的函数pairs
,它提供了数据集中变量之间成对连接图的很好矩阵。所得的情节看起来类似于下图,从this blog post复制:matplotlib类似的R````
有没有准备基于Python的matplolib使用功能?我搜查了它的gallery,但找不到类似我需要的东西。从技术上讲,这应该是一项简单的任务,但对所有可能的案例,标签,标题等的适当处理是非常乏味的。
UPDATE请参阅下面我的答案,以快速和肮脏的近似值。
R有一个有用的函数pairs
,它提供了数据集中变量之间成对连接图的很好矩阵。所得的情节看起来类似于下图,从this blog post复制:matplotlib类似的R````
有没有准备基于Python的matplolib使用功能?我搜查了它的gallery,但找不到类似我需要的东西。从技术上讲,这应该是一项简单的任务,但对所有可能的案例,标签,标题等的适当处理是非常乏味的。
UPDATE请参阅下面我的答案,以快速和肮脏的近似值。
Pandas
有一个内置的功能scatter_matrix
(source code),它是这样的。
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
df = pd.DataFrame(np.random.randn(1000, 4), columns=['A','B','C','D'])
axes = pd.tools.plotting.scatter_matrix(df, alpha=0.2)
plt.tight_layout()
plt.savefig('scatter_matrix.png')
然而,它是特定pandas
(但也可使用作为起点)。
还有一些R
像大熊猫的情节。看看docs。
据我所知,没有这样的随时可用的功能。
快速和肮脏的接近我的需求:
def pair(data, labels=None):
""" Generate something similar to R `pair` """
nVariables = data.shape[1]
if labels is None:
labels = ['var%d'%i for i in range(nVariables)]
fig = pl.figure()
for i in range(nVariables):
for j in range(nVariables):
nSub = i * nVariables + j + 1
ax = fig.add_subplot(nVariables, nVariables, nSub)
if i == j:
ax.hist(data[:,i])
ax.set_title(labels[i])
else:
ax.plot(data[:,i], data[:,j], '.k')
return fig
上面的代码特此发布到公共领域
对我来说,在基本模块代码中有额外的价值。这非常明确,我将把它作为数据处理任务的指导。一个问题:“数据”可能是什么对象类型? – Merlin 2017-11-22 05:00:55
在最新版本的matplotlib的subplots
功能(至少是1.4),使这是一个有点容易:
def pairs(data, names):
"Quick&dirty scatterplot matrix"
d = len(data)
fig, axes = plt.subplots(nrows=d, ncols=d, sharex='col', sharey='row')
for i in range(d):
for j in range(d):
ax = axes[i,j]
if i == j:
ax.text(0.5, 0.5, names[i], transform=ax.transAxes,
horizontalalignment='center', verticalalignment='center',
fontsize=16)
else:
ax.scatter(data[j], data[i], s=10)
如果应用于scikit-learn的数据集标准,则“len(data)”将返回观察值的数量,而不是变量的数量。 'd = data.shape [1]'是这种情况下的解决方案。 – 2016-09-18 12:14:34
Seaborn有这个,请参阅:http://seaborn.pydata.org/generated/seaborn。 pairplot.html – 2017-09-14 19:04:37