2016-09-29 30 views
1

我需要在Python中创建一个散布矩阵。我尝试过使用scatter_matrix,但我想只留下对角线上方的散点图。熊猫scatter_matrix模拟函数对(lower.panel,upper.panel)

我真的开始(没有远),并且当列有名字(而不是默认数字)时我遇到了麻烦。

这里是我的代码:

import itertools 
import numpy as np 
import matplotlib.pyplot as plt 
import pandas as pd 

data=pd.DataFrame(np.random.randint(0,100,size=(10, 5)), columns=list('ABCDE')) #THE PROBLEM IS HERE - I WILL HAVE COLUMNS WITH NAMES 

d = data.shape[1] 

fig, axes = plt.subplots(nrows=d, ncols=d, sharex=True, sharey=True) 
for i in range(d): 
    for j in range(d): 
      ax = axes[i,j] 
      if i == j: 
       ax.text(0.5, 0.5, "Diagonal", transform=ax.transAxes, 
         horizontalalignment='center', verticalalignment='center', 
         fontsize=16) 
      else: 
       ax.scatter(data[j], data[i], s=10) 

回答

1

从数据帧中选择一列时,有一个问题。您可以使用iloc根据整数位置选择列。你最后行更改为:

ax.scatter(data.iloc[:,j], data.iloc[:,i], s=10) 

给出:

enter image description here