2017-10-06 74 views
0

目前,我有以下代码Matplotlib叠加柱状图使用`对大熊猫据帧scatter_matrix`

import matplotlib.pyplot as plt 
import pandas as pd 
from pandas.plotting import scatter_matrix 

df= pd.read_csv(file, sep=',') 
colors = list('r' if i==1 else 'b' for i in df['class']) # class is either 1 or 0 
plt.figure() 
scatter_matrix(df, color=colors) 
plt.show() 

这说明,而不是简单的输出如下

enter image description here

但在对角线这个情节,直方图我想显示堆叠直方图,如下所示,对于类“1”它是红色的,对于“0”它是蓝色的

enter image description here

请指导我该怎么做?

+0

有包称为'seaborn' – Wen

回答

1

使用seaborn的可能是用于绘制散布矩阵样的情节是非常有益的。但是,我不知道如何绘制一个堆积的直方图很容易地进入seaborn的PairGrid的对角线。
正如问题无论如何要求matplotlib,以下是使用熊猫和matplotlib的解决方案。不幸的是,它需要手工做很多事情。以下是一个例子(请注意,由于问题没有提供,因此seaborn只能导入以获取一些数据)。

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

# seaborn import just needed to get some data 
import seaborn as sns 
df = sns.load_dataset("iris") 


n_hist = 10 
category = "species" 
columns = ["sepal_length","sepal_width","petal_length","petal_width"] 
mi = df[columns].values.min() 
ma = df[columns].values.max() 
hist_bins = np.linspace(mi, ma, n_hist) 


fig, axes = plt.subplots(nrows=len(columns), ncols=len(columns), 
         sharex="col") 

for i,row in enumerate(columns): 
    for j,col in enumerate(columns): 
     ax= axes[i,j] 
     if i == j: 
      # diagonal 
      mi = df[col].values.min() 
      ma = df[col].values.max() 
      hist_bins = np.linspace(mi, ma, n_hist) 
      def hist(x): 
       h, e = np.histogram(x.dropna()[col], bins=hist_bins) 
       return pd.Series(h, e[:-1]) 
      b = df[[col,category]].groupby(category).apply(hist).T 
      values = np.cumsum(b.values, axis=1) 
      for k in range(len(b.columns)): 
       if k == 0: 
        ax.bar(b.index, values[:,k], width=np.diff(hist_bins)[0]) 
       else: 
        ax.bar(b.index, values[:,k], width=np.diff(hist_bins)[0], 
          bottom=values[:,k-1]) 
     else: 
      # offdiagonal 
      for (n,cat) in df.groupby(category): 
       ax.scatter(cat[col],cat[row], s = 5,label=n,) 
     ax.set_xlabel(col) 
     ax.set_ylabel(row) 
     #ax.legend() 
plt.tight_layout() 
plt.show() 

enter image description here

1

示例代码

import seaborn as sns 
sns.set(style="ticks") 
df = sns.load_dataset("iris") 
sns.pairplot(df, hue="species") 

enter image description here

+0

有seaborn和背景虚化也能做到这一点,但我想以某种方式与matplotlib工作。在matplotlib中可能吗?谢谢各位 – muazfaiz

+0

不幸的是,这个答案不会绘制**堆积的直方图。如果有人有一个很好的解决方案来绘制堆叠直方图,我很乐意看到它。 – ImportanceOfBeingErnest