2016-06-19 45 views
2

Ich versuche, dieses Bild mit sklearn.datasets.load_iris und seaborn zu erstellen. Ich mag die Idee, fig, ax = plt.subplots() und dann seaborn 's ax=ax Attribut zu tun. Ich kann nicht herausfinden, wie diese Handlung zu erstellen: enter image description hereSeaborn Histogramm mit 4 Panels (2 x 2) in Python

ich auf Stackoverflow geprüft und fanden diese aber überlagert sie How To Plot Multiple Histograms On Same Plot With Seaborn

Hier ist mein Code und Grundstück:

# Iris Dataset 
from sklearn.datasets import load_iris 
import matplotlib.pyplot as plt 
import seaborn as sns; sns.set() 

%matplotlib inline 

DF_data = pd.DataFrame(load_iris().data, 
         columns = load_iris().feature_names, 
         index = ["iris_%d" % i for i in range(load_iris().data.shape[0])]) 

Se_targets = pd.Series(load_iris().target, 
         index = ["iris_%d" % i for i in range(load_iris().data.shape[0])], 
         name = "Targets") 

#Visualizing Iris Data 
D_targets = {0: 'Iris-Setosa', 
      1: 'Iris-Versicolor', 
      2: 'Iris-Virgnica'} 

D_features = {0: 'sepal length [cm]', 
       1: 'sepal width [cm]', 
       2: 'petal length [cm]', 
       3: 'petal width [cm]'} 

fig, ax = plt.subplots(nrows=2, ncols=2, figsize=(8, 6)) 

idx_feature = 0 

#Plot on 2 x 2 ax object 

for i in range(ax.shape[0]): 
    for j in range(0, ax.shape[1]): 
     for idx_target, label_target in list(D_targets.items()): 
      sns.distplot(DF_data.as_matrix()[Se_targets==idx_target, idx_feature], 
         label=D_features[idx_feature], 
         kde=False, 
         bins=10, 
         ax=ax[i][j])   
     idx_feature += 1 

plt.legend(loc='upper right', fancybox=True, fontsize=8) 

plt.tight_layout() 
plt.show() 

Mein Grundstück sucht ziemlich schlecht:

enter image description here

UPDAT E:

Als Reaktion auf @Cel Antwort, habe ich dieses Diagramm erreicht, aber ich war nicht in der Lage, die Etiketten zu beheben und die Linien um die Plots zu verdunkeln.

enter image description here

Antwort

2

Das Problem hierbei ist, dass Sie eine numpy Array mit einer boolean Serie indizieren anstelle eines boolean numpy Array.

Ich stimme zu, dass dies sehr unintuitiv ist. Wie in der Tat, sagen Sie numpy bereits, dass dies in der Zukunft geändert werden:

DF_data.as_matrix()[Se_targets==idx_target, 2] 

/Users/ch/miniconda/envs/sci34/lib/python3.4/site-packages/IPython/kernel/Haupt Py: 1: FutureWarning: in Zukunft wird boolean-Array-likes als boolean-Array-Index

Vorerst behandelt werden, sollte dies für Sie arbeiten:

sns.distplot(DF_data.as_matrix()[Se_targets.as_matrix()==idx_target, idx_feature], 
      label=D_features[idx_feature], 
      kde=False, 
      bins=10, 
      ax=ax[i][j]) 

Dies ist der vollständige Code:

# Iris Dataset 
import pandas as pd 
from sklearn.datasets import load_iris 
import matplotlib.pyplot as plt 
import seaborn as sns; sns.set() 
sns.set_style('whitegrid') 

%matplotlib inline 

DF_data = pd.DataFrame(load_iris().data, 
         columns = load_iris().feature_names, 
         index = ["iris_%d" % i for i in range(load_iris().data.shape[0])]) 

Se_targets = pd.Series(load_iris().target, 
         index = ["iris_%d" % i for i in range(load_iris().data.shape[0])], 
         name = "Targets") 

#Visualizing Iris Data 
D_targets = {0: 'Iris-Setosa', 
      1: 'Iris-Versicolor', 
      2: 'Iris-Virgnica'} 

D_features = {0: 'sepal length [cm]', 
       1: 'sepal width [cm]', 
       2: 'petal length [cm]', 
       3: 'petal width [cm]'} 

fig, ax = plt.subplots(nrows=2, ncols=2, figsize=(8, 6)) 

idx_feature = 0 

#Plot on 2 x 2 ax object 

for i in range(ax.shape[0]): 
    for j in range(0, ax.shape[1]): 
     for idx_target, label_target in list(D_targets.items()): 
      plot = sns.distplot(DF_data.as_matrix()[Se_targets.as_matrix()==idx_target, idx_feature], 
         label=D_features[idx_feature], 
         kde=False, 
         bins=10, 
         ax=ax[i][j]) 
      plot.set_xlabel(D_features[idx_feature]) 
     idx_feature += 1 

plt.legend(loc='upper right', fancybox=True, fontsize=8) 

plt.tight_layout() 

plot

+0

Hey danke für die Antwort tun. Ich habe immer noch Probleme mit meinen Etiketten. Ich habe meine neue Handlung hinzugefügt. Ich habe das Wörterbuch mit den Etiketten überprüft, und die Etiketten werden an dieser Stelle korrekt gedruckt. –

+1

'label = D_targets [idx_target]' behebt die Beschriftungen –

1

Oder Sie könnten

import pandas as pd 
import seaborn as sns 
import matplotlib.pyplot as plt 

iris = sns.load_dataset("iris") 
iris_long = pd.melt(iris, "species", var_name="measurement") 
g = sns.FacetGrid(iris_long, hue="species", col="measurement", col_wrap=2, sharex=False) 
g.map(plt.hist, "value", alpha=.4) 

enter image description here

+0

wie kann man es nicht sagen "measurement =" sagen? –

+0

Wie können Sie auch die Artenlabels in der Legende erhalten? –